feat/preview #3
107
src/extension.ts
107
src/extension.ts
|
@ -60,18 +60,6 @@ const previewDecorationType = vscode.window.createTextEditorDecorationType({
|
|||
},
|
||||
textDecoration: 'none; display: none;', // Hide the original text
|
||||
});
|
||||
// Generate extra lines for the preview
|
||||
function generateExtraPreviewLines(document: vscode.TextDocument, position: vscode.Position, numLines: number): string[] {
|
||||
const extraLines = [];
|
||||
const startLine = position.line + 1;
|
||||
const endLine = Math.min(document.lineCount - 1, startLine + numLines - 1);
|
||||
|
||||
for (let i = startLine; i <= endLine; i++) {
|
||||
extraLines.push(document.lineAt(i).text);
|
||||
}
|
||||
|
||||
return extraLines;
|
||||
}
|
||||
|
||||
async function autocompleteCommand(textEditor: vscode.TextEditor, cancellationToken?: vscode.CancellationToken) {
|
||||
const document = textEditor.document;
|
||||
|
@ -102,9 +90,6 @@ async function autocompleteCommand(textEditor: vscode.TextEditor, cancellationTo
|
|||
vscode.workspace.onDidCloseTextDocument(axiosCancelPost);
|
||||
});
|
||||
|
||||
// Increase the number of tokens to predict
|
||||
const extendedNumPredict = numPredict * 2; // Adjust this multiplier as needed
|
||||
|
||||
// Make the API request
|
||||
const response = await axios.post(apiEndpoint, {
|
||||
model: apiModel,
|
||||
|
@ -112,7 +97,7 @@ async function autocompleteCommand(textEditor: vscode.TextEditor, cancellationTo
|
|||
stream: false,
|
||||
raw: true,
|
||||
options: {
|
||||
num_predict: extendedNumPredict,
|
||||
num_predict: numPredict,
|
||||
temperature: apiTemperature,
|
||||
stop: ["<fim_suffix>", "```"]
|
||||
}
|
||||
|
@ -131,12 +116,8 @@ async function autocompleteCommand(textEditor: vscode.TextEditor, cancellationTo
|
|||
// Split the completion text by new lines
|
||||
const newLines = completionText.split('\n');
|
||||
|
||||
// Calculate the number of new lines in the completion
|
||||
const completionLineCount = newLines.length;
|
||||
|
||||
// Ensure we have at least as many new lines as the completion, plus some extra
|
||||
const extraLines = 1; // You can adjust this number
|
||||
const totalNewLines = Math.max(completionLineCount + extraLines, position.line - startLine + 1);
|
||||
// Calculate the number of new lines in the completion, plus 2 extra lines
|
||||
const totalNewLines = newLines.length + 2;
|
||||
|
||||
// Create preview decorations
|
||||
const previewRanges: vscode.DecorationOptions[] = [];
|
||||
|
@ -158,54 +139,54 @@ async function autocompleteCommand(textEditor: vscode.TextEditor, cancellationTo
|
|||
let previewInserted = true;
|
||||
|
||||
// Handle preview acceptance or dismissal
|
||||
const disposable = vscode.workspace.onDidChangeTextDocument(async (event) => {
|
||||
if (event.document.uri.toString() === document.uri.toString()) {
|
||||
const change = event.contentChanges[0];
|
||||
// Handle preview acceptance or dismissal
|
||||
const disposable = vscode.workspace.onDidChangeTextDocument(async (event) => {
|
||||
if (event.document.uri.toString() === document.uri.toString()) {
|
||||
const change = event.contentChanges[0];
|
||||
|
||||
// Dismiss preview with Backspace
|
||||
if (change && change.text === '' && change.rangeLength === 1) {
|
||||
textEditor.setDecorations(previewDecorationType, []);
|
||||
disposable.dispose();
|
||||
previewInserted = false;
|
||||
}
|
||||
|
||||
// Accept preview with Tab key
|
||||
if (change && change.text === '\t' && previewInserted) {
|
||||
textEditor.setDecorations(previewDecorationType, []);
|
||||
const edit = new vscode.WorkspaceEdit();
|
||||
|
||||
// Create the text to insert: completion text plus extra newlines
|
||||
const insertText = completionText + '\n'.repeat(Math.max(0, totalNewLines - completionLineCount));
|
||||
|
||||
// Replace the context with the new text
|
||||
const replaceRange = new vscode.Range(startLine, 0, position.line, 0);
|
||||
edit.replace(document.uri, replaceRange, insertText);
|
||||
|
||||
await vscode.workspace.applyEdit(edit);
|
||||
await document.save(); // Optionally save after inserting
|
||||
|
||||
// Move the cursor to the end of the inserted completion
|
||||
const newPosition = new vscode.Position(startLine + totalNewLines, 0);
|
||||
textEditor.selection = new vscode.Selection(newPosition, newPosition);
|
||||
|
||||
disposable.dispose();
|
||||
previewInserted = false;
|
||||
}
|
||||
// Dismiss preview with Backspace
|
||||
if (change && change.text === '' && change.rangeLength === 1) {
|
||||
textEditor.setDecorations(previewDecorationType, []);
|
||||
disposable.dispose();
|
||||
previewInserted = false;
|
||||
}
|
||||
});
|
||||
|
||||
} catch (err: any) {
|
||||
vscode.window.showErrorMessage(
|
||||
"Fabelous Autocoder encountered an error: " + err.message
|
||||
);
|
||||
console.log(err);
|
||||
}
|
||||
// Accept preview with Tab key
|
||||
if (change && change.text === '\t' && previewInserted) {
|
||||
textEditor.setDecorations(previewDecorationType, []);
|
||||
const edit = new vscode.WorkspaceEdit();
|
||||
|
||||
// Insert the completion text
|
||||
const insertText = '\n'.repeat(1) + completionText + '\n'.repeat(1); // Add 2 extra newlines
|
||||
|
||||
// Replace the entire range from the start of the context to the current position
|
||||
const replaceRange = new vscode.Range(startLine, 0, position.line, position.character);
|
||||
edit.replace(document.uri, replaceRange, insertText);
|
||||
|
||||
await vscode.workspace.applyEdit(edit);
|
||||
await document.save(); // Optionally save after inserting
|
||||
|
||||
// Move the cursor to the end of the inserted completion
|
||||
const newPosition = new vscode.Position(startLine + totalNewLines - 2, 0);
|
||||
textEditor.selection = new vscode.Selection(newPosition, newPosition);
|
||||
|
||||
disposable.dispose();
|
||||
previewInserted = false;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
} catch (err: any) {
|
||||
vscode.window.showErrorMessage(
|
||||
"Fabelous Autocoder encountered an error: " + err.message
|
||||
);
|
||||
console.log(err);
|
||||
}
|
||||
);
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
|
||||
async function provideCompletionItems(document: vscode.TextDocument, position: vscode.Position, cancellationToken: vscode.CancellationToken) {
|
||||
const item = new vscode.CompletionItem("Fabelous autocompletion");
|
||||
item.insertText = new vscode.SnippetString('${1:}');
|
||||
|
|
Loading…
Reference in New Issue