From 4d35ec435d817c79b4651fef8ff827d6021fdb53 Mon Sep 17 00:00:00 2001 From: Jacob Kim Date: Fri, 29 Aug 2025 14:35:17 -0700 Subject: [PATCH] docs: add docs --- .../src/autocomplete/completionProvider.ts | 84 ++++++++++++++----- 1 file changed, 63 insertions(+), 21 deletions(-) diff --git a/extensions/vscode/src/autocomplete/completionProvider.ts b/extensions/vscode/src/autocomplete/completionProvider.ts index add7c315b..d9901a3d0 100644 --- a/extensions/vscode/src/autocomplete/completionProvider.ts +++ b/extensions/vscode/src/autocomplete/completionProvider.ts @@ -141,6 +141,12 @@ export class ContinueCompletionProvider this.prefetchQueue.initialize(this.usingFullFileDiff); } + /** + * This is the entry point to the autocomplete and next edit logic. + * @param document The text document containing the current cursor position. + * @param position The current cursor position. + * @param context Contextual information about the inline completion request. + */ public async provideInlineCompletionItems( document: vscode.TextDocument, position: vscode.Position, @@ -154,6 +160,11 @@ export class ContinueCompletionProvider // 2. Jumping (chain exists, jump was taken) // 3. Accepting (chain exists, jump is not taken) + /* START OF CONTEXT GATHERING BOILERPLATE */ + + // The code in this block is meant for gathering context for autocomplete and next edit requests. + // e.g. filepath, cursor position, editor, notebook-ness, etc. + const enableTabAutocomplete = getStatusBarStatus() === StatusBarStatus.Enabled; if (token.isCancellationRequested || !enableTabAutocomplete) { @@ -279,12 +290,44 @@ export class ContinueCompletionProvider recentlyEditedRanges, }; + /* END OF CONTEXT GATHERING BOILERPLATE */ + + /* START OF MODEL OUTCOME RECEIVING BLOCK */ + + // Throughout this function, we choose whether we want an autocomplete or a next edit request. + // The conditional logic below is pretty convoluted, so it would be a good idea to refactor. + // The idea was to re-use as much code as possible, but in hindsight I should've just made different functions. + // this.isNextEditActive is the boolean that determines whether we use autocomplete vs. next edit. + + // You will also see mentions of this.usingFullFileDiff. + // This was initially a flag for whether we should let the model output a full file as an output (or to the best of its abilities). + // It ended up becoming a Mercury Coder vs. non-Mercury Coder flag, and is now used to determine whether we should prefetch next edits. + // This is a good place to refactor on. + // In the future, it will be desirable to have a map where we split models into different next edit requirements and capabilities, + // Then use the model's name to retrieve all of its requirements (or better yet, have a model-specific logic inside children class). + + // Prefetching is also a relic of the past. + // Before, I envisioned that we should call the model in the background to get next next edits. + // Due to subpar results, lack of satisfactory next edit location suggestion algorithms and token cost/latency issues, I scratched the idea. + let outcome: AutocompleteOutcome | NextEditOutcome | undefined; // TODO: We can probably decide here if we want to do the jumping logic. // If we aren't going to jump anyways, then we should be not be using the prefetch queue or the jump manager. // It would simplify the logic quite substantially. + // Here, we introduce the concept of jumping and chains. + // Jump and chain are next edit-specific concepts, and autocomplete has nothing to do with it. + // Next edit is rendered to the users in three modes: + // 1. Default. This is the case where the user either hasn't seen a next edit suggestion, or has rejected the previous one. + // We would render either a ghost text or an SVG with deletion decoration. + + // 2. Jumping. This is the case where the user has just accepted a suggestion, and there are more suggestions in the chain to jump to. + // We would render a Jump label suggesting the user to take the jump or reject. + + // 3. Jumped. This is when the user just accepted the jump. + // We grab the next edit suggestion from the chain and render that instead of calling the model. + // Determine why this method was triggered. const isJumping = this.jumpManager.isJumpInProgress(); const chainExists = this.nextEditProvider.chainExists(); @@ -363,8 +406,8 @@ export class ContinueCompletionProvider currentPosition: jumpPosition, }); - // Don't display anything yet. - // This will be handled in Case 2. + // Don't display anything yet. This will be handled in Case 2. + // Recall from above that provideInlineCompletions runs on every cursor movement. return undefined; } } @@ -378,7 +421,6 @@ export class ContinueCompletionProvider } } else { // Case 1: Typing (chain does not exist). - this.nextEditProvider.startChain(); const input: AutocompleteInput = { @@ -397,15 +439,14 @@ export class ContinueCompletionProvider { withChain: false, usingFullFileDiff: this.usingFullFileDiff }, ); - // Start prefetching next edits. - // NOTE: this might be better off not awaited. + // Start prefetching next edits if not using full file diff. + // NOTE: this is better off not awaited. fire and forget. if (!this.usingFullFileDiff) { this.prefetchQueue.process(ctx); } // If initial outcome is null, suggest a jump instead. - // Calling this method again will call it with - // chain active but jump not suggested yet. + // Calling this method again will call it with chain active but jump not suggested yet. if ( !outcome || (!outcome.completion && outcome.diffLines.length === 0) @@ -465,8 +506,8 @@ export class ContinueCompletionProvider return null; } - // Marking the outcome as displayed saves the current outcome - // as a value of the key completionId. + // Marking the outcome as displayed saves the current outcome as a value of the key completionId. + // NOTE: It seems like autocomplete relies on this to be considered accepted. if (this.isNextEditActive) { this.nextEditProvider.markDisplayed( completionId, @@ -480,6 +521,15 @@ export class ContinueCompletionProvider } this._lastShownCompletion = outcome; + /* END OF MODEL OUTCOME RECEIVING BLOCK */ + + /* START OF RENDERING BLOCK */ + + // Here we determine how to render the outcome received in the previous block. + // We check whether the outcome is a FIM completion or not. + // If FIM, we render a ghost text, and an SVG + deletion decoration if not. + // If we are using autocomplete instead of next edit, we just render a ghost text. + // Construct the range/text to show const startPos = selectedCompletionInfo?.range.start ?? position; let range = new vscode.Range(startPos, startPos); @@ -536,14 +586,6 @@ export class ContinueCompletionProvider const newEditRangeSlice = completionText; // Get the contents of the old (current) editable region. - // const editableRegionStartLine = Math.max( - // currCursorPos.line - NEXT_EDIT_EDITABLE_REGION_TOP_MARGIN, - // 0, - // ); - // const editableRegionEndLine = Math.min( - // currCursorPos.line + NEXT_EDIT_EDITABLE_REGION_BOTTOM_MARGIN, - // editor.document.lineCount - 1, - // ); const editableRegionStartLine = (outcome as NextEditOutcome) .editableRegionStartLine; const editableRegionEndLine = (outcome as NextEditOutcome) @@ -581,10 +623,8 @@ export class ContinueCompletionProvider } // Track this ghost text for acceptance detection. - // Ghost text acceptance can *technically* be acted upon in - // the command handler for "continue.logNextEditOutcomeAccept", - // but there is a substantial delay between accepting and logging, - // which introduces a lot of race conditions with different event handlers. + // Ghost text acceptance can *technically* be acted upon in the command handler for "continue.logNextEditOutcomeAccept". + // However, there is a substantial delay between accepting and logging, which introduces a lot of race conditions with different event handlers. // Plus, separating these concerns seems to make sense logically as well. GhostTextAcceptanceTracker.getInstance().setExpectedGhostTextAcceptance( document, @@ -640,6 +680,8 @@ export class ContinueCompletionProvider } finally { stopStatusBarLoading(); } + + /* END OF RENDERING BLOCK */ } willDisplay(