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