Skip to content

Commit

Permalink
Speed up filtering by icon type
Browse files Browse the repository at this point in the history
  • Loading branch information
swiftyfinch committed May 6, 2024
1 parent 959fdfe commit 3adceb6
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 9 deletions.
1 change: 1 addition & 0 deletions Sources/CLI/Commands/Impact.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ extension XTree.Impact {
roots: treeOptions.roots,
contains: treeOptions.contains,
except: treeOptions.except,
exceptIcons: [],
maxHeight: treeOptions.maxHeight
),
sort: treeOptions.sort
Expand Down
1 change: 1 addition & 0 deletions Sources/CLI/Commands/Print.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ extension XTree.Print {
roots: treeOptions.roots,
contains: treeOptions.contains,
except: treeOptions.except,
exceptIcons: [],
maxHeight: treeOptions.maxHeight
),
sort: treeOptions.sort,
Expand Down
10 changes: 9 additions & 1 deletion Sources/XTreeKit/Common/Tree/TreeFilter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,22 @@ final class TreeFilter {
_ tree: TreeNode,
contains: [Regex<Substring>],
except: [Regex<Substring>],
exceptIcons: Set<String>,
height: Int = 0,
maxHeight: Int? = nil
) -> TreeNode? {
if contains.isEmpty, except.isEmpty, maxHeight == nil { return tree }
if contains.isEmpty, except.isEmpty, exceptIcons.isEmpty, maxHeight == nil { return tree }
if let maxHeight = maxHeight, height >= maxHeight { return nil }
guard contains.isEmpty || contains.contains(where: tree.name.contains) else { return nil }
guard except.isEmpty || !except.contains(where: tree.name.contains) else { return nil }
guard !isIconExcept(tree.icon, exceptIcons: exceptIcons) else { return nil }

let filteredExplicitChildren = tree.explicitChildren.compactMap {
filter(
$0,
contains: contains,
except: except,
exceptIcons: exceptIcons,
height: height + 1,
maxHeight: maxHeight
)
Expand All @@ -34,4 +37,9 @@ final class TreeFilter {
)
)
}

private func isIconExcept(_ icon: TreeNode.Icon?, exceptIcons: Set<String>) -> Bool {
guard !exceptIcons.isEmpty, let icon else { return false }
return exceptIcons.contains(icon.sfSymbol)
}
}
4 changes: 4 additions & 0 deletions Sources/XTreeKit/Managers/TreeManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,20 @@ public struct TreeFilterOptions {
public var roots: [String]
public var contains: [String]
public var except: [String]
public var exceptIcons: Set<String>
public var maxHeight: Int?

public init(
roots: [String],
contains: [String],
except: [String],
exceptIcons: Set<String>,
maxHeight: Int? = nil
) {
self.roots = roots
self.contains = contains
self.except = except
self.exceptIcons = exceptIcons
self.maxHeight = maxHeight
}
}
Expand Down Expand Up @@ -83,6 +86,7 @@ extension TreeManager: IInternalTreeManager {
tree,
contains: filter.contains.map(regexBuilder.build(wildcardsPattern:)),
except: filter.except.map(regexBuilder.build(wildcardsPattern:)),
exceptIcons: filter.exceptIcons,
maxHeight: filter.maxHeight
)
}
Expand Down
11 changes: 3 additions & 8 deletions XTree/XTree/Modules/TreeView/TreeBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ final class TreeBuilder: ObservableObject {
guard let fileURL else { return completion(nil) }
Task.detached {
do {
var filter = TreeFilterOptions(roots: roots, contains: contains, except: except)
var filter = TreeFilterOptions(roots: roots, contains: contains, except: except, exceptIcons: [])
let sort = Sort(rawValue: sorting.lowercased()) ?? .name
let adjacentList = try await self.buildAdjacentList(
path: fileURL.path,
Expand All @@ -46,16 +46,11 @@ final class TreeBuilder: ObservableObject {
self.userDefaultsStorage.fileURL = fileURL

let icons = self.icons(adjacentList: adjacentList)
let hiddenByIconTitles = adjacentList.filter {
guard let icon = $0.value.icon else { return false }
return hiddenIcons.contains(icon.sfSymbol)
}.map(\.value.title)

let filteredAdjacentList: [String: TreeNodeContent]
if hiddenByIconTitles.isEmpty {
if hiddenIcons.isEmpty {
filteredAdjacentList = adjacentList
} else {
filter.except += hiddenByIconTitles
filter.exceptIcons.formUnion(hiddenIcons)
filteredAdjacentList = try await self.buildAdjacentList(
path: fileURL.path,
filter: filter,
Expand Down

0 comments on commit 3adceb6

Please sign in to comment.