having now switech to use tab to complete completion, but the preview itself is still a mess
This commit is contained in:
parent
ac7afb4b4e
commit
66c88a053b
|
@ -1,6 +1,6 @@
|
||||||
{
|
{
|
||||||
"name": "fabelous-autocoder",
|
"name": "fabelous-autocoder",
|
||||||
"version": "0.1.7",
|
"version": "0.2.49",
|
||||||
"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",
|
||||||
|
|
|
@ -48,7 +48,6 @@ 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,8 +58,6 @@ const previewDecorationType = vscode.window.createTextEditorDecorationType({
|
||||||
rangeBehavior: vscode.DecorationRangeBehavior.ClosedOpen, // Ensure proper handling of multiline decorations
|
rangeBehavior: vscode.DecorationRangeBehavior.ClosedOpen, // Ensure proper handling of multiline decorations
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
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;
|
||||||
|
@ -78,7 +75,7 @@ async function autocompleteCommand(textEditor: vscode.TextEditor, cancellationTo
|
||||||
async (progress, progressCancellationToken) => {
|
async (progress, progressCancellationToken) => {
|
||||||
try {
|
try {
|
||||||
progress.report({ message: "Starting model..." });
|
progress.report({ message: "Starting model..." });
|
||||||
|
|
||||||
let axiosCancelPost: () => void;
|
let axiosCancelPost: () => void;
|
||||||
const axiosCancelToken = new axios.CancelToken((c) => {
|
const axiosCancelToken = new axios.CancelToken((c) => {
|
||||||
axiosCancelPost = () => {
|
axiosCancelPost = () => {
|
||||||
|
@ -106,24 +103,25 @@ async function autocompleteCommand(textEditor: vscode.TextEditor, cancellationTo
|
||||||
'Authorization': apiAuthentication
|
'Authorization': apiAuthentication
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
progress.report({ message: "Generating..." });
|
progress.report({ message: "Generating..." });
|
||||||
|
|
||||||
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
|
// Split the completion text by new lines
|
||||||
const lines = completionText.split('\n');
|
const lines = completionText.split('\n');
|
||||||
|
|
||||||
// Remove context lines and insert the preview
|
// Define the preview start and end lines
|
||||||
const startLine = Math.max(0, position.line - 1); // Start 1 line before the cursor
|
const startLine = Math.max(0, position.line); // Use cursor position directly
|
||||||
const endLine = position.line + 1; // End 1 line after the cursor
|
const endLine = position.line + lines.length;
|
||||||
|
|
||||||
const rangeToReplace = new vscode.Range(
|
const rangeToReplace = new vscode.Range(
|
||||||
new vscode.Position(startLine, 0),
|
new vscode.Position(startLine, 0),
|
||||||
new vscode.Position(endLine, 0)
|
new vscode.Position(endLine, 0)
|
||||||
);
|
);
|
||||||
|
|
||||||
// Apply grayed-out italic styling to the preview
|
// Apply the preview with multi-line support
|
||||||
const previewRanges = lines.map((line: string, index: number) => {
|
const previewRanges = lines.map((line: string, index: number) => {
|
||||||
const linePos = new vscode.Position(startLine + index, 0);
|
const linePos = new vscode.Position(startLine + index, 0);
|
||||||
return {
|
return {
|
||||||
|
@ -138,39 +136,33 @@ async function autocompleteCommand(textEditor: vscode.TextEditor, cancellationTo
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
const previewDecorationType = vscode.window.createTextEditorDecorationType({
|
|
||||||
color: '#888888', // Grayed-out color
|
|
||||||
fontStyle: 'italic', // Italic style
|
|
||||||
});
|
|
||||||
|
|
||||||
// Apply the preview as decoration
|
|
||||||
textEditor.setDecorations(previewDecorationType, previewRanges);
|
textEditor.setDecorations(previewDecorationType, previewRanges);
|
||||||
|
|
||||||
// Flag to ensure we only accept or dismiss once
|
|
||||||
let previewInserted = true;
|
let previewInserted = true;
|
||||||
|
|
||||||
// Event handler to accept or dismiss the preview
|
// Handle preview acceptance or dismissal with Tab and Backspace
|
||||||
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];
|
||||||
|
|
||||||
// Handle Backspace to dismiss the preview
|
// Dismiss preview with Backspace
|
||||||
if (change && change.text === '' && change.rangeLength === 1) {
|
if (change && change.text === '' && change.rangeLength === 1) {
|
||||||
// Remove the decoration preview
|
textEditor.setDecorations(previewDecorationType, []); // Clear preview
|
||||||
textEditor.setDecorations(previewDecorationType, []);
|
|
||||||
disposable.dispose(); // Clean up event listener
|
disposable.dispose(); // Clean up event listener
|
||||||
previewInserted = false;
|
previewInserted = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handle Enter to accept the preview
|
// Accept preview with Tab key
|
||||||
if (change && change.text === '\n' && previewInserted) {
|
if (change && change.text === '\t' && previewInserted) {
|
||||||
// Remove the decoration preview and insert actual completion text
|
textEditor.setDecorations(previewDecorationType, []); // Remove preview decorations
|
||||||
textEditor.setDecorations(previewDecorationType, []); // Remove decorations
|
|
||||||
|
|
||||||
const edit = new vscode.WorkspaceEdit();
|
const edit = new vscode.WorkspaceEdit();
|
||||||
const acceptedText = completionText;
|
|
||||||
edit.replace(document.uri, rangeToReplace, acceptedText); // Insert actual completion
|
// Replace the context with the actual completion
|
||||||
|
const acceptedText = completionText;
|
||||||
|
edit.replace(document.uri, rangeToReplace, acceptedText);
|
||||||
|
|
||||||
await vscode.workspace.applyEdit(edit);
|
await vscode.workspace.applyEdit(edit);
|
||||||
|
await document.save(); // Optionally save after replacing
|
||||||
|
|
||||||
disposable.dispose(); // Clean up event listener
|
disposable.dispose(); // Clean up event listener
|
||||||
previewInserted = false;
|
previewInserted = false;
|
||||||
|
@ -192,11 +184,11 @@ 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:}');
|
||||||
|
|
||||||
if (responsePreview) {
|
if (responsePreview) {
|
||||||
await new Promise(resolve => setTimeout(resolve, responsePreviewDelay * 1000));
|
await new Promise(resolve => setTimeout(resolve, responsePreviewDelay * 1000));
|
||||||
if (cancellationToken.isCancellationRequested) {
|
if (cancellationToken.isCancellationRequested) {
|
||||||
return [ item ];
|
return [item];
|
||||||
}
|
}
|
||||||
|
|
||||||
const context = getContextLines(document, position);
|
const context = getContextLines(document, position);
|
||||||
|
|
Loading…
Reference in New Issue