Skip to content
This repository has been archived by the owner on May 30, 2024. It is now read-only.

Fix weird indentation for on-type completion #913

Merged
merged 1 commit into from
Nov 23, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 16 additions & 10 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,24 +178,30 @@ export default class Client implements ClientInterface {

const workspaceEdit = new vscode.WorkspaceEdit();
workspaceEdit.set(document.uri, response);
await vscode.workspace.applyEdit(workspaceEdit);

const editor = vscode.window.activeTextEditor!;
const existingText = editor.document.getText(
new vscode.Range(
cursorPosition.range.start,
cursorPosition.range.end,
),
);

// This should happen before applying the edits, otherwise the cursor will be moved to the wrong position
const existingText = editor.document.lineAt(
cursorPosition.range.start.line,
).text;

await vscode.workspace.applyEdit(workspaceEdit);

const indentChar = vscode.window.activeTextEditor?.options
.insertSpaces
? " "
: "\t";

const indentation = indentChar.repeat(
cursorPosition.range.end.character - existingText.length,
);
// If the line is not empty, we don't want to indent the cursor
let indentationLength = 0;

// If the line is empty or only contains whitespace, we want to indent the cursor to the requested position
if (/^\s*$/.exec(existingText)) {
indentationLength = cursorPosition.range.end.character;
}

const indentation = indentChar.repeat(indentationLength);

await vscode.window.activeTextEditor!.insertSnippet(
new vscode.SnippetString(
Expand Down