Compare commits
No commits in common. "77e0dbc04898ef5b952bd3e65ac6ed1f21182cba" and "0450a222e23b91adb6debd9d5d5f572d3881491b" have entirely different histories.
77e0dbc048
...
0450a222e2
|
@ -75,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 = () => {
|
||||||
|
@ -85,7 +85,7 @@ async function autocompleteCommand(textEditor: vscode.TextEditor, cancellationTo
|
||||||
progressCancellationToken.onCancellationRequested(axiosCancelPost);
|
progressCancellationToken.onCancellationRequested(axiosCancelPost);
|
||||||
vscode.workspace.onDidCloseTextDocument(axiosCancelPost);
|
vscode.workspace.onDidCloseTextDocument(axiosCancelPost);
|
||||||
});
|
});
|
||||||
|
|
||||||
const response = await axios.post(apiEndpoint, {
|
const response = await axios.post(apiEndpoint, {
|
||||||
model: apiModel,
|
model: apiModel,
|
||||||
prompt: fimPrompt,
|
prompt: fimPrompt,
|
||||||
|
@ -102,24 +102,19 @@ 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');
|
||||||
|
|
||||||
// Create a decoration for each line of the response
|
// Create a decoration for each line of the response
|
||||||
const previewRanges = lines.map((line: string, idx: number) => {
|
const previewRanges = lines.map((line: string, idx: number) => {
|
||||||
// Determine the start and end positions for each line
|
const linePos = new vscode.Position(position.line + idx, 0);
|
||||||
const startPos = new vscode.Position(position.line + idx, 0);
|
const range = new vscode.Range(linePos, linePos); // Set range at the start of each new line
|
||||||
const endPos = new vscode.Position(position.line + idx, line.length);
|
|
||||||
|
|
||||||
// Create a range covering the whole line
|
|
||||||
const range = new vscode.Range(startPos, endPos);
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
range,
|
range,
|
||||||
renderOptions: {
|
renderOptions: {
|
||||||
|
@ -134,19 +129,17 @@ async function autocompleteCommand(textEditor: vscode.TextEditor, cancellationTo
|
||||||
|
|
||||||
// Apply the decorations for multiline preview
|
// Apply the decorations for multiline preview
|
||||||
textEditor.setDecorations(previewDecorationType, previewRanges);
|
textEditor.setDecorations(previewDecorationType, previewRanges);
|
||||||
|
|
||||||
let completionInserted = false; // Flag to track insertion
|
|
||||||
|
|
||||||
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 decline the preview
|
// Handle Backspace to decline the preview
|
||||||
if (change && change.text === '' && change.rangeLength === 1) {
|
if (change && change.text === '' && change.rangeLength === 1) {
|
||||||
textEditor.setDecorations(previewDecorationType, []); // Remove preview decorations
|
textEditor.setDecorations(previewDecorationType, []); // Remove preview decorations
|
||||||
disposable.dispose();
|
disposable.dispose();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handle Ctrl + Enter (or Cmd + Enter on macOS) to accept the preview
|
// Handle Ctrl + Enter (or Cmd + Enter on macOS) to accept the preview
|
||||||
const isCtrlOrCmdPressed = event.contentChanges.some(
|
const isCtrlOrCmdPressed = event.contentChanges.some(
|
||||||
(change) => {
|
(change) => {
|
||||||
|
@ -155,31 +148,28 @@ async function autocompleteCommand(textEditor: vscode.TextEditor, cancellationTo
|
||||||
return isCtrlOrCmd;
|
return isCtrlOrCmd;
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
if (isCtrlOrCmdPressed && !completionInserted) {
|
if (isCtrlOrCmdPressed) {
|
||||||
// Ensure that we insert the completion text only once
|
|
||||||
completionInserted = true;
|
|
||||||
|
|
||||||
// Remove the preview decoration before applying the final completion
|
// Remove the preview decoration before applying the final completion
|
||||||
textEditor.setDecorations(previewDecorationType, []);
|
textEditor.setDecorations(previewDecorationType, []);
|
||||||
|
|
||||||
const edit = new vscode.WorkspaceEdit();
|
const edit = new vscode.WorkspaceEdit();
|
||||||
const insertPosition = new vscode.Position(position.line, 0);
|
const insertPosition = new vscode.Position(position.line, 0);
|
||||||
|
|
||||||
// Avoid duplicating the completion text
|
// Insert the completion only once
|
||||||
if (!document.getText().includes(completionText)) {
|
if (!document.getText().includes(completionText)) {
|
||||||
edit.insert(document.uri, insertPosition, '\n' + completionText);
|
edit.insert(document.uri, insertPosition, '\n' + completionText);
|
||||||
await vscode.workspace.applyEdit(edit);
|
await vscode.workspace.applyEdit(edit);
|
||||||
}
|
}
|
||||||
|
|
||||||
const newPosition = new vscode.Position(position.line + lines.length, lines[lines.length - 1].length);
|
const newPosition = new vscode.Position(position.line + lines.length, lines[lines.length - 1].length);
|
||||||
textEditor.selection = new vscode.Selection(newPosition, newPosition);
|
textEditor.selection = new vscode.Selection(newPosition, newPosition);
|
||||||
|
|
||||||
disposable.dispose(); // Clean up the listener after accepting the completion
|
disposable.dispose(); // Clean up the listener after accepting the completion
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
} catch (err: any) {
|
} catch (err: any) {
|
||||||
vscode.window.showErrorMessage(
|
vscode.window.showErrorMessage(
|
||||||
"Fabelous Autocoder encountered an error: " + err.message
|
"Fabelous Autocoder encountered an error: " + err.message
|
||||||
|
@ -188,8 +178,6 @@ async function autocompleteCommand(textEditor: vscode.TextEditor, cancellationTo
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue