feat/preview #3

Merged
Fabel merged 25 commits from feat/preview into develop 2024-10-09 12:27:58 +00:00
1 changed files with 44 additions and 63 deletions
Showing only changes of commit ffd22f7cc6 - Show all commits

View File

@ -60,18 +60,6 @@ const previewDecorationType = vscode.window.createTextEditorDecorationType({
}, },
textDecoration: 'none; display: none;', // Hide the original text 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) { async function autocompleteCommand(textEditor: vscode.TextEditor, cancellationToken?: vscode.CancellationToken) {
const document = textEditor.document; const document = textEditor.document;
@ -102,9 +90,6 @@ async function autocompleteCommand(textEditor: vscode.TextEditor, cancellationTo
vscode.workspace.onDidCloseTextDocument(axiosCancelPost); vscode.workspace.onDidCloseTextDocument(axiosCancelPost);
}); });
// Increase the number of tokens to predict
const extendedNumPredict = numPredict * 2; // Adjust this multiplier as needed
// Make the API request // Make the API request
const response = await axios.post(apiEndpoint, { const response = await axios.post(apiEndpoint, {
model: apiModel, model: apiModel,
@ -112,7 +97,7 @@ async function autocompleteCommand(textEditor: vscode.TextEditor, cancellationTo
stream: false, stream: false,
raw: true, raw: true,
options: { options: {
num_predict: extendedNumPredict, num_predict: numPredict,
temperature: apiTemperature, temperature: apiTemperature,
stop: ["<fim_suffix>", "```"] stop: ["<fim_suffix>", "```"]
} }
@ -131,12 +116,8 @@ async function autocompleteCommand(textEditor: vscode.TextEditor, cancellationTo
// Split the completion text by new lines // Split the completion text by new lines
const newLines = completionText.split('\n'); const newLines = completionText.split('\n');
// Calculate the number of new lines in the completion // Calculate the number of new lines in the completion, plus 2 extra lines
const completionLineCount = newLines.length; const totalNewLines = newLines.length + 2;
// 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);
// Create preview decorations // Create preview decorations
const previewRanges: vscode.DecorationOptions[] = []; const previewRanges: vscode.DecorationOptions[] = [];
@ -158,6 +139,7 @@ async function autocompleteCommand(textEditor: vscode.TextEditor, cancellationTo
let previewInserted = true; let previewInserted = true;
// Handle preview acceptance or dismissal // Handle preview acceptance or dismissal
// Handle preview acceptance or dismissal
const disposable = vscode.workspace.onDidChangeTextDocument(async (event) => { const disposable = vscode.workspace.onDidChangeTextDocument(async (event) => {
if (event.document.uri.toString() === document.uri.toString()) { if (event.document.uri.toString() === document.uri.toString()) {
const change = event.contentChanges[0]; const change = event.contentChanges[0];
@ -174,18 +156,18 @@ async function autocompleteCommand(textEditor: vscode.TextEditor, cancellationTo
textEditor.setDecorations(previewDecorationType, []); textEditor.setDecorations(previewDecorationType, []);
const edit = new vscode.WorkspaceEdit(); const edit = new vscode.WorkspaceEdit();
// Create the text to insert: completion text plus extra newlines // Insert the completion text
const insertText = completionText + '\n'.repeat(Math.max(0, totalNewLines - completionLineCount)); const insertText = '\n'.repeat(1) + completionText + '\n'.repeat(1); // Add 2 extra newlines
// Replace the context with the new text // Replace the entire range from the start of the context to the current position
const replaceRange = new vscode.Range(startLine, 0, position.line, 0); const replaceRange = new vscode.Range(startLine, 0, position.line, position.character);
edit.replace(document.uri, replaceRange, insertText); edit.replace(document.uri, replaceRange, insertText);
await vscode.workspace.applyEdit(edit); await vscode.workspace.applyEdit(edit);
await document.save(); // Optionally save after inserting await document.save(); // Optionally save after inserting
// Move the cursor to the end of the inserted completion // Move the cursor to the end of the inserted completion
const newPosition = new vscode.Position(startLine + totalNewLines, 0); const newPosition = new vscode.Position(startLine + totalNewLines - 2, 0);
textEditor.selection = new vscode.Selection(newPosition, newPosition); textEditor.selection = new vscode.Selection(newPosition, newPosition);
disposable.dispose(); disposable.dispose();
@ -205,7 +187,6 @@ async function autocompleteCommand(textEditor: vscode.TextEditor, cancellationTo
} }
async function provideCompletionItems(document: vscode.TextDocument, position: vscode.Position, cancellationToken: vscode.CancellationToken) { async function provideCompletionItems(document: vscode.TextDocument, position: vscode.Position, cancellationToken: vscode.CancellationToken) {
const item = new vscode.CompletionItem("Fabelous autocompletion"); const item = new vscode.CompletionItem("Fabelous autocompletion");
item.insertText = new vscode.SnippetString('${1:}'); item.insertText = new vscode.SnippetString('${1:}');