Skip to content

Commit

Permalink
Simplify is_valid_directory and is_valid_textfile
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeremy Wootten authored and Jeremy Wootten committed Nov 25, 2024
1 parent 098ef7a commit 25bcf80
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 33 deletions.
40 changes: 19 additions & 21 deletions src/FolderManager/File.vala
Original file line number Diff line number Diff line change
Expand Up @@ -83,31 +83,29 @@ namespace Scratch.FolderManager {
}

// Checks if we're dealing with a non-backup directory
// Hidden subfolders are not shown by default, but we need to allow hidden top-level folder
public bool is_valid_directory (bool allow_hidden = true) {
if ((!allow_hidden && name.has_prefix (".")) || // If parent is hidden then inherit validity from parent
info.get_is_backup ()) {

return false;
}
// If parent is hidden then inherit validity from parent
private bool? _is_valid_directory = null;
public bool is_valid_directory {
get {
if (_is_valid_directory == null) {
_is_valid_directory = info != null &&
!info.get_is_backup () &&
info.get_file_type () == FileType.DIRECTORY;
}

if (info.get_file_type () == FileType.DIRECTORY) {
return true;
return _is_valid_directory;
}

return false;
}

public bool is_temporary {
get {
return path.has_suffix ("~");
}
}

// checks if we're dealing with a textfile
private bool? _is_valid_textfile = null;
public bool is_valid_textfile {
get {
return Utils.check_if_valid_text_file (path, info);
if (_is_valid_textfile == null) {
_is_valid_textfile = !path.has_suffix ("~") && Utils.check_if_valid_text_file (path, info);
}

return _is_valid_textfile;
}
}

Expand Down Expand Up @@ -143,7 +141,7 @@ namespace Scratch.FolderManager {
while ((file_info = enumerator.next_file ()) != null) {
var child = file.get_child (file_info.get_name ());
var child_file = new File (child.get_path ());
if (child_file.is_valid_directory () || child_file.is_valid_textfile) {
if (child_file.is_valid_directory || child_file.is_valid_textfile) {
_children.add (child_file);
}
}
Expand Down Expand Up @@ -200,10 +198,10 @@ namespace Scratch.FolderManager {
}

public static int compare (File a, File b) {
if (a.is_valid_directory () && b.is_valid_textfile) {
if (a.is_valid_directory && b.is_valid_textfile) {
return -1;
}
if (a.is_valid_textfile && b.is_valid_directory ()) {
if (a.is_valid_textfile && b.is_valid_directory) {
return 1;
}

Expand Down
2 changes: 1 addition & 1 deletion src/FolderManager/FileView.vala
Original file line number Diff line number Diff line change
Expand Up @@ -509,7 +509,7 @@ public class Scratch.FolderManager.FileView : Code.Widgets.SourceList, Code.Pane
if (is_open (folder)) {
warning ("Folder '%s' is already open.", folder.path);
return;
} else if (!folder.is_valid_directory (true)) { // Allow hidden top-level folders
} else if (!folder.is_valid_directory) {
warning ("Cannot open invalid directory.");
return;
}
Expand Down
19 changes: 8 additions & 11 deletions src/FolderManager/FolderItem.vala
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ namespace Scratch.FolderManager {
private bool has_dummy;
private Code.Widgets.SourceList.Item dummy; /* Blank item for expanded empty folders */

public FolderItem (File file, FileView view) requires (file.is_valid_directory) {
public FolderItem (File file, FileView view) {
Object (file: file, view: view);
}

Expand Down Expand Up @@ -63,16 +63,14 @@ namespace Scratch.FolderManager {
file.children.size > 0) {

foreach (var child in file.children) {
Code.Widgets.SourceList.Item item = null;
if (child.is_valid_directory ()) {
Code.Widgets.SourceList.Item? item = null;
if (child.is_valid_directory) {
item = new FolderItem (child, view);
} else if (child.is_valid_textfile) {
item = new FileItem (child, view);
}

if (item != null) {
add (item);
}
add (item); // ignores null parameter
}

children_loaded = true;
Expand Down Expand Up @@ -304,15 +302,14 @@ namespace Scratch.FolderManager {
var path_item = find_item_for_path (source.get_path ());
if (path_item == null) {
var file = new File (source.get_path ());
if (file.is_valid_directory ()) {
if (file.is_valid_directory) {
path_item = new FolderItem (file, view);
} else if (!file.is_temporary) {

} else if (file.is_valid_textfile) {
path_item = new FileItem (file, view);
} else {
break;
}

add (path_item);
add (path_item); // null parameter is silently ignored
}

break;
Expand Down

0 comments on commit 25bcf80

Please sign in to comment.