Skip to content

Commit adcc39d

Browse files
Allow custom switch-case indentation in formatting (#53733)
1 parent 33ab6fd commit adcc39d

File tree

8 files changed

+55
-10
lines changed

8 files changed

+55
-10
lines changed

src/server/protocol.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3418,6 +3418,7 @@ export interface FormatCodeSettings extends EditorSettings {
34183418
placeOpenBraceOnNewLineForControlBlocks?: boolean;
34193419
insertSpaceBeforeTypeAnnotation?: boolean;
34203420
semicolons?: SemicolonPreference;
3421+
indentSwitchCase?: boolean;
34213422
}
34223423

34233424
export interface UserPreferences {

src/services/formatting/smartIndenter.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -653,9 +653,6 @@ export namespace SmartIndenter {
653653
case SyntaxKind.TypeLiteral:
654654
case SyntaxKind.MappedType:
655655
case SyntaxKind.TupleType:
656-
case SyntaxKind.CaseBlock:
657-
case SyntaxKind.DefaultClause:
658-
case SyntaxKind.CaseClause:
659656
case SyntaxKind.ParenthesizedExpression:
660657
case SyntaxKind.PropertyAccessExpression:
661658
case SyntaxKind.CallExpression:
@@ -684,7 +681,11 @@ export namespace SmartIndenter {
684681
case SyntaxKind.ExportSpecifier:
685682
case SyntaxKind.ImportSpecifier:
686683
case SyntaxKind.PropertyDeclaration:
684+
case SyntaxKind.CaseClause:
685+
case SyntaxKind.DefaultClause:
687686
return true;
687+
case SyntaxKind.CaseBlock:
688+
return settings.indentSwitchCase ?? true;
688689
case SyntaxKind.VariableDeclaration:
689690
case SyntaxKind.PropertyAssignment:
690691
case SyntaxKind.BinaryExpression:

src/services/types.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1118,6 +1118,7 @@ export interface FormatCodeSettings extends EditorSettings {
11181118
readonly insertSpaceBeforeTypeAnnotation?: boolean;
11191119
readonly indentMultiLineObjectLiteralBeginningOnBlankLine?: boolean;
11201120
readonly semicolons?: SemicolonPreference;
1121+
readonly indentSwitchCase?: boolean;
11211122
}
11221123

11231124
export function getDefaultFormatCodeSettings(newLineCharacter?: string): FormatCodeSettings {
@@ -1142,7 +1143,8 @@ export function getDefaultFormatCodeSettings(newLineCharacter?: string): FormatC
11421143
placeOpenBraceOnNewLineForFunctions: false,
11431144
placeOpenBraceOnNewLineForControlBlocks: false,
11441145
semicolons: SemicolonPreference.Ignore,
1145-
trimTrailingWhitespace: true
1146+
trimTrailingWhitespace: true,
1147+
indentSwitchCase: true
11461148
};
11471149
}
11481150

tests/baselines/reference/api/tsserverlibrary.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2718,6 +2718,7 @@ declare namespace ts {
27182718
placeOpenBraceOnNewLineForControlBlocks?: boolean;
27192719
insertSpaceBeforeTypeAnnotation?: boolean;
27202720
semicolons?: SemicolonPreference;
2721+
indentSwitchCase?: boolean;
27212722
}
27222723
interface UserPreferences {
27232724
readonly disableSuggestions?: boolean;
@@ -10524,6 +10525,7 @@ declare namespace ts {
1052410525
readonly insertSpaceBeforeTypeAnnotation?: boolean;
1052510526
readonly indentMultiLineObjectLiteralBeginningOnBlankLine?: boolean;
1052610527
readonly semicolons?: SemicolonPreference;
10528+
readonly indentSwitchCase?: boolean;
1052710529
}
1052810530
interface DefinitionInfo extends DocumentSpan {
1052910531
kind: ScriptElementKind;

tests/baselines/reference/api/typescript.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6594,6 +6594,7 @@ declare namespace ts {
65946594
readonly insertSpaceBeforeTypeAnnotation?: boolean;
65956595
readonly indentMultiLineObjectLiteralBeginningOnBlankLine?: boolean;
65966596
readonly semicolons?: SemicolonPreference;
6597+
readonly indentSwitchCase?: boolean;
65976598
}
65986599
interface DefinitionInfo extends DocumentSpan {
65996600
kind: ScriptElementKind;

tests/baselines/reference/tsserver/formatSettings/works-when-extends-is-specified-with-a-case-insensitive-file-system.js

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,8 @@ Info seq [hh:mm:ss:mss] request:
7676
"placeOpenBraceOnNewLineForFunctions": false,
7777
"placeOpenBraceOnNewLineForControlBlocks": true,
7878
"semicolons": "ignore",
79-
"trimTrailingWhitespace": true
79+
"trimTrailingWhitespace": true,
80+
"indentSwitchCase": true
8081
}
8182
},
8283
"seq": 2,
@@ -112,7 +113,8 @@ FormatCodeOptions should be global:: /a/b/app.ts:: {
112113
"placeOpenBraceOnNewLineForFunctions": false,
113114
"placeOpenBraceOnNewLineForControlBlocks": true,
114115
"semicolons": "ignore",
115-
"trimTrailingWhitespace": true
116+
"trimTrailingWhitespace": true,
117+
"indentSwitchCase": true
116118
}
117119
Before request
118120

@@ -141,7 +143,8 @@ Info seq [hh:mm:ss:mss] request:
141143
"placeOpenBraceOnNewLineForFunctions": false,
142144
"placeOpenBraceOnNewLineForControlBlocks": false,
143145
"semicolons": "ignore",
144-
"trimTrailingWhitespace": true
146+
"trimTrailingWhitespace": true,
147+
"indentSwitchCase": true
145148
},
146149
"file": "/a/b/app.ts"
147150
},
@@ -178,7 +181,8 @@ FormatCodeOptions should be per file:: /a/b/app.ts:: {
178181
"placeOpenBraceOnNewLineForFunctions": false,
179182
"placeOpenBraceOnNewLineForControlBlocks": false,
180183
"semicolons": "ignore",
181-
"trimTrailingWhitespace": true
184+
"trimTrailingWhitespace": true,
185+
"indentSwitchCase": true
182186
}
183187
Before request
184188

@@ -207,7 +211,8 @@ Info seq [hh:mm:ss:mss] request:
207211
"placeOpenBraceOnNewLineForFunctions": false,
208212
"placeOpenBraceOnNewLineForControlBlocks": false,
209213
"semicolons": "ignore",
210-
"trimTrailingWhitespace": true
214+
"trimTrailingWhitespace": true,
215+
"indentSwitchCase": true
211216
}
212217
},
213218
"seq": 4,
@@ -243,5 +248,6 @@ FormatCodeOptions should be per file:: /a/b/app.ts:: {
243248
"placeOpenBraceOnNewLineForFunctions": false,
244249
"placeOpenBraceOnNewLineForControlBlocks": false,
245250
"semicolons": "ignore",
246-
"trimTrailingWhitespace": true
251+
"trimTrailingWhitespace": true,
252+
"indentSwitchCase": true
247253
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/// <reference path='fourslash.ts'/>
2+
3+
////let foo = 1;
4+
////switch (foo) {
5+
/////*1*/case 0:
6+
/////*2*/break;
7+
/////*3*/default:
8+
/////*4*/break;
9+
////}
10+
11+
format.setOption('indentSwitchCase', true);
12+
format.document();
13+
goTo.marker('1');
14+
verify.indentationIs(4);
15+
goTo.marker('2');
16+
verify.indentationIs(8);
17+
goTo.marker('3');
18+
verify.indentationIs(4);
19+
goTo.marker('4');
20+
verify.indentationIs(8);
21+
22+
format.setOption('indentSwitchCase', false);
23+
format.document();
24+
goTo.marker('1');
25+
verify.indentationIs(0);
26+
goTo.marker('2');
27+
verify.indentationIs(4);
28+
goTo.marker('3');
29+
verify.indentationIs(0);
30+
goTo.marker('4');
31+
verify.indentationIs(4);

tests/cases/fourslash/fourslash.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@ declare namespace FourSlashInterface {
196196
readonly insertSpaceBeforeTypeAnnotation?: boolean;
197197
readonly indentMultiLineObjectLiteralBeginningOnBlankLine?: boolean;
198198
readonly semicolons?: ts.SemicolonPreference;
199+
readonly indentSwitchCase?: boolean;
199200
}
200201
interface Range {
201202
fileName: string;

0 commit comments

Comments
 (0)