From d2891d2a2159e66db401d0cdc51ad19db80c3869 Mon Sep 17 00:00:00 2001 From: Nate Sesti Date: Wed, 21 Feb 2024 09:40:02 -0800 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=EF=B8=8F=20only=20multiline=20complet?= =?UTF-8?q?e=20if=20at=20start=20of=20function=20body=20/=20statement=20bl?= =?UTF-8?q?ock?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/autocomplete/constructPrompt.ts | 72 +++++++++++++++---- extensions/vscode/media/welcome.md | 8 +++ .../src/lang-server/completionProvider.ts | 21 +++--- 3 files changed, 78 insertions(+), 23 deletions(-) diff --git a/core/autocomplete/constructPrompt.ts b/core/autocomplete/constructPrompt.ts index 29e0a38a7..f9f28704c 100644 --- a/core/autocomplete/constructPrompt.ts +++ b/core/autocomplete/constructPrompt.ts @@ -29,11 +29,10 @@ function formatExternalSnippet( return lines.join("\n"); } -async function getTreePathAtCursor( +async function getAst( filepath: string, - fileContents: string, - cursorIndex: number -): Promise { + fileContents: string +): Promise { const parser = await getParserForFile(filepath); if (!parser) { @@ -41,6 +40,13 @@ async function getTreePathAtCursor( } const ast = parser.parse(fileContents); + return ast; +} + +async function getTreePathAtCursor( + ast: Parser.Tree, + cursorIndex: number +): Promise { const path = [ast.rootNode]; while (path[path.length - 1].childCount > 0) { let foundChild = false; @@ -65,6 +71,35 @@ export interface AutocompleteSnippet { content: string; } +const BLOCK_TYPES = ["body", "statement_block"]; + +function shouldCompleteMultiline( + treePath: Parser.SyntaxNode[], + cursorLine: number +): boolean { + // If at the base of the file, do multiline + if (treePath.length === 1) { + return true; + } + + // If at the first line of an otherwise empty funtion body, do multiline + for (let i = treePath.length - 1; i >= 0; i--) { + const node = treePath[i]; + if ( + BLOCK_TYPES.includes(node.type) && + Math.abs(node.startPosition.row - cursorLine) <= 1 + ) { + let text = node.text; + text = text.slice(text.indexOf("{") + 1); + text = text.slice(0, text.lastIndexOf("}")); + text = text.trim(); + return text.split("\n").length === 1; + } + } + + return false; +} + export async function constructAutocompletePrompt( filepath: string, fullPrefix: string, @@ -77,23 +112,30 @@ export async function constructAutocompletePrompt( character: number ) => Promise, options: TabAutocompleteOptions -): Promise<{ prefix: string; suffix: string; useFim: boolean }> { +): Promise<{ + prefix: string; + suffix: string; + useFim: boolean; + completeMultiline: boolean; +}> { // Find external snippets const snippets: AutocompleteSnippet[] = []; - let treePath; + let treePath: Parser.SyntaxNode[] | undefined; try { - treePath = await getTreePathAtCursor( - filepath, - fullPrefix + fullSuffix, - fullPrefix.length - ); + const ast = await getAst(filepath, fullPrefix + fullSuffix); + if (!ast) { + throw new Error(`AST undefined for ${filepath}`); + } + + treePath = await getTreePathAtCursor(ast, fullPrefix.length); } catch (e) { console.error("Failed to parse AST", e); } - // Get function def when inside call expression + let completeMultiline = false; if (treePath) { + // Get function def when inside call expression let callExpression = undefined; for (let node of treePath.reverse()) { if (node.type === "call_expression") { @@ -111,6 +153,10 @@ export async function constructAutocompletePrompt( snippets.push(definition); } } + + // Use AST to determine whether to complete multiline + let cursorLine = fullPrefix.split("\n").length - 1; + completeMultiline = shouldCompleteMultiline(treePath, cursorLine); } // Construct basic prefix / suffix @@ -133,5 +179,5 @@ export async function constructAutocompletePrompt( ); let suffix = pruneLinesFromBottom(fullSuffix, maxSuffixTokens); - return { prefix, suffix, useFim: true }; + return { prefix, suffix, useFim: true, completeMultiline }; } diff --git a/extensions/vscode/media/welcome.md b/extensions/vscode/media/welcome.md index a9e7e381c..f43ed4b37 100644 --- a/extensions/vscode/media/welcome.md +++ b/extensions/vscode/media/welcome.md @@ -15,3 +15,11 @@ Continue is currently in the left sidebar, but we recommend moving it to the rig Cmd/Ctrl + Shift + L = Quick edit Cmd/Ctrl + Shift + R = Automatically debug terminal + +## ⚡️ Setting up tab autocomplete + +1. Download [Ollama](https://ollama.ai) +2. Download starcoder:3b by running `ollama run starcoder:3b` in the terminal +3. Click the `Continue` button in the status bar (bottom right of the VS Code window) to enable autocomplete + +![Autocomplete](./autocomplete.gif) diff --git a/extensions/vscode/src/lang-server/completionProvider.ts b/extensions/vscode/src/lang-server/completionProvider.ts index 515bbb9f2..bf6d22ccc 100644 --- a/extensions/vscode/src/lang-server/completionProvider.ts +++ b/extensions/vscode/src/lang-server/completionProvider.ts @@ -273,15 +273,16 @@ async function getTabCompletion( Math.min(pos.line + 1, document.lineCount - 1) ).text; const clipboardText = await vscode.env.clipboard.readText(); - const { prefix, suffix } = await constructAutocompletePrompt( - document.uri.toString(), - fullPrefix, - fullSuffix, - clipboardText, - lang, - getDefinition, - options - ); + const { prefix, suffix, completeMultiline } = + await constructAutocompletePrompt( + document.uri.toString(), + fullPrefix, + fullSuffix, + clipboardText, + lang, + getDefinition, + options + ); const { template, completionOptions } = options.template ? { template: options.template, completionOptions: {} } @@ -310,7 +311,7 @@ async function getTabCompletion( "```", ...lang.stopWords, ]; - if (options.disableMultiLineCompletions) { + if (options.disableMultiLineCompletions || !completeMultiline) { stop.unshift("\n"); } let generator = GeneratorReuseManager.getGenerator(prefix, () =>