-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Improved classes search speed * Removed comments
- Loading branch information
Showing
6 changed files
with
126 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import Foundation | ||
|
||
struct FSIndex { | ||
let classes: [String: String] | ||
|
||
init(path: URL) throws { | ||
self.classes = try Self.classes(in: path) | ||
} | ||
} | ||
|
||
extension FSIndex { | ||
private static func classes(in path: URL) throws -> [String: String] { | ||
let fileManager = FileManager.default | ||
|
||
var classDictionary: [String: String] = [:] | ||
|
||
// Create a DirectoryEnumerator to recursively search for .swift files | ||
let enumerator = fileManager.enumerator( | ||
at: URL(fileURLWithPath: path.relativePath), | ||
includingPropertiesForKeys: [.isRegularFileKey], | ||
options: [.skipsHiddenFiles] | ||
) { (url, error) -> Bool in | ||
DBLogger.logWarning("Directory enumeration error at \(url)") | ||
DBLogger.logWarning(error.localizedDescription) | ||
return true | ||
} | ||
|
||
// Regular expression to find class names | ||
let regex = try NSRegularExpression(pattern: "class\\s+([A-Za-z_][A-Za-z_0-9]*)", options: []) | ||
|
||
// Iterate over each file found by the enumerator | ||
while let element = enumerator?.nextObject() as? URL { | ||
let isFile = try element.resourceValues(forKeys: [.isRegularFileKey]).isRegularFile ?? false | ||
guard isFile, | ||
element.pathExtension == "swift" else { | ||
continue | ||
} | ||
|
||
let fileContent = try String(contentsOf: element, encoding: .utf8) | ||
|
||
// Search for class definitions | ||
let nsRange = NSRange(fileContent.startIndex..<fileContent.endIndex, in: fileContent) | ||
let matches = regex.matches(in: fileContent, options: [], range: nsRange) | ||
|
||
// Extract class names from the matches and store them in the dictionary | ||
for match in matches { | ||
if let range = Range(match.range(at: 1), in: fileContent) { | ||
let className = String(fileContent[range]) | ||
let relativePath = try element.relativePath(from: path) ?! Error.cantGetRelativePath(filePath: element, basePath: path) | ||
classDictionary[className] = relativePath | ||
} | ||
} | ||
} | ||
|
||
return classDictionary | ||
} | ||
|
||
enum Error: Swift.Error { | ||
case cantGetRelativePath(filePath: URL, basePath: URL) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,3 @@ | ||
// | ||
// Logger.swift | ||
// | ||
// | ||
// Created by Aleksey Berezka on 19.12.2023. | ||
// | ||
|
||
import Foundation | ||
|
||
class DBLogger { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import Foundation | ||
|
||
extension URL { | ||
var isRegularFile: Bool { | ||
get throws { | ||
try resourceValues(forKeys: [.isRegularFileKey]).isRegularFile ?! Error.noResourceValues | ||
} | ||
} | ||
|
||
enum Error: Swift.Error { | ||
case noResourceValues | ||
} | ||
} | ||
|
||
extension URL { | ||
/// Returns a relative path from a base URL | ||
/// - Parameter baseURL: The base URL to calculate the relative path from. | ||
/// - Returns: A relative path if possible, otherwise nil. | ||
func relativePath(from baseURL: URL) -> String? { | ||
// Check if both URLs are file URLs and that the base URL is a directory | ||
guard self.isFileURL, baseURL.isFileURL, baseURL.hasDirectoryPath else { | ||
return nil | ||
} | ||
|
||
// Remove/replace "." and "..", make sure URLs are absolute: | ||
let pathComponents = standardized.pathComponents | ||
let basePathComponents = baseURL.standardized.pathComponents | ||
|
||
// Find the number of common path components | ||
let commonPart = zip(pathComponents, basePathComponents).prefix { $0 == $1 }.count | ||
|
||
// Build the relative path | ||
let relativeComponents = Array(repeating: "..", count: basePathComponents.count - commonPart) + | ||
pathComponents.dropFirst(commonPart) | ||
|
||
return relativeComponents.joined(separator: "/") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import Foundation | ||
|
||
infix operator ?!: NilCoalescingPrecedence | ||
|
||
/// Throws the right hand side error if the left hand side optional is `nil`. | ||
func ?!<T>(value: T?, error: @autoclosure () -> Error) throws -> T { | ||
guard let value = value else { | ||
throw error() | ||
} | ||
return value | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters