develop #6

Merged
Fabel merged 35 commits from develop into main 2024-10-09 18:50:26 +00:00
2 changed files with 42 additions and 21 deletions
Showing only changes of commit 9109c199e6 - Show all commits

View File

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

View File

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