@@ -41,43 +41,42 @@ public enum Environment {
4141 }
4242
4343 #warning("""
44- The current version causes an issue in real- time suggestion that when complet ion panel is own ,
45- the command handler can not find the correct workspace, so we are not using it for now .
44+ TODO : The current version causes an issue in real- time suggestion that when complet ion panel is open ,
45+ the command handler can not find the correct workspace.
4646 """ )
4747 public static var fetchCurrentProjectRootURL : ( _ fileURL: URL ? ) async throws
4848 -> URL ? = { fileURL in
49- return URL ( fileURLWithPath: " / " )
50- // let appleScript = """
51- // tell application "Xcode"
52- // return path of document of the first window
53- // end tell
54- // """
55- //
56- // let path = (try? await runAppleScript(appleScript)) ?? ""
57- // if !path.isEmpty {
58- // let trimmedNewLine = path.trimmingCharacters(in: .newlines)
59- // var url = URL(fileURLWithPath: trimmedNewLine)
60- // while !FileManager.default.fileIsDirectory(atPath: url.path) ||
61- // !url.pathExtension.isEmpty
62- // {
63- // url = url.deletingLastPathComponent()
64- // }
65- // return url
66- // }
67- //
68- // guard var currentURL = fileURL else { return nil }
69- // var firstDirectoryURL: URL?
70- // while currentURL.pathComponents.count > 1 {
71- // defer { currentURL.deleteLastPathComponent() }
72- // guard FileManager.default.fileIsDirectory(atPath: currentURL.path) else { continue }
73- // if firstDirectoryURL == nil { firstDirectoryURL = currentURL }
74- // let gitURL = currentURL.appendingPathComponent(".git")
75- // if FileManager.default.fileIsDirectory(atPath: gitURL.path) {
76- // return currentURL
77- // }
78- // }
79- //
80- // return firstDirectoryURL ?? fileURL
49+ let appleScript = """
50+ tell application " Xcode "
51+ return path of document of the first window
52+ end tell
53+ """
54+
55+ let path = ( try ? await runAppleScript ( appleScript) ) ?? " "
56+ if !path. isEmpty {
57+ let trimmedNewLine = path. trimmingCharacters ( in: . newlines)
58+ var url = URL ( fileURLWithPath: trimmedNewLine)
59+ while !FileManager. default. fileIsDirectory ( atPath: url. path) ||
60+ !url. pathExtension. isEmpty
61+ {
62+ url = url. deletingLastPathComponent ( )
63+ }
64+ return url
65+ }
66+
67+ guard var currentURL = fileURL else { return nil }
68+ var firstDirectoryURL : URL ?
69+ while currentURL. pathComponents. count > 1 {
70+ defer { currentURL. deleteLastPathComponent ( ) }
71+ guard FileManager . default. fileIsDirectory ( atPath: currentURL. path) else { continue }
72+ if firstDirectoryURL == nil { firstDirectoryURL = currentURL }
73+ let gitURL = currentURL. appendingPathComponent ( " .git " )
74+ if FileManager . default. fileIsDirectory ( atPath: gitURL. path) {
75+ return currentURL
76+ }
77+ }
78+
79+ return firstDirectoryURL ?? fileURL
8180 }
8281
8382 public static var fetchCurrentFileURL : ( ) async throws -> URL = {
0 commit comments