Skip to content

Commit db3bf84

Browse files
committed
adjust logic
1 parent dd85854 commit db3bf84

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

src/lsptoolshost/autoInsert/onAutoInsert.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
} from 'vscode-languageclient/node';
1414
import * as RoslynProtocol from '../server/roslynProtocol';
1515
import { RoslynLanguageServer } from '../server/roslynLanguageServer';
16+
import { EOL } from 'node:os';
1617

1718
export function registerOnAutoInsert(languageServer: RoslynLanguageServer, languageClient: LanguageClient) {
1819
let source = new vscode.CancellationTokenSource();
@@ -57,11 +58,18 @@ export function registerOnAutoInsert(languageServer: RoslynLanguageServer, langu
5758
// Regular expression to match all whitespace characters except the newline character
5859
let changeTrimmed = change.text.replace(/[^\S\n]+/g, '');
5960

61+
// If the change is empty after removing whitespace, we don't need to process it.
62+
if (changeTrimmed.length === 0) {
63+
return;
64+
}
65+
6066
// When hitting enter between braces, we can end up with two new lines added (one to move the cursor down to an empty line,
61-
// and another to move the close brace to a new line below that). We still want to trigger on auto insert here, so if we
62-
// have a whitespace only edit (meaning its new lines only) with multiple characters, we reduce it to a single newline.
63-
if (changeTrimmed.length > 1 && changeTrimmed.trim() === '') {
64-
changeTrimmed = [...new Set(changeTrimmed)].join('');
67+
// and another to move the close brace to a new line below that). We want to detect that edit as a single new line trigger.
68+
//
69+
// Since we already removed all whitespace except new lines above, we can just trim the string to remove new lines as well
70+
// and check if there is anything left. If not, we know the change is just whitespace and new lines and can set the trigger to the new line character.
71+
if (changeTrimmed.trim() === '') {
72+
changeTrimmed = EOL;
6573
}
6674

6775
if (!vsTriggerCharacters.includes(changeTrimmed)) {

0 commit comments

Comments
 (0)