⚡️ only multiline complete if at start of function body / statement block

This commit is contained in:
Nate Sesti 2024-02-21 09:40:02 -08:00
parent d25e313fec
commit d2891d2a21
3 changed files with 78 additions and 23 deletions

View file

@ -29,11 +29,10 @@ function formatExternalSnippet(
return lines.join("\n");
}
async function getTreePathAtCursor(
async function getAst(
filepath: string,
fileContents: string,
cursorIndex: number
): Promise<Parser.SyntaxNode[] | undefined> {
fileContents: string
): Promise<Parser.Tree | undefined> {
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<Parser.SyntaxNode[] | undefined> {
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<AutocompleteSnippet | undefined>,
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 };
}

View file

@ -15,3 +15,11 @@ Continue is currently in the left sidebar, but we recommend moving it to the rig
<kbd>Cmd/Ctrl</kbd> + <kbd>Shift</kbd> + <kbd>L</kbd> = Quick edit
<kbd>Cmd/Ctrl</kbd> + <kbd>Shift</kbd> + <kbd>R</kbd> = 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)

View file

@ -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, () =>