Skip to content

Commit 9146d9b

Browse files
committed
Add Spacing for Nonempty Brackets Formatting Option
User formatting option to add single space after opening and before closing nonempty brackets.
1 parent f92aa86 commit 9146d9b

8 files changed

Lines changed: 46 additions & 12 deletions

File tree

src/harness/fourslash.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,7 @@ module FourSlash {
364364
InsertSpaceAfterKeywordsInControlFlowStatements: true,
365365
InsertSpaceAfterFunctionKeywordForAnonymousFunctions: false,
366366
InsertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis: false,
367+
InsertSpaceAfterOpeningAndBeforeClosingNonemptyBrackets: false,
367368
PlaceOpenBraceOnNewLineForFunctions: false,
368369
PlaceOpenBraceOnNewLineForControlBlocks: false,
369370
};

src/server/editorServices.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -990,6 +990,7 @@ namespace ts.server {
990990
InsertSpaceAfterKeywordsInControlFlowStatements: true,
991991
InsertSpaceAfterFunctionKeywordForAnonymousFunctions: false,
992992
InsertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis: false,
993+
InsertSpaceAfterOpeningAndBeforeClosingNonemptyBrackets: false,
993994
PlaceOpenBraceOnNewLineForFunctions: false,
994995
PlaceOpenBraceOnNewLineForControlBlocks: false,
995996
}

src/server/protocol.d.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,9 @@ declare namespace ts.server.protocol {
452452

453453
/** Defines space handling after opening and before closing non empty parenthesis. Default value is false. */
454454
insertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis?: boolean;
455+
456+
/** Defines space handling after opening and before closing non empty brackets. Default value is false. */
457+
insertSpaceAfterOpeningAndBeforeClosingNonemptyBrackets?: boolean;
455458

456459
/** Defines whether an open brace is put onto a new line for functions or not. Default value is false. */
457460
placeOpenBraceOnNewLineForFunctions?: boolean;

src/services/formatting/rules.ts

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,12 @@ namespace ts.formatting {
3939
public SpaceBetweenCloseBraceAndWhile: Rule;
4040
public NoSpaceAfterCloseBrace: Rule;
4141

42-
// No space for indexer and dot
42+
// No space for dot
4343
public NoSpaceBeforeDot: Rule;
4444
public NoSpaceAfterDot: Rule;
45+
46+
// No space before and after indexer
4547
public NoSpaceBeforeOpenBracket: Rule;
46-
public NoSpaceAfterOpenBracket: Rule;
47-
public NoSpaceBeforeCloseBracket: Rule;
4848
public NoSpaceAfterCloseBracket: Rule;
4949

5050
// Insert a space after { and before } in single-line contexts, but remove space from empty object literals {}.
@@ -191,6 +191,13 @@ namespace ts.formatting {
191191
public NoSpaceAfterOpenParen: Rule;
192192
public NoSpaceBeforeCloseParen: Rule;
193193

194+
// Insert space after opening and before closing nonempty brackets
195+
public SpaceAfterOpenBracket: Rule;
196+
public SpaceBeforeCloseBracket: Rule;
197+
public NoSpaceBetweenBrackets: Rule;
198+
public NoSpaceAfterOpenBracket: Rule;
199+
public NoSpaceBeforeCloseBracket: Rule;
200+
194201
// Insert space after function keyword for anonymous functions
195202
public SpaceAfterAnonymousFunctionKeyword: Rule;
196203
public NoSpaceAfterAnonymousFunctionKeyword: Rule;
@@ -232,13 +239,13 @@ namespace ts.formatting {
232239
this.SpaceBetweenCloseBraceAndWhile = new Rule(RuleDescriptor.create1(SyntaxKind.CloseBraceToken, SyntaxKind.WhileKeyword), RuleOperation.create2(new RuleOperationContext(Rules.IsSameLineTokenContext), RuleAction.Space));
233240
this.NoSpaceAfterCloseBrace = new Rule(RuleDescriptor.create3(SyntaxKind.CloseBraceToken, Shared.TokenRange.FromTokens([SyntaxKind.CloseParenToken, SyntaxKind.CloseBracketToken, SyntaxKind.CommaToken, SyntaxKind.SemicolonToken])), RuleOperation.create2(new RuleOperationContext(Rules.IsSameLineTokenContext), RuleAction.Delete));
234241

235-
// No space for indexer and dot
236-
this.NoSpaceBeforeDot = new Rule(RuleDescriptor.create2(Shared.TokenRange.Any, SyntaxKind.DotToken), RuleOperation.create2(new RuleOperationContext(Rules.IsSameLineTokenContext), RuleAction.Delete));
237-
this.NoSpaceAfterDot = new Rule(RuleDescriptor.create3(SyntaxKind.DotToken, Shared.TokenRange.Any), RuleOperation.create2(new RuleOperationContext(Rules.IsSameLineTokenContext), RuleAction.Delete));
238-
this.NoSpaceBeforeOpenBracket = new Rule(RuleDescriptor.create2(Shared.TokenRange.Any, SyntaxKind.OpenBracketToken), RuleOperation.create2(new RuleOperationContext(Rules.IsSameLineTokenContext), RuleAction.Delete));
239-
this.NoSpaceAfterOpenBracket = new Rule(RuleDescriptor.create3(SyntaxKind.OpenBracketToken, Shared.TokenRange.Any), RuleOperation.create2(new RuleOperationContext(Rules.IsSameLineTokenContext), RuleAction.Delete));
240-
this.NoSpaceBeforeCloseBracket = new Rule(RuleDescriptor.create2(Shared.TokenRange.Any, SyntaxKind.CloseBracketToken), RuleOperation.create2(new RuleOperationContext(Rules.IsSameLineTokenContext), RuleAction.Delete));
241-
this.NoSpaceAfterCloseBracket = new Rule(RuleDescriptor.create3(SyntaxKind.CloseBracketToken, Shared.TokenRange.Any), RuleOperation.create2(new RuleOperationContext(Rules.IsSameLineTokenContext, Rules.IsNotBeforeBlockInFunctionDeclarationContext), RuleAction.Delete));
242+
// No space for dot
243+
this.NoSpaceBeforeDot = new Rule( RuleDescriptor.create2( Shared.TokenRange.Any, SyntaxKind.DotToken ), RuleOperation.create2( new RuleOperationContext( Rules.IsSameLineTokenContext ), RuleAction.Delete ) );
244+
this.NoSpaceAfterDot = new Rule( RuleDescriptor.create3( SyntaxKind.DotToken, Shared.TokenRange.Any ), RuleOperation.create2( new RuleOperationContext( Rules.IsSameLineTokenContext ), RuleAction.Delete ) );
245+
246+
// No space before and after indexer
247+
this.NoSpaceBeforeOpenBracket = new Rule( RuleDescriptor.create2( Shared.TokenRange.Any, SyntaxKind.OpenBracketToken ), RuleOperation.create2( new RuleOperationContext( Rules.IsSameLineTokenContext ), RuleAction.Delete ) );
248+
this.NoSpaceAfterCloseBracket = new Rule( RuleDescriptor.create3( SyntaxKind.CloseBracketToken, Shared.TokenRange.Any ), RuleOperation.create2( new RuleOperationContext( Rules.IsSameLineTokenContext, Rules.IsNotBeforeBlockInFunctionDeclarationContext ), RuleAction.Delete ) );
242249

243250
// Place a space before open brace in a function declaration
244251
this.FunctionOpenBraceLeftTokenRange = Shared.TokenRange.AnyIncludingMultilineComments;
@@ -405,8 +412,8 @@ namespace ts.formatting {
405412
this.NoSpaceBeforeSemicolon,
406413
this.SpaceBeforeOpenBraceInControl, this.SpaceBeforeOpenBraceInFunction, this.SpaceBeforeOpenBraceInTypeScriptDeclWithBlock,
407414
this.NoSpaceBeforeComma,
408-
this.NoSpaceBeforeOpenBracket, this.NoSpaceAfterOpenBracket,
409-
this.NoSpaceBeforeCloseBracket, this.NoSpaceAfterCloseBracket,
415+
this.NoSpaceBeforeOpenBracket,
416+
this.NoSpaceAfterCloseBracket,
410417
this.SpaceAfterSemicolon,
411418
this.NoSpaceBeforeOpenParenInFuncDecl,
412419
this.SpaceBetweenStatements, this.SpaceAfterTryFinally
@@ -451,6 +458,13 @@ namespace ts.formatting {
451458
this.NoSpaceAfterOpenParen = new Rule(RuleDescriptor.create3(SyntaxKind.OpenParenToken, Shared.TokenRange.Any), RuleOperation.create2(new RuleOperationContext(Rules.IsSameLineTokenContext), RuleAction.Delete));
452459
this.NoSpaceBeforeCloseParen = new Rule(RuleDescriptor.create2(Shared.TokenRange.Any, SyntaxKind.CloseParenToken), RuleOperation.create2(new RuleOperationContext(Rules.IsSameLineTokenContext), RuleAction.Delete));
453460

461+
// Insert space after opening and before closing nonempty brackets
462+
this.SpaceAfterOpenBracket = new Rule( RuleDescriptor.create3( SyntaxKind.OpenBracketToken, Shared.TokenRange.Any ), RuleOperation.create2( new RuleOperationContext( Rules.IsSameLineTokenContext ), RuleAction.Space ) );
463+
this.SpaceBeforeCloseBracket = new Rule( RuleDescriptor.create2( Shared.TokenRange.Any, SyntaxKind.CloseBracketToken ), RuleOperation.create2( new RuleOperationContext( Rules.IsSameLineTokenContext ), RuleAction.Space ) );
464+
this.NoSpaceBetweenBrackets = new Rule( RuleDescriptor.create1( SyntaxKind.OpenBracketToken, SyntaxKind.CloseBracketToken ), RuleOperation.create2( new RuleOperationContext( Rules.IsSameLineTokenContext ), RuleAction.Delete ) );
465+
this.NoSpaceAfterOpenBracket = new Rule( RuleDescriptor.create3( SyntaxKind.OpenBracketToken, Shared.TokenRange.Any ), RuleOperation.create2( new RuleOperationContext( Rules.IsSameLineTokenContext ), RuleAction.Delete ) );
466+
this.NoSpaceBeforeCloseBracket = new Rule( RuleDescriptor.create2( Shared.TokenRange.Any, SyntaxKind.CloseBracketToken ), RuleOperation.create2( new RuleOperationContext( Rules.IsSameLineTokenContext ), RuleAction.Delete ) );
467+
454468
// Insert space after function keyword for anonymous functions
455469
this.SpaceAfterAnonymousFunctionKeyword = new Rule(RuleDescriptor.create1(SyntaxKind.FunctionKeyword, SyntaxKind.OpenParenToken), RuleOperation.create2(new RuleOperationContext(Rules.IsFunctionDeclContext), RuleAction.Space));
456470
this.NoSpaceAfterAnonymousFunctionKeyword = new Rule(RuleDescriptor.create1(SyntaxKind.FunctionKeyword, SyntaxKind.OpenParenToken), RuleOperation.create2(new RuleOperationContext(Rules.IsFunctionDeclContext), RuleAction.Delete));

src/services/formatting/rulesProvider.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,17 @@ namespace ts.formatting {
7171
rules.push(this.globalRules.NoSpaceBetweenParens);
7272
}
7373

74+
if ( options.InsertSpaceAfterOpeningAndBeforeClosingNonemptyBrackets ) {
75+
rules.push( this.globalRules.SpaceAfterOpenBracket );
76+
rules.push( this.globalRules.SpaceBeforeCloseBracket );
77+
rules.push( this.globalRules.NoSpaceBetweenBrackets );
78+
}
79+
else {
80+
rules.push( this.globalRules.NoSpaceAfterOpenBracket );
81+
rules.push( this.globalRules.NoSpaceBeforeCloseBracket );
82+
rules.push( this.globalRules.NoSpaceBetweenBrackets );
83+
}
84+
7485
if (options.InsertSpaceAfterSemicolonInForStatements) {
7586
rules.push(this.globalRules.SpaceAfterSemicolonInFor);
7687
}

src/services/services.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1149,6 +1149,7 @@ namespace ts {
11491149
InsertSpaceAfterKeywordsInControlFlowStatements: boolean;
11501150
InsertSpaceAfterFunctionKeywordForAnonymousFunctions: boolean;
11511151
InsertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis: boolean;
1152+
InsertSpaceAfterOpeningAndBeforeClosingNonemptyBrackets: boolean;
11521153
PlaceOpenBraceOnNewLineForFunctions: boolean;
11531154
PlaceOpenBraceOnNewLineForControlBlocks: boolean;
11541155
[s: string]: boolean | number| string;

tests/cases/fourslash/formattingOptionsChange.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
/////*InsertSpaceAfterKeywordsInControlFlowStatements*/if (true) { }
77
/////*InsertSpaceAfterFunctionKeywordForAnonymousFunctions*/(function () { })
88
/////*InsertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis*/(1 )
9+
/////*InsertSpaceAfterOpeningAndBeforeClosingNonemptyBrackets*/[1 ]
910
/////*PlaceOpenBraceOnNewLineForFunctions*/class foo {
1011
////}
1112
/////*PlaceOpenBraceOnNewLineForControlBlocks*/if (true) {
@@ -17,6 +18,7 @@ runTest("InsertSpaceBeforeAndAfterBinaryOperators", "1 + 2 - 3", "1+2-3");
1718
runTest("InsertSpaceAfterKeywordsInControlFlowStatements", "if (true) { }", "if(true) { }");
1819
runTest("InsertSpaceAfterFunctionKeywordForAnonymousFunctions", "(function () { })", "(function() { })");
1920
runTest("InsertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis", " ( 1 )", " (1)");
21+
runTest( "InsertSpaceAfterOpeningAndBeforeClosingNonemptyBrackets", "[ 1 ]", "[1]" );
2022
runTest("PlaceOpenBraceOnNewLineForFunctions", "class foo", "class foo {");
2123
runTest("PlaceOpenBraceOnNewLineForControlBlocks", "if ( true )", "if ( true ) {");
2224

tests/cases/fourslash/fourslash.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ module FourSlashInterface {
9090
InsertSpaceAfterKeywordsInControlFlowStatements: boolean;
9191
InsertSpaceAfterFunctionKeywordForAnonymousFunctions: boolean;
9292
InsertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis: boolean;
93+
InsertSpaceAfterOpeningAndBeforeClosingNonemptyBrackets: boolean;
9394
PlaceOpenBraceOnNewLineForFunctions: boolean;
9495
PlaceOpenBraceOnNewLineForControlBlocks: boolean;
9596
[s: string]: boolean | number| string;

0 commit comments

Comments
 (0)