@@ -19,6 +19,7 @@ public protocol GitHubCopilotSuggestionServiceType {
1919 func getCompletions(
2020 fileURL: URL ,
2121 content: String ,
22+ originalContent: String ,
2223 cursorPosition: CursorPosition ,
2324 tabSize: Int ,
2425 indentSize: Int ,
@@ -357,6 +358,7 @@ public final class GitHubCopilotService: GitHubCopilotBaseService,
357358 public func getCompletions(
358359 fileURL: URL ,
359360 content: String ,
361+ originalContent: String ,
360362 cursorPosition: CursorPosition ,
361363 tabSize: Int ,
362364 indentSize: Int ,
@@ -384,30 +386,51 @@ public final class GitHubCopilotService: GitHubCopilotBaseService,
384386 await localProcessServer? . cancelOngoingTasks ( )
385387
386388 let task = Task {
387- let completions = try await server
388- . sendRequest ( GitHubCopilotRequest . GetCompletionsCycling ( doc: . init(
389- source: content,
390- tabSize: tabSize,
391- indentSize: indentSize,
392- insertSpaces: !usesTabsForIndentation,
393- path: fileURL. path,
394- uri: fileURL. path,
395- relativePath: relativePath,
396- languageId: languageId,
397- position: cursorPosition
398- ) ) )
399- . completions
400- . map {
401- let suggestion = CodeSuggestion (
402- id: $0. uuid,
403- text: $0. text,
404- position: $0. position,
405- range: $0. range
406- )
407- return suggestion
389+ // since when the language server is no longer using the passed in content to generate
390+ // suggestions, therefore, we will need to update the content to the file before we
391+ // do any request.
392+ //
393+ // And sometimes the language server's content was not up to date and may generate
394+ // weird result when the cursor position exceeds the line.
395+ try ? await notifyChangeTextDocument ( fileURL: fileURL, content: content)
396+ defer {
397+ // recover the content.
398+ Task {
399+ try ? await notifyChangeTextDocument ( fileURL: fileURL, content: originalContent)
408400 }
401+ }
409402 try Task . checkCancellation ( )
410- return completions
403+ do {
404+ let completions = try await server
405+ . sendRequest ( GitHubCopilotRequest . InlineCompletion ( doc: . init(
406+ source: content,
407+ tabSize: tabSize,
408+ indentSize: indentSize,
409+ insertSpaces: !usesTabsForIndentation,
410+ path: fileURL. path,
411+ uri: fileURL. path,
412+ relativePath: relativePath,
413+ languageId: languageId,
414+ position: cursorPosition
415+ ) ) )
416+ . items
417+ . compactMap { ( item: _ ) -> CodeSuggestion ? in
418+ guard let range = item. range else { return nil }
419+ let suggestion = CodeSuggestion (
420+ id: item. command? . arguments? . first ?? UUID ( ) . uuidString,
421+ text: item. insertText,
422+ position: cursorPosition,
423+ range: . init( start: range. start, end: range. end)
424+ )
425+ return suggestion
426+ }
427+ try Task . checkCancellation ( )
428+ return completions
429+ } catch let error as ServerError {
430+ throw GitHubCopilotError . languageServerError ( error)
431+ } catch {
432+ throw error
433+ }
411434 }
412435
413436 ongoingTasks. insert ( task)
0 commit comments