forked from github/CopilotForXcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathContextUtils.swift
More file actions
77 lines (67 loc) · 2.62 KB
/
ContextUtils.swift
File metadata and controls
77 lines (67 loc) · 2.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import ConversationServiceProvider
import XcodeInspector
import Foundation
import Logger
public let supportedFileExtensions: Set<String> = ["swift", "m", "mm", "h", "cpp", "c", "js", "py", "rb", "java", "applescript", "scpt", "plist", "entitlements"]
private let skipPatterns: [String] = [
".git",
".svn",
".hg",
"CVS",
".DS_Store",
"Thumbs.db",
"node_modules",
"bower_components"
]
public struct ContextUtils {
static func matchesPatterns(_ url: URL, patterns: [String]) -> Bool {
let fileName = url.lastPathComponent
for pattern in patterns {
if fnmatch(pattern, fileName, 0) == 0 {
return true
}
}
return false
}
public static func getFilesInActiveWorkspace() -> [FileReference] {
guard let workspaceURL = XcodeInspector.shared.realtimeActiveWorkspaceURL,
let projectURL = XcodeInspector.shared.realtimeActiveProjectURL else {
return []
}
do {
let fileManager = FileManager.default
let enumerator = fileManager.enumerator(
at: projectURL,
includingPropertiesForKeys: [.isRegularFileKey, .isDirectoryKey],
options: [.skipsHiddenFiles]
)
var files: [FileReference] = []
while let fileURL = enumerator?.nextObject() as? URL {
// Skip items matching the specified pattern
if matchesPatterns(fileURL, patterns: skipPatterns) {
enumerator?.skipDescendants()
continue
}
let resourceValues = try fileURL.resourceValues(forKeys: [.isRegularFileKey, .isDirectoryKey])
// Handle directories if needed
if resourceValues.isDirectory == true {
continue
}
guard resourceValues.isRegularFile == true else { continue }
if supportedFileExtensions.contains(fileURL.pathExtension.lowercased()) == false {
continue
}
let relativePath = fileURL.path.replacingOccurrences(of: projectURL.path, with: "")
let fileName = fileURL.lastPathComponent
let file = FileReference(url: fileURL,
relativePath: relativePath,
fileName: fileName)
files.append(file)
}
return files
} catch {
Logger.client.error("Failed to get files in workspace: \(error)")
return []
}
}
}