Compare commits
No commits in common. "4c5eb334dfd9955e1c937c15b45e39563f8c7bfc" and "6a953b7a126770f1229a87e4f6c78d62a37a3de1" have entirely different histories.
4c5eb334df
...
6a953b7a12
|
@ -1,6 +1,6 @@
|
||||||
{
|
{
|
||||||
"name": "fabelous-autocoder",
|
"name": "fabelous-autocoder",
|
||||||
"version": "0.2.0",
|
"version": "0.2.74",
|
||||||
"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",
|
||||||
|
@ -110,9 +110,10 @@
|
||||||
},
|
},
|
||||||
"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",
|
||||||
|
|
178
src/extension.ts
178
src/extension.ts
|
@ -113,139 +113,93 @@ 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();
|
||||||
|
|
||||||
let previewInserted = false;
|
// Split the completion text by new lines
|
||||||
let originalContent: string;
|
const newLines = completionText.split('\n');
|
||||||
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);
|
|
||||||
|
|
||||||
const previewContent = completionText + '\n'.repeat(1);
|
// Calculate the number of new lines in the completion, plus 2 extra lines
|
||||||
const edit = new vscode.WorkspaceEdit();
|
const totalNewLines = newLines.length + 2;
|
||||||
edit.replace(document.uri, fullRange, previewContent); // Overwrite the content
|
|
||||||
await vscode.workspace.applyEdit(edit);
|
|
||||||
|
|
||||||
// Split the preview content into lines
|
// Create preview decorations and insert new lines
|
||||||
const previewLines = previewContent.split('\n');
|
const previewRanges: vscode.DecorationOptions[] = [];
|
||||||
|
const edit = new vscode.WorkspaceEdit();
|
||||||
|
const document = textEditor.document;
|
||||||
|
|
||||||
// Calculate the exact position of the last character of the preview content
|
for (let i = 0; i < totalNewLines; i++) {
|
||||||
const lastPreviewLineIndex = previewStartLine + previewLines.length - 1;
|
const lineContent = i < newLines.length ? newLines[i] : '';
|
||||||
const lastPreviewLine = previewLines[previewLines.length - 2]; // Second to last line (last line is newline)
|
const position = new vscode.Position(startLine + i, 0);
|
||||||
const newPosition = new vscode.Position(lastPreviewLineIndex - 1, lastPreviewLine.length); // Place cursor at end of last line
|
|
||||||
|
|
||||||
// Set the new cursor position after inserting preview
|
// Insert a new line
|
||||||
textEditor.selection = new vscode.Selection(newPosition, newPosition);
|
edit.insert(document.uri, position, '\n');
|
||||||
|
|
||||||
// Explicitly scroll to reveal the cursor position at the end of the preview
|
// Create a range for the newly inserted line
|
||||||
textEditor.revealRange(new vscode.Range(newPosition, newPosition), vscode.TextEditorRevealType.InCenter);
|
const range = new vscode.Range(position, position.translate(1, 0));
|
||||||
|
|
||||||
// Set decorations on the newly inserted lines
|
previewRanges.push({
|
||||||
const previewRanges: vscode.DecorationOptions[] = [];
|
range,
|
||||||
for (let i = 0; i < previewLines.length; i++) {
|
renderOptions: {
|
||||||
const range = new vscode.Range(previewStartLine + i, 0, previewStartLine + i, previewLines[i].length);
|
after: {
|
||||||
previewRanges.push({
|
contentText: lineContent,
|
||||||
range,
|
|
||||||
renderOptions: {
|
|
||||||
after: {
|
|
||||||
contentText: previewLines[i],
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
});
|
|
||||||
}
|
|
||||||
textEditor.setDecorations(previewDecorationType, previewRanges);
|
|
||||||
previewInserted = true;
|
|
||||||
};
|
|
||||||
|
|
||||||
const disposable = vscode.workspace.onDidChangeTextDocument(async (event) => {
|
|
||||||
const textEditor = vscode.window.activeTextEditor;
|
|
||||||
if (!textEditor || event.document.uri.toString() !== textEditor.document.uri.toString() || !previewInserted) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Determine tab size setting
|
|
||||||
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
|
// Apply the edit to insert new lines
|
||||||
if (/^[ ]+$/.test(change.text) && change.text.length === tabSize && changeStartLine >= previewStartLine) {
|
await vscode.workspace.applyEdit(edit);
|
||||||
await acceptPreview(textEditor, document, startLine, position, completionText);
|
|
||||||
}
|
// Set decorations on the newly inserted lines
|
||||||
|
textEditor.setDecorations(previewDecorationType, previewRanges);
|
||||||
|
|
||||||
|
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];
|
||||||
|
|
||||||
|
// 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();
|
||||||
|
|
||||||
const restoreOriginalContent = async () => {
|
// Insert the completion text
|
||||||
if (!previewInserted) return;
|
const insertText = '\n'.repeat(1) + completionText + '\n'.repeat(1); // Add 2 extra newlines
|
||||||
|
|
||||||
const endLine = document.lineCount - 1;
|
// Replace the entire range from the start of the context to the current position
|
||||||
const fullRange = new vscode.Range(previewStartLine, 0, endLine, document.lineAt(endLine).text.length);
|
const replaceRange = new vscode.Range(startLine, 0, position.line, position.character);
|
||||||
const edit = new vscode.WorkspaceEdit();
|
edit.replace(document.uri, replaceRange, insertText);
|
||||||
|
|
||||||
edit.replace(document.uri, fullRange, originalContent);
|
await vscode.workspace.applyEdit(edit);
|
||||||
await vscode.workspace.applyEdit(edit);
|
await document.save(); // Optionally save after inserting
|
||||||
|
|
||||||
textEditor.setDecorations(previewDecorationType, []);
|
// Move the cursor to the end of the inserted completion
|
||||||
previewInserted = false;
|
const newPosition = new vscode.Position(startLine + totalNewLines - 2, 0);
|
||||||
disposable.dispose();
|
textEditor.selection = new vscode.Selection(newPosition, newPosition);
|
||||||
|
|
||||||
// Move cursor back to the end of the original content
|
disposable.dispose();
|
||||||
const lastOriginalLineNumber = previewStartLine + originalContent.split('\n').length - 1;
|
previewInserted = false;
|
||||||
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
|
} catch (err: any) {
|
||||||
const acceptPreview = async (textEditor: vscode.TextEditor, document: vscode.TextDocument, startLine: number, position: vscode.Position, completionText: string) => {
|
vscode.window.showErrorMessage(
|
||||||
console.log("Accepting preview");
|
"Fabelous Autocoder encountered an error: " + err.message
|
||||||
textEditor.setDecorations(previewDecorationType, []);
|
);
|
||||||
const edit = new vscode.WorkspaceEdit();
|
console.log(err);
|
||||||
|
|
||||||
// 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 totalNewLines = insertText.split('\n').length;
|
|
||||||
const newPosition = new vscode.Position(startLine + totalNewLines - 2, 0);
|
|
||||||
textEditor.selection = new vscode.Selection(newPosition, newPosition);
|
|
||||||
|
|
||||||
disposable.dispose();
|
|
||||||
previewInserted = false;
|
|
||||||
console.log("Preview accepted");
|
|
||||||
};
|
|
||||||
|
|
||||||
// Call this function to initiate the preview
|
|
||||||
await storeAndInsertPreview();
|
|
||||||
|
|
||||||
} 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) {
|
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