@@ -136,61 +136,68 @@ struct WindowBaseCommandHandler: SuggestionCommandHandler {
136136 func acceptSuggestion( editor: EditorContent ) async throws -> UpdatedContent ? {
137137 presenter. markAsProcessing ( true )
138138 defer { presenter. markAsProcessing ( false ) }
139-
139+
140140 let fileURL = try await Environment . fetchCurrentFileURL ( )
141141 let ( workspace, _) = try await Workspace . fetchOrCreateWorkspaceIfNeeded ( fileURL: fileURL)
142142
143- let result : ( suggestion: CopilotCompletion , cleanup: ( ) -> Void ) ? = {
144- if let service = WidgetDataSource . shared. promptToCodes [ fileURL] ? . promptToCodeService {
145- return ( CopilotCompletion (
146- text: service. code,
147- position: service. selectionRange. start,
148- uuid: UUID ( ) . uuidString,
149- range: service. selectionRange,
150- displayText: service. code
151- ) , {
152- WidgetDataSource . shared. removePromptToCode ( for: fileURL)
153- presenter. closePromptToCode ( fileURL: fileURL)
154- } )
155- }
156-
157- if let acceptedSuggestion = workspace. acceptSuggestion (
158- forFileAt: fileURL,
159- editor: editor
160- ) {
161- return ( acceptedSuggestion, {
162- presenter. discardSuggestion ( fileURL: fileURL)
163- } )
164- }
165-
166- return nil
167- } ( )
168-
169- guard let result else { return nil }
170-
171143 let injector = SuggestionInjector ( )
172144 var lines = editor. lines
173145 var cursorPosition = editor. cursorPosition
174146 var extraInfo = SuggestionInjector . ExtraInfo ( )
175- injector. rejectCurrentSuggestions (
176- from: & lines,
177- cursorPosition: & cursorPosition,
178- extraInfo: & extraInfo
179- )
180- injector. acceptSuggestion (
181- intoContentWithoutSuggestion: & lines,
182- cursorPosition: & cursorPosition,
183- completion: result. suggestion,
184- extraInfo: & extraInfo
185- )
186147
187- result. cleanup ( )
188-
189- return . init(
190- content: String ( lines. joined ( separator: " " ) ) ,
191- newCursor: cursorPosition,
192- modifications: extraInfo. modifications
193- )
148+ if let service = WidgetDataSource . shared. promptToCodes [ fileURL] ? . promptToCodeService {
149+ let suggestion = CopilotCompletion (
150+ text: service. code,
151+ position: service. selectionRange. start,
152+ uuid: UUID ( ) . uuidString,
153+ range: service. selectionRange,
154+ displayText: service. code
155+ )
156+
157+ injector. acceptSuggestion (
158+ intoContentWithoutSuggestion: & lines,
159+ cursorPosition: & cursorPosition,
160+ completion: suggestion,
161+ extraInfo: & extraInfo
162+ )
163+
164+ if service. isContinuous {
165+ service. selectionRange = . init(
166+ start: service. selectionRange. start,
167+ end: cursorPosition
168+ )
169+ presenter. presentPromptToCode ( fileURL: fileURL)
170+ } else {
171+ WidgetDataSource . shared. removePromptToCode ( for: fileURL)
172+ presenter. closePromptToCode ( fileURL: fileURL)
173+ }
174+
175+ return . init(
176+ content: String ( lines. joined ( separator: " " ) ) ,
177+ newSelection: . init( start: service. selectionRange. start, end: cursorPosition) ,
178+ modifications: extraInfo. modifications
179+ )
180+ } else if let acceptedSuggestion = workspace. acceptSuggestion (
181+ forFileAt: fileURL,
182+ editor: editor
183+ ) {
184+ injector. acceptSuggestion (
185+ intoContentWithoutSuggestion: & lines,
186+ cursorPosition: & cursorPosition,
187+ completion: acceptedSuggestion,
188+ extraInfo: & extraInfo
189+ )
190+
191+ presenter. discardSuggestion ( fileURL: fileURL)
192+
193+ return . init(
194+ content: String ( lines. joined ( separator: " " ) ) ,
195+ newSelection: . cursor( cursorPosition) ,
196+ modifications: extraInfo. modifications
197+ )
198+ }
199+
200+ return nil
194201 }
195202
196203 func presentRealtimeSuggestions( editor: EditorContent ) async throws -> UpdatedContent ? {
@@ -334,24 +341,30 @@ struct WindowBaseCommandHandler: SuggestionCommandHandler {
334341 presenter. markAsProcessing ( true )
335342 defer { presenter. markAsProcessing ( false ) }
336343 let fileURL = try await Environment . fetchCurrentFileURL ( )
337- let language = UserDefaults . shared. value ( for: \. chatGPTLanguage)
338344 let codeLanguage = languageIdentifierFromFileURL ( fileURL)
339- let code = {
340- guard let selection = editor. selections. last,
341- selection. start != selection. end else { return " " }
342- return editor. selectedCode ( in: selection)
343- } ( )
345+
346+ let ( code, selection) = {
347+ guard var selection = editor. selections. last,
348+ selection. start != selection. end
349+ else { return ( " " , . cursor( editor. cursorPosition) ) }
350+ if selection. start. line != selection. end. line {
351+ // when there are multiple lines start from char 0 so that it can keep the
352+ // indentation.
353+ selection. start = . init( line: selection. start. line, character: 0 )
354+ }
355+ return (
356+ editor. selectedCode ( in: selection) ,
357+ . init(
358+ start: . init( line: selection. start. line, character: selection. start. character) ,
359+ end: . init( line: selection. end. line, character: selection. end. character)
360+ )
361+ )
362+ } ( ) as ( String , CursorRange )
344363
345364 _ = await WidgetDataSource . shared. createPromptToCode (
346365 for: fileURL,
347366 code: code,
348- selectionRange: editor. selections. last. map { . init(
349- start: $0. start,
350- end: $0. end
351- ) } ?? . init(
352- start: editor. cursorPosition,
353- end: editor. cursorPosition
354- ) ,
367+ selectionRange: selection,
355368 language: codeLanguage
356369 )
357370
0 commit comments