Skip to content

Commit 8334ce8

Browse files
committed
Make active project/workspace/document URL optional
1 parent d4c811c commit 8334ce8

File tree

8 files changed

+53
-49
lines changed

8 files changed

+53
-49
lines changed

Core/Sources/ChatService/CustomCommandTemplateProcessor.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ struct CustomCommandTemplateProcessor {
3434
func getEditorInformation() -> EditorInformation {
3535
let editorContent = XcodeInspector.shared.focusedEditor?.content
3636
let documentURL = XcodeInspector.shared.activeDocumentURL
37-
let language = languageIdentifierFromFileURL(documentURL)
37+
let language = documentURL.map(languageIdentifierFromFileURL) ?? .plaintext
3838

3939
return .init(
4040
editorContent: editorContent,

Core/Sources/Service/GUI/ChatTabFactory.swift

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,10 @@ enum ChatTabFactory {
4545
let content = editor.content
4646
return .init(
4747
selectedText: content.selectedContent,
48-
language: languageIdentifierFromFileURL(
49-
XcodeInspector.shared
50-
.activeDocumentURL
51-
)
52-
.rawValue,
48+
language: (
49+
XcodeInspector.shared.activeDocumentURL
50+
.map(languageIdentifierFromFileURL) ?? .plaintext
51+
).rawValue,
5352
fileContent: content.content
5453
)
5554
},

Core/Sources/Service/RealtimeSuggestionController.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,9 @@ public actor RealtimeSuggestionController {
109109
await self.triggerPrefetchDebounced()
110110
await self.notifyEditingFileChange(editor: focusElement)
111111
case kAXSelectedTextChangedNotification:
112-
guard let sourceEditor = await sourceEditor else { continue }
113-
let fileURL = XcodeInspector.shared.activeDocumentURL
112+
guard let sourceEditor = await sourceEditor,
113+
let fileURL = XcodeInspector.shared.activeDocumentURL
114+
else { continue }
114115
await PseudoCommandHandler().invalidateRealtimeSuggestionsIfNeeded(
115116
fileURL: fileURL,
116117
sourceEditor: sourceEditor

Core/Sources/SuggestionWidget/FeatureReducers/PanelFeature.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,9 @@ public struct PanelFeature: ReducerProtocol {
5252
switch action {
5353
case .presentSuggestion:
5454
return .run { send in
55-
guard let provider = await fetchSuggestionProvider(
56-
fileURL: xcodeInspector.activeDocumentURL
57-
) else { return }
55+
guard let fileURL = xcodeInspector.activeDocumentURL,
56+
let provider = await fetchSuggestionProvider(fileURL: fileURL)
57+
else { return }
5858
await send(.presentSuggestionProvider(provider, displayContent: true))
5959
}
6060

@@ -96,7 +96,7 @@ public struct PanelFeature: ReducerProtocol {
9696
case .switchToAnotherEditorAndUpdateContent:
9797
state.content.error = nil
9898
return .run { send in
99-
let fileURL = xcodeInspector.activeDocumentURL
99+
guard let fileURL = xcodeInspector.realtimeActiveDocumentURL else { return }
100100
if let suggestion = await fetchSuggestionProvider(fileURL: fileURL) {
101101
await send(.presentSuggestionProvider(suggestion, displayContent: false))
102102
}

Core/Sources/SuggestionWidget/WidgetView.swift

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ struct WidgetView: View {
1010
@State var isHovering: Bool = false
1111
var onOpenChatClicked: () -> Void = {}
1212
var onCustomCommandClicked: (CustomCommand) -> Void = { _ in }
13-
13+
1414
@AppStorage(\.hideCircularWidget) var hideCircularWidget
1515

1616
var body: some View {
@@ -216,8 +216,9 @@ extension WidgetContextMenu {
216216
@ViewBuilder
217217
var enableSuggestionForProject: some View {
218218
WithViewStore(store) { _ in
219-
let projectPath = xcodeInspector.activeProjectRootURL.path
220-
if disableSuggestionFeatureGlobally {
219+
if let projectPath = xcodeInspector.activeProjectRootURL?.path,
220+
disableSuggestionFeatureGlobally
221+
{
221222
let matchedPath = suggestionFeatureEnabledProjectList.first { path in
222223
projectPath.hasPrefix(path)
223224
}
@@ -243,7 +244,7 @@ extension WidgetContextMenu {
243244
var disableSuggestionForLanguage: some View {
244245
WithViewStore(store) { _ in
245246
let fileURL = xcodeInspector.activeDocumentURL
246-
let fileLanguage = languageIdentifierFromFileURL(fileURL)
247+
let fileLanguage = fileURL.map(languageIdentifierFromFileURL) ?? .plaintext
247248
let matched = suggestionFeatureDisabledLanguageList.first { rawValue in
248249
fileLanguage.rawValue == rawValue
249250
}

ExtensionService/AppDelegate+Menu.swift

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,12 @@ extension AppDelegate: NSMenuDelegate {
9595
case xcodeInspectorDebugMenuIdentifier:
9696
let inspector = XcodeInspector.shared
9797
menu.items.removeAll()
98-
menu.items.append(.text("Active Project: \(inspector.activeProjectRootURL)"))
99-
menu.items.append(.text("Active Workspace: \(inspector.activeWorkspaceURL)"))
100-
menu.items.append(.text("Active Document: \(inspector.activeDocumentURL)"))
98+
menu.items
99+
.append(.text("Active Project: \(inspector.activeProjectRootURL?.path ?? "N/A")"))
100+
menu.items
101+
.append(.text("Active Workspace: \(inspector.activeWorkspaceURL?.path ?? "N/A")"))
102+
menu.items
103+
.append(.text("Active Document: \(inspector.activeDocumentURL?.path ?? "N/A")"))
101104
for xcode in inspector.xcodes {
102105
let item = NSMenuItem(
103106
title: "Xcode \(xcode.runningApplication.processIdentifier)",
@@ -108,9 +111,12 @@ extension AppDelegate: NSMenuDelegate {
108111
let xcodeMenu = NSMenu()
109112
item.submenu = xcodeMenu
110113
xcodeMenu.items.append(.text("Is Active: \(xcode.isActive)"))
111-
xcodeMenu.items.append(.text("Active Project: \(xcode.projectRootURL)"))
112-
xcodeMenu.items.append(.text("Active Workspace: \(xcode.workspaceURL)"))
113-
xcodeMenu.items.append(.text("Active Document: \(xcode.documentURL)"))
114+
xcodeMenu.items
115+
.append(.text("Active Project: \(xcode.projectRootURL?.path ?? "N/A")"))
116+
xcodeMenu.items
117+
.append(.text("Active Workspace: \(xcode.workspaceURL?.path ?? "N/A")"))
118+
xcodeMenu.items
119+
.append(.text("Active Document: \(xcode.documentURL?.path ?? "N/A")"))
114120

115121
for (key, workspace) in xcode.realtimeWorkspaces {
116122
let workspaceItem = NSMenuItem(

Pro

Submodule Pro updated from 0034a4e to 27e94aa

Tool/Sources/XcodeInspector/XcodeInspector.swift

Lines changed: 23 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,23 @@ public final class XcodeInspector: ObservableObject {
1717
@Published public internal(set) var activeXcode: XcodeAppInstanceInspector?
1818
@Published public internal(set) var latestActiveXcode: XcodeAppInstanceInspector?
1919
@Published public internal(set) var xcodes: [XcodeAppInstanceInspector] = []
20-
@Published public internal(set) var activeProjectRootURL = URL(fileURLWithPath: "/")
21-
@Published public internal(set) var activeDocumentURL = URL(fileURLWithPath: "/")
22-
@Published public internal(set) var activeWorkspaceURL = URL(fileURLWithPath: "/")
20+
@Published public internal(set) var activeProjectRootURL: URL? = nil
21+
@Published public internal(set) var activeDocumentURL: URL? = nil
22+
@Published public internal(set) var activeWorkspaceURL: URL? = nil
2323
@Published public internal(set) var focusedWindow: XcodeWindowInspector?
2424
@Published public internal(set) var focusedEditor: SourceEditor?
2525
@Published public internal(set) var focusedElement: AXUIElement?
2626
@Published public internal(set) var completionPanel: AXUIElement?
2727

2828
public var focusedEditorContent: EditorInformation? {
29+
guard let documentURL = XcodeInspector.shared.realtimeActiveDocumentURL,
30+
let workspaceURL = XcodeInspector.shared.realtimeActiveWorkspaceURL,
31+
let projectURL = XcodeInspector.shared.activeProjectRootURL
32+
else { return nil }
33+
2934
let editorContent = XcodeInspector.shared.focusedEditor?.content
30-
let documentURL = XcodeInspector.shared.realtimeActiveDocumentURL
31-
let workspaceURL = XcodeInspector.shared.realtimeActiveWorkspaceURL
32-
let projectURL = XcodeInspector.shared.activeProjectRootURL
3335
let language = languageIdentifierFromFileURL(documentURL)
34-
let relativePath = documentURL.path
35-
.replacingOccurrences(of: projectURL.path, with: "")
36+
let relativePath = documentURL.path.replacingOccurrences(of: projectURL.path, with: "")
3637

3738
if let editorContent, let range = editorContent.selections.first {
3839
let (selectedContent, selectedLines) = EditorInformation.code(
@@ -63,15 +64,15 @@ public final class XcodeInspector: ObservableObject {
6364
)
6465
}
6566

66-
public var realtimeActiveDocumentURL: URL {
67+
public var realtimeActiveDocumentURL: URL? {
6768
latestActiveXcode?.realtimeDocumentURL ?? activeDocumentURL
6869
}
69-
70-
public var realtimeActiveWorkspaceURL: URL {
70+
71+
public var realtimeActiveWorkspaceURL: URL? {
7172
latestActiveXcode?.realtimeWorkspaceURL ?? activeWorkspaceURL
7273
}
73-
74-
public var realtimeActiveProjectURL: URL {
74+
75+
public var realtimeActiveProjectURL: URL? {
7576
latestActiveXcode?.realtimeProjectURL ?? activeWorkspaceURL
7677
}
7778

@@ -192,7 +193,7 @@ public final class XcodeInspector: ObservableObject {
192193
xcode.$documentURL.sink { [weak self] url in
193194
self?.activeDocumentURL = url
194195
}.store(in: &activeXcodeCancellable)
195-
196+
196197
xcode.$workspaceURL.sink { [weak self] url in
197198
self?.activeWorkspaceURL = url
198199
}.store(in: &activeXcodeCancellable)
@@ -224,9 +225,9 @@ public class AppInstanceInspector: ObservableObject {
224225

225226
public final class XcodeAppInstanceInspector: AppInstanceInspector {
226227
@Published public var focusedWindow: XcodeWindowInspector?
227-
@Published public var documentURL: URL = .init(fileURLWithPath: "/")
228-
@Published public var workspaceURL: URL = .init(fileURLWithPath: "/")
229-
@Published public var projectRootURL: URL = .init(fileURLWithPath: "/")
228+
@Published public var documentURL: URL? = nil
229+
@Published public var workspaceURL: URL? = nil
230+
@Published public var projectRootURL: URL? = nil
230231
@Published public var workspaces = [WorkspaceIdentifier: Workspace]()
231232
public var realtimeWorkspaces: [WorkspaceIdentifier: WorkspaceInfo] {
232233
updateWorkspaceInfo()
@@ -238,25 +239,21 @@ public final class XcodeAppInstanceInspector: AppInstanceInspector {
238239
public var realtimeDocumentURL: URL? {
239240
guard let window = appElement.focusedWindow,
240241
window.identifier == "Xcode.WorkspaceWindow"
241-
else {
242-
return nil
243-
}
242+
else { return nil }
244243

245244
return WorkspaceXcodeWindowInspector.extractDocumentURL(windowElement: window)
246245
}
247-
246+
248247
public var realtimeWorkspaceURL: URL? {
249248
guard let window = appElement.focusedWindow,
250249
window.identifier == "Xcode.WorkspaceWindow"
251-
else {
252-
return nil
253-
}
250+
else { return nil }
254251

255252
return WorkspaceXcodeWindowInspector.extractWorkspaceURL(windowElement: window)
256253
}
257-
254+
258255
public var realtimeProjectURL: URL? {
259-
guard let window = appElement.focusedWindow else { return URL(fileURLWithPath: "/") }
256+
guard let window = appElement.focusedWindow else { return nil }
260257
let workspaceURL = realtimeWorkspaceURL
261258
let documentURL = realtimeDocumentURL
262259
return WorkspaceXcodeWindowInspector.extractProjectURL(

0 commit comments

Comments
 (0)