develop #6
|
@ -18,6 +18,7 @@ let config: {
|
|||
};
|
||||
|
||||
let previewDecorationType: vscode.TextEditorDecorationType;
|
||||
let activeCompletionManager: CompletionManager | null = null;
|
||||
|
||||
function updateConfig() {
|
||||
const vsConfig = vscode.workspace.getConfiguration('fabelous-autocoder');
|
||||
|
@ -100,14 +101,13 @@ class CompletionManager {
|
|||
|
||||
public async showPreview() {
|
||||
const completionLines = this.completionText.split('\n');
|
||||
const previewLines = [
|
||||
'', // Empty line before
|
||||
...completionLines,
|
||||
'' // Empty line after
|
||||
];
|
||||
const emptyLine = ''; // Empty line for spacing
|
||||
const previewLines = [emptyLine, ...completionLines, emptyLine];
|
||||
|
||||
const previewRanges: vscode.DecorationOptions[] = previewLines.map((line, index) => {
|
||||
const lineNumber = Math.max(0, this.startPosition.line + index - 1);
|
||||
const actualLineNumber = this.startPosition.line + index;
|
||||
const totalLines = this.textEditor.document.lineCount;
|
||||
const lineNumber = Math.min(totalLines - 1, actualLineNumber);
|
||||
return {
|
||||
range: new vscode.Range(
|
||||
new vscode.Position(lineNumber, 0),
|
||||
|
@ -115,15 +115,15 @@ class CompletionManager {
|
|||
),
|
||||
renderOptions: {
|
||||
after: {
|
||||
contentText: line,
|
||||
}
|
||||
}
|
||||
contentText: line.length > 0 ? ` ${line}` : '',
|
||||
},
|
||||
},
|
||||
};
|
||||
});
|
||||
|
||||
this.textEditor.setDecorations(previewDecorationType, previewRanges);
|
||||
}
|
||||
|
||||
|
||||
public async acceptCompletion() {
|
||||
const edit = new vscode.WorkspaceEdit();
|
||||
const startLine = Math.max(0, this.startPosition.line - 1);
|
||||
|
@ -139,6 +139,7 @@ class CompletionManager {
|
|||
public clearPreview() {
|
||||
this.textEditor.setDecorations(previewDecorationType, []);
|
||||
}
|
||||
|
||||
public declineCompletion() {
|
||||
this.clearPreview();
|
||||
}
|
||||
|
@ -167,6 +168,7 @@ async function autocompleteCommand(textEditor: vscode.TextEditor, edit: vscode.T
|
|||
|
||||
const completionManager = new CompletionManager(textEditor, position, completionText);
|
||||
await completionManager.showPreview();
|
||||
activeCompletionManager = completionManager;
|
||||
|
||||
let isDisposed = false;
|
||||
|
||||
|
@ -174,7 +176,8 @@ async function autocompleteCommand(textEditor: vscode.TextEditor, edit: vscode.T
|
|||
if (!isDisposed) {
|
||||
console.log('Disposing listeners');
|
||||
disposable.dispose();
|
||||
declineDisposable.dispose();
|
||||
typeDisposable.dispose();
|
||||
activeCompletionManager = null;
|
||||
isDisposed = true;
|
||||
}
|
||||
};
|
||||
|
@ -203,7 +206,7 @@ async function autocompleteCommand(textEditor: vscode.TextEditor, edit: vscode.T
|
|||
})
|
||||
);
|
||||
|
||||
const declineDisposable = vscode.commands.registerCommand('type', async (args) => {
|
||||
const typeDisposable = vscode.commands.registerCommand('type', async (args) => {
|
||||
if (args.text === '\b') { // Backspace key
|
||||
console.log('Declining completion');
|
||||
completionManager.declineCompletion();
|
||||
|
@ -219,6 +222,27 @@ async function autocompleteCommand(textEditor: vscode.TextEditor, edit: vscode.T
|
|||
}
|
||||
}
|
||||
|
||||
async function acceptCompletion() {
|
||||
if (activeCompletionManager) {
|
||||
await activeCompletionManager.acceptCompletion();
|
||||
const editor = vscode.window.activeTextEditor;
|
||||
if (editor) {
|
||||
const lastLine = editor.document.lineAt(editor.document.lineCount - 1);
|
||||
const newPosition = new vscode.Position(lastLine.lineNumber, lastLine.text.length);
|
||||
editor.selection = new vscode.Selection(newPosition, newPosition);
|
||||
}
|
||||
activeCompletionManager = null;
|
||||
}
|
||||
}
|
||||
|
||||
async function handleTab() {
|
||||
if (activeCompletionManager) {
|
||||
await acceptCompletion();
|
||||
} else {
|
||||
await vscode.commands.executeCommand('tab');
|
||||
}
|
||||
}
|
||||
|
||||
async function provideCompletionItems(document: vscode.TextDocument, position: vscode.Position, cancellationToken: vscode.CancellationToken) {
|
||||
const item = new vscode.CompletionItem('Fabelous autocompletion');
|
||||
item.insertText = new vscode.SnippetString('${1:}');
|
||||
|
@ -237,7 +261,7 @@ async function provideCompletionItems(document: vscode.TextDocument, position: v
|
|||
const result = await generateCompletion(fimPrompt, cancellationToken);
|
||||
const preview = (result as any).preview;
|
||||
if (preview) {
|
||||
item.detail = preview.split('\n')[0];
|
||||
item.detail = preview.split('\n')[0];
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error fetching preview:', error);
|
||||
|
@ -254,7 +278,6 @@ async function provideCompletionItems(document: vscode.TextDocument, position: v
|
|||
|
||||
return [item];
|
||||
}
|
||||
|
||||
export function activate(context: vscode.ExtensionContext) {
|
||||
updateConfig();
|
||||
createPreviewDecorationType();
|
||||
|
@ -262,8 +285,11 @@ export function activate(context: vscode.ExtensionContext) {
|
|||
context.subscriptions.push(
|
||||
vscode.workspace.onDidChangeConfiguration(updateConfig),
|
||||
vscode.languages.registerCompletionItemProvider('*', { provideCompletionItems }, ...config.completionKeys),
|
||||
vscode.commands.registerTextEditorCommand('fabelous-autocoder.autocomplete', autocompleteCommand)
|
||||
vscode.commands.registerTextEditorCommand('fabelous-autocoder.autocomplete', autocompleteCommand),
|
||||
vscode.commands.registerCommand('fabelous-autocoder.acceptCompletion', acceptCompletion),
|
||||
vscode.commands.registerCommand('fabelous-autocoder.handleTab', handleTab)
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
export function deactivate() {}
|
||||
|
|
Loading…
Reference in New Issue