Skip to content

Commit dd85854

Browse files
committed
adjust and add integration test
1 parent 11bb070 commit dd85854

File tree

2 files changed

+42
-3
lines changed

2 files changed

+42
-3
lines changed

src/lsptoolshost/autoInsert/onAutoInsert.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,11 @@ export function registerOnAutoInsert(languageServer: RoslynLanguageServer, langu
5656

5757
// Regular expression to match all whitespace characters except the newline character
5858
let changeTrimmed = change.text.replace(/[^\S\n]+/g, '');
59+
5960
// 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) {
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() === '') {
6364
changeTrimmed = [...new Set(changeTrimmed)].join('');
6465
}
6566

test/lsptoolshost/integrationTests/onAutoInsert.integration.test.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
waitForExpectedResult,
1616
} from './integrationHelpers';
1717
import { describe, beforeAll, beforeEach, afterAll, test, expect, afterEach } from '@jest/globals';
18+
import { EOL } from 'os';
1819

1920
describe(`OnAutoInsert Tests`, () => {
2021
beforeAll(async () => {
@@ -72,6 +73,43 @@ describe(`OnAutoInsert Tests`, () => {
7273
}
7374
);
7475
});
76+
77+
test('Enter inside braces fixes brace lines', async () => {
78+
await vscode.window.activeTextEditor!.edit((editBuilder) => {
79+
editBuilder.insert(new vscode.Position(11, 15), '\n');
80+
});
81+
82+
// OnAutoInsert is triggered by the change event but completes asynchronously, so wait for the buffer to be updated.
83+
84+
const expectedLines = [
85+
'class DocComments',
86+
'{',
87+
' //',
88+
' string M(int param1, string param2)',
89+
' {',
90+
' return null;',
91+
' }',
92+
'',
93+
' /// <summary>',
94+
'',
95+
' /// </summary>',
96+
' void M2()',
97+
' {',
98+
' ',
99+
' }',
100+
'}',
101+
'',
102+
];
103+
104+
await waitForExpectedResult<string | undefined>(
105+
async () => vscode.window.activeTextEditor?.document.getText(),
106+
10000,
107+
100,
108+
(input) => {
109+
expect(input).toBe(expectedLines.join(EOL));
110+
}
111+
);
112+
});
75113
});
76114

77115
function normalizeNewlines(text: string | undefined): string | undefined {

0 commit comments

Comments
 (0)