Skip to content

Commit ffb9364

Browse files
committed
Do now allow client to modify whitespace for auto insert edits
1 parent 99db45b commit ffb9364

File tree

3 files changed

+37
-46
lines changed

3 files changed

+37
-46
lines changed

package-lock.json

Lines changed: 8 additions & 38 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@
128128
"@types/semver": "7.3.13",
129129
"@types/tmp": "0.0.33",
130130
"@types/uuid": "^9.0.1",
131-
"@types/vscode": "1.93.0",
131+
"@types/vscode": "1.98.0",
132132
"@types/yauzl": "2.10.0",
133133
"@typescript-eslint/eslint-plugin": "^8.19.0",
134134
"@typescript-eslint/parser": "^8.19.0",
@@ -688,7 +688,7 @@
688688
}
689689
],
690690
"engines": {
691-
"vscode": "^1.93.0"
691+
"vscode": "^1.98.0"
692692
},
693693
"activationEvents": [
694694
"onDebugInitialConfigurations",

src/lsptoolshost/autoInsert/onAutoInsert.ts

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,12 @@
55

66
import * as vscode from 'vscode';
77

8-
import { FormattingOptions, LanguageClient, TextDocumentIdentifier } from 'vscode-languageclient/node';
8+
import {
9+
FormattingOptions,
10+
InsertTextFormat,
11+
LanguageClient,
12+
TextDocumentIdentifier,
13+
} from 'vscode-languageclient/node';
914
import * as RoslynProtocol from '../server/roslynProtocol';
1015
import { RoslynLanguageServer } from '../server/roslynLanguageServer';
1116

@@ -50,7 +55,13 @@ export function registerOnAutoInsert(languageServer: RoslynLanguageServer, langu
5055
}
5156

5257
// Regular expression to match all whitespace characters except the newline character
53-
const changeTrimmed = change.text.replace(/[^\S\n]+/g, '');
58+
let changeTrimmed = change.text.replace(/[^\S\n]+/g, '');
59+
// When hitting enter between braces, we can end up with two new lines added (one to move the cursor down to an empty line,
60+
// and another to move the close brace to a new line below that). We still want to trigger on auto insert here, so remove
61+
// duplicates before we check if the change matches a trigger character.
62+
if (changeTrimmed.length > 1) {
63+
changeTrimmed = [...new Set(changeTrimmed)].join('');
64+
}
5465

5566
if (!vsTriggerCharacters.includes(changeTrimmed)) {
5667
return;
@@ -98,12 +109,22 @@ async function applyAutoInsertEdit(
98109
const textEdit = response._vs_textEdit;
99110
const startPosition = new vscode.Position(textEdit.range.start.line, textEdit.range.start.character);
100111
const endPosition = new vscode.Position(textEdit.range.end.line, textEdit.range.end.character);
101-
const docComment = new vscode.SnippetString(textEdit.newText);
102-
const code: any = vscode;
103-
const textEdits = [new code.SnippetTextEdit(new vscode.Range(startPosition, endPosition), docComment)];
112+
113+
//const editor = vscode.window.activeTextEditor?.insertSnippet();
114+
115+
let textEdits: (vscode.TextEdit | vscode.SnippetTextEdit)[] = [];
116+
if (response._vs_textEditFormat === InsertTextFormat.Snippet) {
117+
const docComment = new vscode.SnippetString(textEdit.newText);
118+
const edit = vscode.SnippetTextEdit.replace(new vscode.Range(startPosition, endPosition), docComment);
119+
// Roslyn already formats the snippet correctly - we don't want the client to try and change the whitespace.
120+
edit.keepWhitespace = true;
121+
textEdits = [edit];
122+
} else {
123+
textEdits = [vscode.TextEdit.replace(new vscode.Range(startPosition, endPosition), textEdit.newText)];
124+
}
125+
104126
const edit = new vscode.WorkspaceEdit();
105127
edit.set(uri, textEdits);
106-
107128
const applied = vscode.workspace.applyEdit(edit);
108129
if (!applied) {
109130
throw new Error('Tried to apply an edit but an error occurred.');

0 commit comments

Comments
 (0)