so preview and complition is technially working, but it overwrites in preview mode the current code

This commit is contained in:
Falko Victor Habel 2024-10-02 15:40:11 +03:00
parent 8e72d08d53
commit 9109c199e6
2 changed files with 42 additions and 21 deletions

View File

@ -1,6 +1,6 @@
{
"name": "fabelous-autocoder",
"version": "0.2.6",
"version": "0.2.61",
"displayName": "Fabelous Autocoder",
"description": "A simple to use Ollama autocompletion Plugin",
"icon": "icon.png",

View File

@ -48,6 +48,7 @@ function getContextLines(document: vscode.TextDocument, position: vscode.Positio
return lines.join("\n");
}
function createFIMPrompt(prefix: string, language: string): string {
return `<fim_prefix>${prefix}<fim_middle><fim_suffix>${language}\n`;
}
@ -59,14 +60,26 @@ 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;
const position = textEditor.selection.active;
// Get the context and create the FIM prompt
const contextLines = 2;
const startLine = Math.max(0, position.line - contextLines);
const context = getContextLines(document, position);
const fimPrompt = createFIMPrompt(context, document.languageId);
vscode.window.withProgress(
@ -89,6 +102,9 @@ 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,
@ -96,7 +112,7 @@ async function autocompleteCommand(textEditor: vscode.TextEditor, cancellationTo
stream: false,
raw: true,
options: {
num_predict: numPredict,
num_predict: extendedNumPredict,
temperature: apiTemperature,
stop: ["<fim_suffix>", "```"]
}
@ -113,24 +129,25 @@ async function autocompleteCommand(textEditor: vscode.TextEditor, cancellationTo
completionText = completionText.replace(/<fim_middle>|<fim_suffix>|<fim_prefix>/g, '').trim();
// Split the completion text by new lines
const lines = completionText.split('\n');
const newLines = completionText.split('\n');
// Define the start line based on the current position
const startLine = position.line;
const endLine = Math.min(startLine + lines.length, document.lineCount);
// Calculate the number of new lines in the completion
const completionLineCount = newLines.length;
// Apply the preview, replacing existing content
// 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
const previewRanges: vscode.DecorationOptions[] = [];
for (let i = 0; i < lines.length; i++) {
const currentLine = startLine + i;
if (currentLine >= document.lineCount) break;
const range = document.lineAt(currentLine).range;
for (let i = 0; i < totalNewLines; i++) {
const lineContent = i < newLines.length ? newLines[i] : '';
const range = new vscode.Range(startLine + i, 0, startLine + i + 1, 0);
previewRanges.push({
range,
renderOptions: {
after: {
contentText: lines[i],
contentText: lineContent,
}
}
});
@ -157,15 +174,19 @@ async function autocompleteCommand(textEditor: vscode.TextEditor, cancellationTo
textEditor.setDecorations(previewDecorationType, []);
const edit = new vscode.WorkspaceEdit();
const rangeToReplace = new vscode.Range(
new vscode.Position(startLine, 0),
new vscode.Position(endLine, document.lineAt(endLine - 1).range.end.character)
);
// Create the text to insert: completion text plus extra newlines
const insertText = completionText + '\n'.repeat(Math.max(0, totalNewLines - completionLineCount));
edit.replace(document.uri, rangeToReplace, completionText);
// Replace the context with the new text
const replaceRange = new vscode.Range(startLine, 0, position.line + 1, 0);
edit.replace(document.uri, replaceRange, insertText);
await vscode.workspace.applyEdit(edit);
await document.save(); // Optionally save after replacing
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;