Skip to content

Commit 18203a5

Browse files
committed
Change re-attach to attach to the initial selection range
1 parent 33f4d78 commit 18203a5

File tree

1 file changed

+9
-42
lines changed

1 file changed

+9
-42
lines changed

Core/Sources/PromptToCodeService/PromptToCodeService.swift

Lines changed: 9 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import FocusedCodeFinder
21
import Foundation
32
import OpenAIService
43
import SuggestionModel
@@ -42,6 +41,7 @@ public final class PromptToCodeService: ObservableObject {
4241
@Published public var description: String = ""
4342
@Published public var isContinuous = false
4443
@Published public var selectionRange: CursorRange?
44+
@Published public var isAttachedToSelectionRange: Bool
4545
public var canRevert: Bool { history != .empty }
4646
public var language: CodeLanguage
4747
public var indentSize: Int
@@ -73,6 +73,7 @@ public final class PromptToCodeService: ObservableObject {
7373
self.fileURL = fileURL
7474
self.allCode = allCode
7575
history = .empty
76+
isAttachedToSelectionRange = true
7677
self.extraSystemPrompt = extraSystemPrompt
7778
self.generateDescriptionRequirement = generateDescriptionRequirement
7879
}
@@ -93,8 +94,12 @@ public final class PromptToCodeService: ObservableObject {
9394
indentSize: indentSize,
9495
usesTabsForIndentation: usesTabsForIndentation,
9596
requirement: prompt,
96-
projectRootURL: projectRootURL,
97-
fileURL: fileURL,
97+
projectRootURL: isAttachedToSelectionRange
98+
? projectRootURL
99+
: XcodeInspector.shared.activeProjectURL,
100+
fileURL: isAttachedToSelectionRange
101+
? fileURL
102+
: XcodeInspector.shared.activeDocumentURL,
98103
allCode: allCode,
99104
extraSystemPrompt: extraSystemPrompt,
100105
generateDescriptionRequirement: generateDescriptionRequirement
@@ -130,45 +135,7 @@ public final class PromptToCodeService: ObservableObject {
130135
}
131136

132137
public func toggleAttachOrDetachToCode() {
133-
if selectionRange != nil {
134-
selectionRange = nil
135-
} else {
136-
let inspector = XcodeInspector.shared
137-
guard let editor = XcodeInspector.shared.focusedEditorContent
138-
else { return }
139-
if let range = editor.editorContent?.selections.first, range.start != range.end {
140-
selectionRange = range
141-
} else {
142-
let focusedCodeFinder: FocusedCodeFinder = {
143-
switch language {
144-
case .builtIn(.swift):
145-
return SwiftFocusedCodeFinder()
146-
default:
147-
return UnknownLanguageFocusedCodeFinder(proposedSearchRange: 10)
148-
}
149-
}()
150-
151-
let cursorPosition = editor.editorContent?.cursorPosition ?? .zero
152-
let codeContext = focusedCodeFinder.findFocusedCode(
153-
containingRange: .init(start: cursorPosition, end: cursorPosition),
154-
activeDocumentContext: .init(
155-
filePath: editor.documentURL.path,
156-
relativePath: editor.relativePath,
157-
language: editor.language,
158-
fileContent: editor.editorContent?.content ?? "",
159-
lines: editor.editorContent?.lines ?? [],
160-
selectedCode: editor.editorContent?.selectedContent ?? "",
161-
selectionRange: editor.editorContent?.selections.first ?? .zero,
162-
lineAnnotations: [],
163-
imports: []
164-
)
165-
)
166-
167-
fileURL = editor.documentURL
168-
projectRootURL = editor.projectURL
169-
selectionRange = codeContext.contextRange
170-
}
171-
}
138+
isAttachedToSelectionRange.toggle()
172139
}
173140
}
174141

0 commit comments

Comments
 (0)