Compare commits
5 Commits
6a953b7a12
...
4c5eb334df
Author | SHA1 | Date |
---|---|---|
Falko Victor Habel | 4c5eb334df | |
Falko Victor Habel | d8e97c2b4e | |
Falko Victor Habel | b6241855fc | |
Falko Victor Habel | ccd3528325 | |
Falko Victor Habel | 77e328fcee |
|
@ -1,6 +1,6 @@
|
||||||
{
|
{
|
||||||
"name": "fabelous-autocoder",
|
"name": "fabelous-autocoder",
|
||||||
"version": "0.2.74",
|
"version": "0.2.0",
|
||||||
"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",
|
||||||
|
@ -111,9 +111,8 @@
|
||||||
"commands": [
|
"commands": [
|
||||||
{
|
{
|
||||||
"command": "fabelous-autocoder.autocomplete",
|
"command": "fabelous-autocoder.autocomplete",
|
||||||
"title": "Fabelous autocompletion"
|
"title": "Fabelous Autocompletion"
|
||||||
}
|
}]
|
||||||
]
|
|
||||||
},
|
},
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"vscode:prepublish": "npm run compile",
|
"vscode:prepublish": "npm run compile",
|
||||||
|
|
120
src/extension.ts
120
src/extension.ts
|
@ -113,59 +113,101 @@ async function autocompleteCommand(textEditor: vscode.TextEditor, cancellationTo
|
||||||
let completionText = response.data.response;
|
let completionText = response.data.response;
|
||||||
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
|
let previewInserted = false;
|
||||||
const newLines = completionText.split('\n');
|
let originalContent: string;
|
||||||
|
let previewStartLine: number;
|
||||||
|
const storeAndInsertPreview = async () => {
|
||||||
|
previewStartLine = startLine;
|
||||||
|
const endLine = document.lineCount - 1;
|
||||||
|
const endCharacter = document.lineAt(endLine).text.length;
|
||||||
|
const fullRange = new vscode.Range(startLine, 0, endLine, endCharacter);
|
||||||
|
originalContent = document.getText(fullRange);
|
||||||
|
|
||||||
// Calculate the number of new lines in the completion, plus 2 extra lines
|
const previewContent = completionText + '\n'.repeat(1);
|
||||||
const totalNewLines = newLines.length + 2;
|
|
||||||
|
|
||||||
// Create preview decorations and insert new lines
|
|
||||||
const previewRanges: vscode.DecorationOptions[] = [];
|
|
||||||
const edit = new vscode.WorkspaceEdit();
|
const edit = new vscode.WorkspaceEdit();
|
||||||
const document = textEditor.document;
|
edit.replace(document.uri, fullRange, previewContent); // Overwrite the content
|
||||||
|
await vscode.workspace.applyEdit(edit);
|
||||||
|
|
||||||
for (let i = 0; i < totalNewLines; i++) {
|
// Split the preview content into lines
|
||||||
const lineContent = i < newLines.length ? newLines[i] : '';
|
const previewLines = previewContent.split('\n');
|
||||||
const position = new vscode.Position(startLine + i, 0);
|
|
||||||
|
|
||||||
// Insert a new line
|
// Calculate the exact position of the last character of the preview content
|
||||||
edit.insert(document.uri, position, '\n');
|
const lastPreviewLineIndex = previewStartLine + previewLines.length - 1;
|
||||||
|
const lastPreviewLine = previewLines[previewLines.length - 2]; // Second to last line (last line is newline)
|
||||||
|
const newPosition = new vscode.Position(lastPreviewLineIndex - 1, lastPreviewLine.length); // Place cursor at end of last line
|
||||||
|
|
||||||
// Create a range for the newly inserted line
|
// Set the new cursor position after inserting preview
|
||||||
const range = new vscode.Range(position, position.translate(1, 0));
|
textEditor.selection = new vscode.Selection(newPosition, newPosition);
|
||||||
|
|
||||||
|
// Explicitly scroll to reveal the cursor position at the end of the preview
|
||||||
|
textEditor.revealRange(new vscode.Range(newPosition, newPosition), vscode.TextEditorRevealType.InCenter);
|
||||||
|
|
||||||
|
// Set decorations on the newly inserted lines
|
||||||
|
const previewRanges: vscode.DecorationOptions[] = [];
|
||||||
|
for (let i = 0; i < previewLines.length; i++) {
|
||||||
|
const range = new vscode.Range(previewStartLine + i, 0, previewStartLine + i, previewLines[i].length);
|
||||||
previewRanges.push({
|
previewRanges.push({
|
||||||
range,
|
range,
|
||||||
renderOptions: {
|
renderOptions: {
|
||||||
after: {
|
after: {
|
||||||
contentText: lineContent,
|
contentText: previewLines[i],
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// Apply the edit to insert new lines
|
|
||||||
await vscode.workspace.applyEdit(edit);
|
|
||||||
|
|
||||||
// Set decorations on the newly inserted lines
|
|
||||||
textEditor.setDecorations(previewDecorationType, previewRanges);
|
textEditor.setDecorations(previewDecorationType, previewRanges);
|
||||||
|
previewInserted = true;
|
||||||
|
};
|
||||||
|
|
||||||
let previewInserted = true;
|
|
||||||
|
|
||||||
// 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()) {
|
const textEditor = vscode.window.activeTextEditor;
|
||||||
const change = event.contentChanges[0];
|
if (!textEditor || event.document.uri.toString() !== textEditor.document.uri.toString() || !previewInserted) {
|
||||||
|
return;
|
||||||
// Dismiss preview with Backspace
|
|
||||||
if (change && change.text === '' && change.rangeLength === 1) {
|
|
||||||
textEditor.setDecorations(previewDecorationType, []);
|
|
||||||
disposable.dispose();
|
|
||||||
previewInserted = false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Accept preview with Tab key
|
// Determine tab size setting
|
||||||
if (change && change.text === '\t' && previewInserted) {
|
const tabSize = textEditor.options.tabSize as number || 4;
|
||||||
|
|
||||||
|
for (const change of event.contentChanges) {
|
||||||
|
const changeStartLine = change.range.start.line;
|
||||||
|
|
||||||
|
// Detect Backspace by a single-character deletion
|
||||||
|
if (change.text === '' && change.rangeLength === 1 && changeStartLine >= previewStartLine) {
|
||||||
|
await restoreOriginalContent();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Detect Tab by checking if the change is a multiple of spaces equal to the tab size
|
||||||
|
if (/^[ ]+$/.test(change.text) && change.text.length === tabSize && changeStartLine >= previewStartLine) {
|
||||||
|
await acceptPreview(textEditor, document, startLine, position, completionText);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
const restoreOriginalContent = async () => {
|
||||||
|
if (!previewInserted) return;
|
||||||
|
|
||||||
|
const endLine = document.lineCount - 1;
|
||||||
|
const fullRange = new vscode.Range(previewStartLine, 0, endLine, document.lineAt(endLine).text.length);
|
||||||
|
const edit = new vscode.WorkspaceEdit();
|
||||||
|
|
||||||
|
edit.replace(document.uri, fullRange, originalContent);
|
||||||
|
await vscode.workspace.applyEdit(edit);
|
||||||
|
|
||||||
|
textEditor.setDecorations(previewDecorationType, []);
|
||||||
|
previewInserted = false;
|
||||||
|
disposable.dispose();
|
||||||
|
|
||||||
|
// Move cursor back to the end of the original content
|
||||||
|
const lastOriginalLineNumber = previewStartLine + originalContent.split('\n').length - 1;
|
||||||
|
const lastLineLength = originalContent.split('\n').pop()?.length || 0;
|
||||||
|
const newPosition = new vscode.Position(lastOriginalLineNumber, lastLineLength);
|
||||||
|
textEditor.selection = new vscode.Selection(newPosition, newPosition);
|
||||||
|
};
|
||||||
|
|
||||||
|
// Modify the acceptPreview function to match the working version
|
||||||
|
const acceptPreview = async (textEditor: vscode.TextEditor, document: vscode.TextDocument, startLine: number, position: vscode.Position, completionText: string) => {
|
||||||
|
console.log("Accepting preview");
|
||||||
textEditor.setDecorations(previewDecorationType, []);
|
textEditor.setDecorations(previewDecorationType, []);
|
||||||
const edit = new vscode.WorkspaceEdit();
|
const edit = new vscode.WorkspaceEdit();
|
||||||
|
|
||||||
|
@ -180,14 +222,17 @@ async function autocompleteCommand(textEditor: vscode.TextEditor, cancellationTo
|
||||||
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 totalNewLines = insertText.split('\n').length;
|
||||||
const newPosition = new vscode.Position(startLine + totalNewLines - 2, 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();
|
||||||
previewInserted = false;
|
previewInserted = false;
|
||||||
}
|
console.log("Preview accepted");
|
||||||
}
|
};
|
||||||
});
|
|
||||||
|
// Call this function to initiate the preview
|
||||||
|
await storeAndInsertPreview();
|
||||||
|
|
||||||
} catch (err: any) {
|
} catch (err: any) {
|
||||||
vscode.window.showErrorMessage(
|
vscode.window.showErrorMessage(
|
||||||
|
@ -200,6 +245,7 @@ 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:}');
|
||||||
|
|
Loading…
Reference in New Issue