1- import SuggestionModel
1+ import FocusedCodeFinder
22import Foundation
33import OpenAIService
4+ import SuggestionModel
5+ import XcodeInspector
46
57public final class PromptToCodeService : ObservableObject {
68 var designatedPromptToCodeAPI : PromptToCodeAPI ?
79 var promptToCodeAPI : PromptToCodeAPI {
810 if let designatedPromptToCodeAPI {
911 return designatedPromptToCodeAPI
1012 }
11-
13+
1214 return OpenAIPromptToCodeAPI ( )
1315 }
1416
@@ -70,7 +72,7 @@ public final class PromptToCodeService: ObservableObject {
7072 self . projectRootURL = projectRootURL
7173 self . fileURL = fileURL
7274 self . allCode = allCode
73- self . history = . empty
75+ history = . empty
7476 self . extraSystemPrompt = extraSystemPrompt
7577 self . generateDescriptionRequirement = generateDescriptionRequirement
7678 }
@@ -126,6 +128,48 @@ public final class PromptToCodeService: ObservableObject {
126128 runningAPI? . stopResponding ( )
127129 isResponding = false
128130 }
131+
132+ 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+ }
172+ }
129173}
130174
131175protocol PromptToCodeAPI {
@@ -144,3 +188,4 @@ protocol PromptToCodeAPI {
144188
145189 func stopResponding( )
146190}
191+
0 commit comments