Allow custom switch-case indentation in formatting (#53733)

This commit is contained in:
Donald33 Wang
2023-04-13 08:35:05 -07:00
committed by GitHub
parent 33ab6fd0d5
commit adcc39d2c7
8 changed files with 55 additions and 10 deletions
+1
View File
@@ -3418,6 +3418,7 @@ export interface FormatCodeSettings extends EditorSettings {
placeOpenBraceOnNewLineForControlBlocks?: boolean;
insertSpaceBeforeTypeAnnotation?: boolean;
semicolons?: SemicolonPreference;
indentSwitchCase?: boolean;
}
export interface UserPreferences {
+4 -3
View File
@@ -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:
@@ -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:
+3 -1
View File
@@ -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 {
@@ -1142,7 +1143,8 @@ export function getDefaultFormatCodeSettings(newLineCharacter?: string): FormatC
placeOpenBraceOnNewLineForFunctions: false,
placeOpenBraceOnNewLineForControlBlocks: false,
semicolons: SemicolonPreference.Ignore,
trimTrailingWhitespace: true
trimTrailingWhitespace: true,
indentSwitchCase: true
};
}
+2
View File
@@ -2718,6 +2718,7 @@ declare namespace ts {
placeOpenBraceOnNewLineForControlBlocks?: boolean;
insertSpaceBeforeTypeAnnotation?: boolean;
semicolons?: SemicolonPreference;
indentSwitchCase?: boolean;
}
interface UserPreferences {
readonly disableSuggestions?: boolean;
@@ -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;
+1
View File
@@ -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;
@@ -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,
@@ -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
@@ -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"
},
@@ -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
@@ -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,
@@ -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
}
@@ -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
View File
@@ -196,6 +196,7 @@ declare namespace FourSlashInterface {
readonly insertSpaceBeforeTypeAnnotation?: boolean;
readonly indentMultiLineObjectLiteralBeginningOnBlankLine?: boolean;
readonly semicolons?: ts.SemicolonPreference;
readonly indentSwitchCase?: boolean;
}
interface Range {
fileName: string;