Skip to content

Allow custom switch-case indentation in formatting #53733

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Apr 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/server/protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3418,6 +3418,7 @@ export interface FormatCodeSettings extends EditorSettings {
placeOpenBraceOnNewLineForControlBlocks?: boolean;
insertSpaceBeforeTypeAnnotation?: boolean;
semicolons?: SemicolonPreference;
indentSwitchCase?: boolean;
}

export interface UserPreferences {
Expand Down
7 changes: 4 additions & 3 deletions src/services/formatting/smartIndenter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -653,9 +653,6 @@ export namespace SmartIndenter {
case SyntaxKind.TypeLiteral:
case SyntaxKind.MappedType:
case SyntaxKind.TupleType:
case SyntaxKind.CaseBlock:
case SyntaxKind.DefaultClause:
case SyntaxKind.CaseClause:
case SyntaxKind.ParenthesizedExpression:
case SyntaxKind.PropertyAccessExpression:
case SyntaxKind.CallExpression:
Expand Down Expand Up @@ -684,7 +681,11 @@ export namespace SmartIndenter {
case SyntaxKind.ExportSpecifier:
case SyntaxKind.ImportSpecifier:
case SyntaxKind.PropertyDeclaration:
case SyntaxKind.CaseClause:
case SyntaxKind.DefaultClause:
return true;
case SyntaxKind.CaseBlock:
return settings.indentSwitchCase ?? true;
case SyntaxKind.VariableDeclaration:
case SyntaxKind.PropertyAssignment:
case SyntaxKind.BinaryExpression:
Expand Down
4 changes: 3 additions & 1 deletion src/services/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1118,6 +1118,7 @@ export interface FormatCodeSettings extends EditorSettings {
readonly insertSpaceBeforeTypeAnnotation?: boolean;
readonly indentMultiLineObjectLiteralBeginningOnBlankLine?: boolean;
readonly semicolons?: SemicolonPreference;
readonly indentSwitchCase?: boolean;
}

export function getDefaultFormatCodeSettings(newLineCharacter?: string): FormatCodeSettings {
Expand All @@ -1142,7 +1143,8 @@ export function getDefaultFormatCodeSettings(newLineCharacter?: string): FormatC
placeOpenBraceOnNewLineForFunctions: false,
placeOpenBraceOnNewLineForControlBlocks: false,
semicolons: SemicolonPreference.Ignore,
trimTrailingWhitespace: true
trimTrailingWhitespace: true,
indentSwitchCase: true
};
}

Expand Down
2 changes: 2 additions & 0 deletions tests/baselines/reference/api/tsserverlibrary.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2718,6 +2718,7 @@ declare namespace ts {
placeOpenBraceOnNewLineForControlBlocks?: boolean;
insertSpaceBeforeTypeAnnotation?: boolean;
semicolons?: SemicolonPreference;
indentSwitchCase?: boolean;
}
interface UserPreferences {
readonly disableSuggestions?: boolean;
Expand Down Expand Up @@ -10524,6 +10525,7 @@ declare namespace ts {
readonly insertSpaceBeforeTypeAnnotation?: boolean;
readonly indentMultiLineObjectLiteralBeginningOnBlankLine?: boolean;
readonly semicolons?: SemicolonPreference;
readonly indentSwitchCase?: boolean;
}
interface DefinitionInfo extends DocumentSpan {
kind: ScriptElementKind;
Expand Down
1 change: 1 addition & 0 deletions tests/baselines/reference/api/typescript.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6594,6 +6594,7 @@ declare namespace ts {
readonly insertSpaceBeforeTypeAnnotation?: boolean;
readonly indentMultiLineObjectLiteralBeginningOnBlankLine?: boolean;
readonly semicolons?: SemicolonPreference;
readonly indentSwitchCase?: boolean;
}
interface DefinitionInfo extends DocumentSpan {
kind: ScriptElementKind;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ Info seq [hh:mm:ss:mss] request:
"placeOpenBraceOnNewLineForFunctions": false,
"placeOpenBraceOnNewLineForControlBlocks": true,
"semicolons": "ignore",
"trimTrailingWhitespace": true
"trimTrailingWhitespace": true,
"indentSwitchCase": true
}
},
"seq": 2,
Expand Down Expand Up @@ -112,7 +113,8 @@ FormatCodeOptions should be global:: /a/b/app.ts:: {
"placeOpenBraceOnNewLineForFunctions": false,
"placeOpenBraceOnNewLineForControlBlocks": true,
"semicolons": "ignore",
"trimTrailingWhitespace": true
"trimTrailingWhitespace": true,
"indentSwitchCase": true
}
Before request

Expand Down Expand Up @@ -141,7 +143,8 @@ Info seq [hh:mm:ss:mss] request:
"placeOpenBraceOnNewLineForFunctions": false,
"placeOpenBraceOnNewLineForControlBlocks": false,
"semicolons": "ignore",
"trimTrailingWhitespace": true
"trimTrailingWhitespace": true,
"indentSwitchCase": true
},
"file": "/a/b/app.ts"
},
Expand Down Expand Up @@ -178,7 +181,8 @@ FormatCodeOptions should be per file:: /a/b/app.ts:: {
"placeOpenBraceOnNewLineForFunctions": false,
"placeOpenBraceOnNewLineForControlBlocks": false,
"semicolons": "ignore",
"trimTrailingWhitespace": true
"trimTrailingWhitespace": true,
"indentSwitchCase": true
}
Before request

Expand Down Expand Up @@ -207,7 +211,8 @@ Info seq [hh:mm:ss:mss] request:
"placeOpenBraceOnNewLineForFunctions": false,
"placeOpenBraceOnNewLineForControlBlocks": false,
"semicolons": "ignore",
"trimTrailingWhitespace": true
"trimTrailingWhitespace": true,
"indentSwitchCase": true
}
},
"seq": 4,
Expand Down Expand Up @@ -243,5 +248,6 @@ FormatCodeOptions should be per file:: /a/b/app.ts:: {
"placeOpenBraceOnNewLineForFunctions": false,
"placeOpenBraceOnNewLineForControlBlocks": false,
"semicolons": "ignore",
"trimTrailingWhitespace": true
"trimTrailingWhitespace": true,
"indentSwitchCase": true
}
31 changes: 31 additions & 0 deletions tests/cases/fourslash/formattingIndentSwitchCase.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/// <reference path='fourslash.ts'/>

////let foo = 1;
////switch (foo) {
/////*1*/case 0:
/////*2*/break;
/////*3*/default:
/////*4*/break;
////}

format.setOption('indentSwitchCase', true);
format.document();
goTo.marker('1');
verify.indentationIs(4);
goTo.marker('2');
verify.indentationIs(8);
goTo.marker('3');
verify.indentationIs(4);
goTo.marker('4');
verify.indentationIs(8);

format.setOption('indentSwitchCase', false);
format.document();
goTo.marker('1');
verify.indentationIs(0);
goTo.marker('2');
verify.indentationIs(4);
goTo.marker('3');
verify.indentationIs(0);
goTo.marker('4');
verify.indentationIs(4);
1 change: 1 addition & 0 deletions tests/cases/fourslash/fourslash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ declare namespace FourSlashInterface {
readonly insertSpaceBeforeTypeAnnotation?: boolean;
readonly indentMultiLineObjectLiteralBeginningOnBlankLine?: boolean;
readonly semicolons?: ts.SemicolonPreference;
readonly indentSwitchCase?: boolean;
}
interface Range {
fileName: string;
Expand Down