preview_lines #4

Merged
Fabel merged 4 commits from preview_lines into develop 2024-10-09 18:00:32 +00:00
1 changed files with 28 additions and 18 deletions
Showing only changes of commit e02296c5a7 - Show all commits

View File

@ -85,7 +85,6 @@ async function generateCompletion(prompt: string, cancellationToken: vscode.Canc
return response.data.response.replace(/<fim_middle>|<fim_suffix>|<fim_prefix>/g, '').trim();
}
class CompletionManager {
private textEditor: vscode.TextEditor;
private document: vscode.TextDocument;
@ -100,24 +99,27 @@ class CompletionManager {
}
public async showPreview() {
this.completionText = '\n' + this.completionText;
const completionLines = this.completionText.split('\n');
// Calculate lines in the completionText
const completionLines = this.completionText.split('\n').length;
const emptyLine = ''; // Empty line for spacing
const previewLines = [emptyLine, ...completionLines, emptyLine];
// Insert empty lines to make space for completion preview
const edit = new vscode.WorkspaceEdit();
const linePadding = '\n'.repeat(completionLines); // Adjust padding based on lines in completion
const range = new vscode.Range(this.startPosition, this.startPosition);
edit.insert(this.document.uri, this.startPosition, linePadding);
await vscode.workspace.applyEdit(edit);
const previewRanges: vscode.DecorationOptions[] = previewLines.map((line, index) => {
const previewRanges: vscode.DecorationOptions[] = this.completionText.split('\n').map((line, index) => {
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),
new vscode.Position(lineNumber, Number.MAX_VALUE)
new vscode.Position(actualLineNumber, 0),
new vscode.Position(actualLineNumber, 0) // Positions should be zero-length for insertions
),
renderOptions: {
after: {
contentText: line.length > 0 ? ` ${line}` : '',
contentText: ` ${line}`.trim(),
},
},
};
@ -128,18 +130,25 @@ class CompletionManager {
public async acceptCompletion() {
const edit = new vscode.WorkspaceEdit();
const startLine = Math.max(0, this.startPosition.line - 1);
const range = new vscode.Range(
new vscode.Position(startLine, 0),
this.startPosition.translate(0, Number.MAX_VALUE)
// Prepare to insert completion text
const endLine = this.startPosition.line + this.completionText.split('\n').length;
// Replace the preview lines with actual completion text
const rangeToReplace = new vscode.Range(
this.startPosition,
new vscode.Position(endLine, 0) // Position for line end should just reach intended insert
);
edit.replace(this.document.uri, range, this.completionText);
edit.replace(this.document.uri, rangeToReplace, this.completionText);
await vscode.workspace.applyEdit(edit);
this.clearPreview();
}
public clearPreview() {
this.textEditor.setDecorations(previewDecorationType, []);
this.textEditor.setDecorations(previewDecorationType, []); // Clear only the decorations
}
public declineCompletion() {
@ -147,6 +156,7 @@ class CompletionManager {
}
}
async function autocompleteCommand(textEditor: vscode.TextEditor, edit: vscode.TextEditorEdit, ...args: any[]) {
const cancellationTokenSource = new vscode.CancellationTokenSource();
const cancellationToken = cancellationTokenSource.token;