Compare commits
No commits in common. "ac7afb4b4ec7206784a4073c8a342716d735bff8" and "77e0dbc04898ef5b952bd3e65ac6ed1f21182cba" have entirely different histories.
ac7afb4b4e
...
77e0dbc048
105
src/extension.ts
105
src/extension.ts
|
@ -59,13 +59,10 @@ 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;
|
||||||
|
|
||||||
// Get the context and create the FIM prompt
|
|
||||||
const context = getContextLines(document, position);
|
const context = getContextLines(document, position);
|
||||||
const fimPrompt = createFIMPrompt(context, document.languageId);
|
const fimPrompt = createFIMPrompt(context, document.languageId);
|
||||||
|
|
||||||
|
@ -88,8 +85,7 @@ async function autocompleteCommand(textEditor: vscode.TextEditor, cancellationTo
|
||||||
progressCancellationToken.onCancellationRequested(axiosCancelPost);
|
progressCancellationToken.onCancellationRequested(axiosCancelPost);
|
||||||
vscode.workspace.onDidCloseTextDocument(axiosCancelPost);
|
vscode.workspace.onDidCloseTextDocument(axiosCancelPost);
|
||||||
});
|
});
|
||||||
|
|
||||||
// Make the API request
|
|
||||||
const response = await axios.post(apiEndpoint, {
|
const response = await axios.post(apiEndpoint, {
|
||||||
model: apiModel,
|
model: apiModel,
|
||||||
prompt: fimPrompt,
|
prompt: fimPrompt,
|
||||||
|
@ -111,73 +107,79 @@ 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
|
// Split the completion text by new lines
|
||||||
const lines = completionText.split('\n');
|
const lines = completionText.split('\n');
|
||||||
|
|
||||||
// Remove context lines and insert the preview
|
|
||||||
const startLine = Math.max(0, position.line - 1); // Start 1 line before the cursor
|
|
||||||
const endLine = position.line + 1; // End 1 line after the cursor
|
|
||||||
const rangeToReplace = new vscode.Range(
|
|
||||||
new vscode.Position(startLine, 0),
|
|
||||||
new vscode.Position(endLine, 0)
|
|
||||||
);
|
|
||||||
|
|
||||||
// Apply grayed-out italic styling to the preview
|
// Create a decoration for each line of the response
|
||||||
const previewRanges = lines.map((line: string, index: number) => {
|
const previewRanges = lines.map((line: string, idx: number) => {
|
||||||
const linePos = new vscode.Position(startLine + index, 0);
|
// Determine the start and end positions for each line
|
||||||
|
const startPos = new vscode.Position(position.line + idx, 0);
|
||||||
|
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: new vscode.Range(linePos, linePos),
|
range,
|
||||||
renderOptions: {
|
renderOptions: {
|
||||||
before: {
|
before: {
|
||||||
contentText: line,
|
contentText: line,
|
||||||
color: '#888888', // Grayed-out text
|
color: '#888888',
|
||||||
fontStyle: 'italic', // Italic text
|
fontStyle: 'italic',
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
const previewDecorationType = vscode.window.createTextEditorDecorationType({
|
// Apply the decorations for multiline preview
|
||||||
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 completionInserted = false; // Flag to track insertion
|
||||||
let previewInserted = true;
|
|
||||||
|
|
||||||
// Event handler to accept or dismiss the preview
|
|
||||||
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
|
// Handle Backspace to decline the preview
|
||||||
if (change && change.text === '' && change.rangeLength === 1) {
|
if (change && change.text === '' && change.rangeLength === 1) {
|
||||||
// Remove the decoration preview
|
textEditor.setDecorations(previewDecorationType, []); // Remove preview decorations
|
||||||
textEditor.setDecorations(previewDecorationType, []);
|
disposable.dispose();
|
||||||
disposable.dispose(); // Clean up event listener
|
|
||||||
previewInserted = false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handle Enter to accept the preview
|
// Handle Ctrl + Enter (or Cmd + Enter on macOS) to accept the preview
|
||||||
if (change && change.text === '\n' && previewInserted) {
|
const isCtrlOrCmdPressed = event.contentChanges.some(
|
||||||
// Remove the decoration preview and insert actual completion text
|
(change) => {
|
||||||
textEditor.setDecorations(previewDecorationType, []); // Remove decorations
|
const isMac = process.platform === 'darwin';
|
||||||
|
const isCtrlOrCmd = isMac ? change.text.includes('\u0010') : change.text.includes('\n');
|
||||||
|
return isCtrlOrCmd;
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
if (isCtrlOrCmdPressed && !completionInserted) {
|
||||||
|
// Ensure that we insert the completion text only once
|
||||||
|
completionInserted = true;
|
||||||
|
|
||||||
|
// Remove the preview decoration before applying the final completion
|
||||||
|
textEditor.setDecorations(previewDecorationType, []);
|
||||||
|
|
||||||
const edit = new vscode.WorkspaceEdit();
|
const edit = new vscode.WorkspaceEdit();
|
||||||
const acceptedText = completionText;
|
const insertPosition = new vscode.Position(position.line, 0);
|
||||||
edit.replace(document.uri, rangeToReplace, acceptedText); // Insert actual completion
|
|
||||||
await vscode.workspace.applyEdit(edit);
|
// Avoid duplicating the completion text
|
||||||
|
if (!document.getText().includes(completionText)) {
|
||||||
disposable.dispose(); // Clean up event listener
|
edit.insert(document.uri, insertPosition, '\n' + completionText);
|
||||||
previewInserted = false;
|
await vscode.workspace.applyEdit(edit);
|
||||||
|
}
|
||||||
|
|
||||||
|
const newPosition = new vscode.Position(position.line + lines.length, lines[lines.length - 1].length);
|
||||||
|
textEditor.selection = new vscode.Selection(newPosition, newPosition);
|
||||||
|
|
||||||
|
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
|
||||||
|
@ -186,9 +188,12 @@ 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