Skip to content

Commit cd132df

Browse files
Copilotdibarbet
andcommitted
Fix auto-indent to respect language-specific tabSize configuration
Co-authored-by: dibarbet <5749229+dibarbet@users.noreply.github.com>
1 parent 6adbf48 commit cd132df

File tree

2 files changed

+68
-3
lines changed

2 files changed

+68
-3
lines changed

src/lsptoolshost/autoInsert/onAutoInsert.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ async function applyAutoInsertEdit(
104104
languageServer: RoslynLanguageServer,
105105
token: vscode.CancellationToken
106106
) {
107-
const formattingOptions = getFormattingOptions();
107+
const formattingOptions = getFormattingOptions(uri);
108108
const request: RoslynProtocol.OnAutoInsertParams = {
109109
_vs_textDocument: textDocumentIdentifier,
110110
_vs_position: position,
@@ -142,8 +142,8 @@ async function applyAutoInsertEdit(
142142
}
143143
}
144144

145-
function getFormattingOptions(): FormattingOptions {
146-
const editorConfig = vscode.workspace.getConfiguration('editor');
145+
function getFormattingOptions(resource: vscode.Uri): FormattingOptions {
146+
const editorConfig = vscode.workspace.getConfiguration('editor', resource);
147147
const tabSize = editorConfig.get<number>('tabSize') ?? 4;
148148
const insertSpaces = editorConfig.get<boolean>('insertSpaces') ?? true;
149149
return FormattingOptions.create(tabSize, insertSpaces);

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

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,71 @@ describe(`OnAutoInsert Tests`, () => {
110110
}
111111
);
112112
});
113+
114+
test('Enter inside braces respects language-specific tabSize', async () => {
115+
// Set global tabSize to 2 and C# tabSize to 4
116+
const globalConfig = vscode.workspace.getConfiguration('editor');
117+
const csharpConfig = vscode.workspace.getConfiguration('editor', {
118+
languageId: 'csharp',
119+
uri: vscode.window.activeTextEditor!.document.uri,
120+
});
121+
122+
const originalGlobalTabSize = globalConfig.get('tabSize');
123+
const originalCsharpTabSize = csharpConfig.get('tabSize');
124+
125+
try {
126+
// Configure global tabSize to 2
127+
await globalConfig.update('tabSize', 2, vscode.ConfigurationTarget.Global);
128+
// Configure C# tabSize to 4
129+
await vscode.workspace
130+
.getConfiguration('[csharp]', vscode.window.activeTextEditor!.document.uri)
131+
.update('editor.tabSize', 4, vscode.ConfigurationTarget.Global);
132+
133+
// Wait for configuration to propagate
134+
await sleep(100);
135+
136+
await vscode.window.activeTextEditor!.edit((editBuilder) => {
137+
editBuilder.insert(new vscode.Position(11, 15), '\n');
138+
});
139+
140+
// OnAutoInsert is triggered by the change event but completes asynchronously, so wait for the buffer to be updated.
141+
142+
const expectedLines = [
143+
'class DocComments',
144+
'{',
145+
' //',
146+
' string M(int param1, string param2)',
147+
' {',
148+
' return null;',
149+
' }',
150+
'',
151+
' /// <summary>',
152+
'',
153+
' /// </summary>',
154+
' void M2()',
155+
' {',
156+
' ', // Should be 4 spaces (C# setting), not 2 (global setting)
157+
' }',
158+
'}',
159+
'',
160+
];
161+
162+
await waitForExpectedResult<string | undefined>(
163+
async () => vscode.window.activeTextEditor?.document.getText(),
164+
10000,
165+
100,
166+
(input) => {
167+
expect(input).toBe(expectedLines.join(EOL));
168+
}
169+
);
170+
} finally {
171+
// Restore original settings
172+
await globalConfig.update('tabSize', originalGlobalTabSize, vscode.ConfigurationTarget.Global);
173+
await vscode.workspace
174+
.getConfiguration('[csharp]', vscode.window.activeTextEditor!.document.uri)
175+
.update('editor.tabSize', originalCsharpTabSize, vscode.ConfigurationTarget.Global);
176+
}
177+
});
113178
});
114179

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

0 commit comments

Comments
 (0)