|
| 1 | +import { EventEmitter as NodeEventEmitter } from 'stream'; |
| 2 | +import { |
| 3 | + InlineCompletionItem, |
| 4 | + InlineCompletionItemProvider, |
| 5 | + Position, |
| 6 | + Range, |
| 7 | + TextDocument, |
| 8 | + commands, |
| 9 | + window, |
| 10 | +} from 'vscode'; |
| 11 | +import { EXTENSION_DISPLAY_NAME } from '../constants'; |
| 12 | +import completionService from './completion.service'; |
| 13 | + |
| 14 | +interface ICommandInlineCompletionItemProvider extends InlineCompletionItemProvider { |
| 15 | + triggerCompletion(onceCompleted: () => void): void; |
| 16 | +} |
| 17 | + |
| 18 | +/** |
| 19 | + * Trigger {@link ICommandInlineCompletionItemProvider.provideInlineCompletionItems}. |
| 20 | + * Executing editor.action.inlineSuggest.trigger command doesn't trigger inline completion when inlineSuggestionVisible context key is set. |
| 21 | + * Executing editor.action.inlineSuggest.hide before editor.action.inlineSuggest.trigger will make inline completion text bliks. |
| 22 | + * Replacing previous character before trigger seems to do the job. |
| 23 | + */ |
| 24 | +async function triggerInlineCompletionProvider(): Promise<void> { |
| 25 | + const editor = window.activeTextEditor; |
| 26 | + if (!editor) { |
| 27 | + return; |
| 28 | + } |
| 29 | + |
| 30 | + const document = editor.document; |
| 31 | + const activePosition = editor.selection.active; |
| 32 | + const activeOffset = document.offsetAt(activePosition); |
| 33 | + |
| 34 | + if (activeOffset === 0) { |
| 35 | + return; |
| 36 | + } |
| 37 | + |
| 38 | + const prevCharPosition = document.positionAt(activeOffset - 1); |
| 39 | + const replaceRange = new Range(prevCharPosition, activePosition); |
| 40 | + const value = document.getText(replaceRange); |
| 41 | + |
| 42 | + await editor.edit((edit) => edit.replace(replaceRange, value)); |
| 43 | + await commands.executeCommand('editor.action.inlineSuggest.trigger'); |
| 44 | +} |
| 45 | + |
| 46 | +export class StreamingCommandInlineCompletionItemProvider implements ICommandInlineCompletionItemProvider { |
| 47 | + private _isCommandRunning = false; |
| 48 | + |
| 49 | + private readonly _emitter = new NodeEventEmitter().setMaxListeners(1); |
| 50 | + |
| 51 | + private _streamBuffer: string = ''; |
| 52 | + |
| 53 | + private readonly _commandCompletedEvent = 'CommandInlineCompletionItemProvider:completed'; |
| 54 | + |
| 55 | + private _abortController = new AbortController(); |
| 56 | + |
| 57 | + private _beforeComplete(): void { |
| 58 | + this._isCommandRunning = false; |
| 59 | + this._streamBuffer = ''; |
| 60 | + this._abortController.abort(); |
| 61 | + this._abortController = new AbortController(); |
| 62 | + this._emitter.emit(this._commandCompletedEvent); |
| 63 | + } |
| 64 | + |
| 65 | + async triggerCompletion(onceCompleted: () => void) { |
| 66 | + this._emitter.once(this._commandCompletedEvent, onceCompleted); |
| 67 | + |
| 68 | + if (!window.activeTextEditor) { |
| 69 | + void window.showInformationMessage(`Please open a file first to use ${EXTENSION_DISPLAY_NAME}.`); |
| 70 | + this._beforeComplete(); |
| 71 | + return; |
| 72 | + } |
| 73 | + |
| 74 | + if (this._isCommandRunning) { |
| 75 | + return; |
| 76 | + } |
| 77 | + |
| 78 | + this._isCommandRunning = true; |
| 79 | + |
| 80 | + void commands.executeCommand('workbench.action.focusStatusBar'); |
| 81 | + void window.showTextDocument(window.activeTextEditor.document); |
| 82 | + |
| 83 | + await completionService.getCompletionStream( |
| 84 | + window.activeTextEditor.document, |
| 85 | + window.activeTextEditor.selection.active, |
| 86 | + async (chunk) => { |
| 87 | + this._streamBuffer += chunk; |
| 88 | + await triggerInlineCompletionProvider(); |
| 89 | + }, |
| 90 | + this._abortController.signal |
| 91 | + ); |
| 92 | + |
| 93 | + this._isCommandRunning = false; |
| 94 | + await triggerInlineCompletionProvider(); |
| 95 | + } |
| 96 | + |
| 97 | + stopGeneration() { |
| 98 | + this._abortController.abort(); |
| 99 | + } |
| 100 | + |
| 101 | + cancelGeneration() { |
| 102 | + this._beforeComplete(); |
| 103 | + } |
| 104 | + |
| 105 | + provideInlineCompletionItems(document: TextDocument, position: Position) { |
| 106 | + const buffer = this._streamBuffer; |
| 107 | + if (!this._isCommandRunning) { |
| 108 | + this._beforeComplete(); |
| 109 | + } |
| 110 | + |
| 111 | + return [new InlineCompletionItem(`${buffer}`, new Range(position, position.translate(0, 1)))]; |
| 112 | + } |
| 113 | +} |
0 commit comments