|
| 1 | +// |
| 2 | +// CEWorkspaceFile+Recursion.swift |
| 3 | +// CodeEdit |
| 4 | +// |
| 5 | +// Created by Matthijs Eikelenboom on 30/04/2023. |
| 6 | +// |
| 7 | + |
| 8 | +import Foundation |
| 9 | + |
| 10 | +extension CEWorkspaceFile { |
| 11 | + |
| 12 | + func childrenDescription(tabCount: Int) -> String { |
| 13 | + var myDetails = "\(String(repeating: "| ", count: max(tabCount - 1, 0)))\(tabCount != 0 ? "╰--" : "")" |
| 14 | + myDetails += "\(url.path)" |
| 15 | + if !self.isFolder { // if im a file, just return the url |
| 16 | + return myDetails |
| 17 | + } else { // if im a folder, return the url and its children's details |
| 18 | + var childDetails = "\(myDetails)" |
| 19 | + for child in children ?? [] { |
| 20 | + childDetails += "\n\(child.childrenDescription(tabCount: tabCount + 1))" |
| 21 | + } |
| 22 | + return childDetails |
| 23 | + } |
| 24 | + } |
| 25 | + |
| 26 | + /// Flattens the children of ``self`` recursively with depth. |
| 27 | + /// - Parameters: |
| 28 | + /// - depth: An int that indicates the how deep the tree files need to be flattened |
| 29 | + /// - ignoringFolders: A boolean on whether to ignore files that are Folders |
| 30 | + /// - Returns: An array of flattened `CEWorkspaceFiles` |
| 31 | + func flattenedChildren(withDepth depth: Int, ignoringFolders: Bool) -> [CEWorkspaceFile] { |
| 32 | + guard depth > 0 else { return [] } |
| 33 | + guard self.isFolder else { return [self] } |
| 34 | + var childItems: [CEWorkspaceFile] = ignoringFolders ? [] : [self] |
| 35 | + children?.forEach { child in |
| 36 | + childItems.append(contentsOf: child.flattenedChildren( |
| 37 | + withDepth: depth - 1, |
| 38 | + ignoringFolders: ignoringFolders |
| 39 | + )) |
| 40 | + } |
| 41 | + return childItems |
| 42 | + } |
| 43 | + |
| 44 | + /// Returns a list of `CEWorkspaceFiles` that are sibilings of ``self``. |
| 45 | + /// The `height` parameter lets the function navigate up the folder hierarchy to |
| 46 | + /// select a starting point from which it should start flettening the items. |
| 47 | + /// - Parameters: |
| 48 | + /// - height: `Int` that tells where to start in the hierarchy |
| 49 | + /// - ignoringFolders: Wether the sibling folders should be flattened |
| 50 | + /// - Returns: A list of `FileSystemItems` |
| 51 | + func flattenedSiblings(withHeight height: Int, ignoringFolders: Bool) -> [CEWorkspaceFile] { |
| 52 | + let topMostParent = self.getParent(withHeight: height) |
| 53 | + return topMostParent.flattenedChildren(withDepth: height, ignoringFolders: ignoringFolders) |
| 54 | + } |
| 55 | + |
| 56 | + /// Recursive function that returns the number of children |
| 57 | + /// that contain the `searchString` in their path or their subitems' paths. |
| 58 | + /// Returns `0` if the item is not a folder. |
| 59 | + /// - Parameters: |
| 60 | + /// - searchString: The string |
| 61 | + /// - ignoredStrings: The prefixes to ignore if they prefix file names |
| 62 | + /// - Returns: The number of children that match the conditiions |
| 63 | + func appearanceWithinChildrenOf(searchString: String, ignoredStrings: [String] = [".", "~"]) -> Int { |
| 64 | + var count = 0 |
| 65 | + guard self.isFolder else { return 0 } |
| 66 | + for child in self.children ?? [] { |
| 67 | + var isIgnored: Bool = false |
| 68 | + for ignoredString in ignoredStrings where child.name.hasPrefix(ignoredString) { |
| 69 | + isIgnored = true // can use regex later |
| 70 | + } |
| 71 | + |
| 72 | + if isIgnored { |
| 73 | + continue |
| 74 | + } |
| 75 | + |
| 76 | + guard !searchString.isEmpty else { count += 1; continue } |
| 77 | + if child.isFolder { |
| 78 | + count += child.appearanceWithinChildrenOf(searchString: searchString) > 0 ? 1 : 0 |
| 79 | + } else { |
| 80 | + count += child.name.lowercased().contains(searchString.lowercased()) ? 1 : 0 |
| 81 | + } |
| 82 | + } |
| 83 | + return count |
| 84 | + } |
| 85 | + |
| 86 | + /// Function that returns an array of the children |
| 87 | + /// that contain the `searchString` in their path or their subitems' paths. |
| 88 | + /// Similar to `appearanceWithinChildrenOf(searchString: String)` |
| 89 | + /// Returns `[]` if the item is not a folder. |
| 90 | + /// - Parameter searchString: The string |
| 91 | + /// - Parameter ignoredStrings: The prefixes to ignore if they prefix file names |
| 92 | + /// - Returns: The children that match the conditiions |
| 93 | + func childrenSatisfying(searchString: String, ignoredStrings: [String] = [".", "~"]) -> [CEWorkspaceFile] { |
| 94 | + var satisfyingChildren: [CEWorkspaceFile] = [] |
| 95 | + guard self.isFolder else { return [] } |
| 96 | + for child in self.children ?? [] { |
| 97 | + var isIgnored: Bool = false |
| 98 | + for ignoredString in ignoredStrings where child.name.hasPrefix(ignoredString) { |
| 99 | + isIgnored = true // can use regex later |
| 100 | + } |
| 101 | + |
| 102 | + if isIgnored { |
| 103 | + continue |
| 104 | + } |
| 105 | + |
| 106 | + guard !searchString.isEmpty else { satisfyingChildren.append(child); continue } |
| 107 | + if child.isFolder { |
| 108 | + if child.appearanceWithinChildrenOf(searchString: searchString) > 0 { |
| 109 | + satisfyingChildren.append(child) |
| 110 | + } |
| 111 | + } else { |
| 112 | + if child.name.lowercased().contains(searchString.lowercased()) { |
| 113 | + satisfyingChildren.append(child) |
| 114 | + } |
| 115 | + } |
| 116 | + } |
| 117 | + return satisfyingChildren |
| 118 | + } |
| 119 | + |
| 120 | + /// Using the current instance of `FileSystemItem` it will walk back up the Workspace file hiarchy |
| 121 | + /// the amount of times specified with the `withHeight` parameter. |
| 122 | + /// - Parameter height: The amount of times you want to up a folder. |
| 123 | + /// - Returns: The found `FileSystemItem` object, This should always be a folder. |
| 124 | + private func getParent(withHeight height: Int) -> CEWorkspaceFile { |
| 125 | + var topmostParent = self |
| 126 | + for _ in 0..<height { |
| 127 | + guard let parent = topmostParent.parent else { break } |
| 128 | + topmostParent = parent |
| 129 | + } |
| 130 | + |
| 131 | + return topmostParent |
| 132 | + } |
| 133 | + |
| 134 | +} |
0 commit comments