develop #6

Merged
Fabel merged 35 commits from develop into main 2024-10-09 18:50:26 +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(); return response.data.response.replace(/<fim_middle>|<fim_suffix>|<fim_prefix>/g, '').trim();
} }
class CompletionManager { class CompletionManager {
private textEditor: vscode.TextEditor; private textEditor: vscode.TextEditor;
private document: vscode.TextDocument; private document: vscode.TextDocument;
@ -100,24 +99,27 @@ class CompletionManager {
} }
public async showPreview() { public async showPreview() {
this.completionText = '\n' + this.completionText; // Calculate lines in the completionText
const completionLines = this.completionText.split('\n'); const completionLines = this.completionText.split('\n').length;
const emptyLine = ''; // Empty line for spacing // Insert empty lines to make space for completion preview
const previewLines = [emptyLine, ...completionLines, emptyLine]; 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);
const previewRanges: vscode.DecorationOptions[] = previewLines.map((line, index) => { edit.insert(this.document.uri, this.startPosition, linePadding);
await vscode.workspace.applyEdit(edit);
const previewRanges: vscode.DecorationOptions[] = this.completionText.split('\n').map((line, index) => {
const actualLineNumber = this.startPosition.line + index; 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(actualLineNumber, 0),
new vscode.Position(lineNumber, Number.MAX_VALUE) new vscode.Position(actualLineNumber, 0) // Positions should be zero-length for insertions
), ),
renderOptions: { renderOptions: {
after: { after: {
contentText: line.length > 0 ? ` ${line}` : '', contentText: ` ${line}`.trim(),
}, },
}, },
}; };
@ -128,18 +130,25 @@ class CompletionManager {
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 range = new vscode.Range( // Prepare to insert completion text
new vscode.Position(startLine, 0), const endLine = this.startPosition.line + this.completionText.split('\n').length;
this.startPosition.translate(0, Number.MAX_VALUE)
// 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); await vscode.workspace.applyEdit(edit);
this.clearPreview(); this.clearPreview();
} }
public clearPreview() { public clearPreview() {
this.textEditor.setDecorations(previewDecorationType, []); this.textEditor.setDecorations(previewDecorationType, []); // Clear only the decorations
} }
public declineCompletion() { public declineCompletion() {
@ -147,6 +156,7 @@ class CompletionManager {
} }
} }
async function autocompleteCommand(textEditor: vscode.TextEditor, edit: vscode.TextEditorEdit, ...args: any[]) { async function autocompleteCommand(textEditor: vscode.TextEditor, edit: vscode.TextEditorEdit, ...args: any[]) {
const cancellationTokenSource = new vscode.CancellationTokenSource(); const cancellationTokenSource = new vscode.CancellationTokenSource();
const cancellationToken = cancellationTokenSource.token; const cancellationToken = cancellationTokenSource.token;