diff --git a/src/harness/fourslash.ts b/src/harness/fourslash.ts index 653b87236fd..2a0d37d190b 100644 --- a/src/harness/fourslash.ts +++ b/src/harness/fourslash.ts @@ -479,7 +479,7 @@ namespace FourSlash { endPos = endMarker.position; } - errors.forEach(function(error: ts.Diagnostic) { + errors.forEach(function (error: ts.Diagnostic) { if (predicate(error.start, error.start + error.length, startPos, endPos)) { exists = true; } @@ -496,7 +496,7 @@ namespace FourSlash { Harness.IO.log("Unexpected error(s) found. Error list is:"); } - errors.forEach(function(error: ts.Diagnostic) { + errors.forEach(function (error: ts.Diagnostic) { Harness.IO.log(" minChar: " + error.start + ", limChar: " + (error.start + error.length) + ", message: " + ts.flattenDiagnosticMessageText(error.messageText, Harness.IO.newLine()) + "\n"); @@ -3524,11 +3524,8 @@ namespace FourSlashInterface { this.state.formatOnType(this.state.getMarkerByName(posMarker).position, key); } - public setOption(name: string, value: number): void; - public setOption(name: string, value: string): void; - public setOption(name: string, value: boolean): void; - public setOption(name: string, value: any): void { - (this.state.formatCodeSettings)[name] = value; + public setOption(name: keyof ts.FormatCodeSettings, value: number | string | boolean): void { + this.state.formatCodeSettings[name] = value; } } diff --git a/src/server/utilities.ts b/src/server/utilities.ts index 641ca128e63..d0790b93dba 100644 --- a/src/server/utilities.ts +++ b/src/server/utilities.ts @@ -86,6 +86,7 @@ namespace ts.server { insertSpaceAfterFunctionKeywordForAnonymousFunctions: false, insertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis: false, insertSpaceAfterOpeningAndBeforeClosingNonemptyBrackets: false, + insertSpaceAfterOpeningAndBeforeClosingNonemptyBraces: true, insertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces: false, insertSpaceAfterOpeningAndBeforeClosingJsxExpressionBraces: false, insertSpaceBeforeFunctionParenthesis: false, diff --git a/src/services/formatting/rules.ts b/src/services/formatting/rules.ts index e0268675e5a..5b1543b79f0 100644 --- a/src/services/formatting/rules.ts +++ b/src/services/formatting/rules.ts @@ -292,10 +292,10 @@ namespace ts.formatting { this.SpaceBeforeOpenBraceInControl = new Rule(RuleDescriptor.create2(this.ControlOpenBraceLeftTokenRange, SyntaxKind.OpenBraceToken), RuleOperation.create2(new RuleOperationContext(Rules.IsControlDeclContext, Rules.IsNotFormatOnEnter, Rules.IsSameLineTokenOrBeforeMultilineBlockContext), RuleAction.Space), RuleFlags.CanDeleteNewLines); // Insert a space after { and before } in single-line contexts, but remove space from empty object literals {}. - this.SpaceAfterOpenBrace = new Rule(RuleDescriptor.create3(SyntaxKind.OpenBraceToken, Shared.TokenRange.Any), RuleOperation.create2(new RuleOperationContext(Rules.IsSingleLineBlockContext), RuleAction.Space)); - this.SpaceBeforeCloseBrace = new Rule(RuleDescriptor.create2(Shared.TokenRange.Any, SyntaxKind.CloseBraceToken), RuleOperation.create2(new RuleOperationContext(Rules.IsSingleLineBlockContext), RuleAction.Space)); - this.NoSpaceAfterOpenBrace = new Rule(RuleDescriptor.create3(SyntaxKind.OpenBraceToken, Shared.TokenRange.Any), RuleOperation.create2(new RuleOperationContext(Rules.IsSingleLineBlockContext), RuleAction.Delete)); - this.NoSpaceBeforeCloseBrace = new Rule(RuleDescriptor.create2(Shared.TokenRange.Any, SyntaxKind.CloseBraceToken), RuleOperation.create2(new RuleOperationContext(Rules.IsSingleLineBlockContext), RuleAction.Delete)); + this.SpaceAfterOpenBrace = new Rule(RuleDescriptor.create3(SyntaxKind.OpenBraceToken, Shared.TokenRange.Any), RuleOperation.create2(new RuleOperationContext(Rules.IsBraceWrappedContext), RuleAction.Space)); + this.SpaceBeforeCloseBrace = new Rule(RuleDescriptor.create2(Shared.TokenRange.Any, SyntaxKind.CloseBraceToken), RuleOperation.create2(new RuleOperationContext(Rules.IsBraceWrappedContext), RuleAction.Space)); + this.NoSpaceAfterOpenBrace = new Rule(RuleDescriptor.create3(SyntaxKind.OpenBraceToken, Shared.TokenRange.Any), RuleOperation.create2(new RuleOperationContext(Rules.IsBraceWrappedContext), RuleAction.Delete)); + this.NoSpaceBeforeCloseBrace = new Rule(RuleDescriptor.create2(Shared.TokenRange.Any, SyntaxKind.CloseBraceToken), RuleOperation.create2(new RuleOperationContext(Rules.IsBraceWrappedContext), RuleAction.Delete)); this.NoSpaceBetweenEmptyBraceBrackets = new Rule(RuleDescriptor.create1(SyntaxKind.OpenBraceToken, SyntaxKind.CloseBraceToken), RuleOperation.create2(new RuleOperationContext(Rules.IsNonJsxSameLineTokenContext, Rules.IsObjectContext), RuleAction.Delete)); // Insert new line after { and before } in multi-line contexts. @@ -617,6 +617,10 @@ namespace ts.formatting { return context.TokensAreOnSameLine() || Rules.IsBeforeMultilineBlockContext(context); } + static IsBraceWrappedContext(context: FormattingContext): boolean { + return context.contextNode.kind === SyntaxKind.ObjectBindingPattern || Rules.IsSingleLineBlockContext(context); + } + // This check is done before an open brace in a control construct, a function, or a typescript block declaration static IsBeforeMultilineBlockContext(context: FormattingContext): boolean { return Rules.IsBeforeBlockContext(context) && !(context.NextNodeAllOnSameLine() || context.NextNodeBlockIsOnOneLine()); diff --git a/tests/cases/fourslash/formattingOptionsChange.ts b/tests/cases/fourslash/formattingOptionsChange.ts index 63d51eec0ca..0e731412ede 100644 --- a/tests/cases/fourslash/formattingOptionsChange.ts +++ b/tests/cases/fourslash/formattingOptionsChange.ts @@ -1,33 +1,34 @@ /// -/////*InsertSpaceAfterCommaDelimiter*/[1,2, 3];[ 72 , ]; -/////*InsertSpaceAfterSemicolonInForStatements*/for (i = 0;i; i++); -/////*InsertSpaceBeforeAndAfterBinaryOperators*/1+2- 3 -/////*InsertSpaceAfterKeywordsInControlFlowStatements*/if (true) { } -/////*InsertSpaceAfterFunctionKeywordForAnonymousFunctions*/(function () { }) -/////*InsertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis*/(1 ) -/////*InsertSpaceAfterOpeningAndBeforeClosingNonemptyBrackets*/[1 ]; [ ]; []; [,]; -/////*InsertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces*/`${1}`;`${ 1 }` -/////*InsertSpaceAfterTypeAssertion*/const bar = Thing.getFoo(); -/////*PlaceOpenBraceOnNewLineForFunctions*/class foo { +/////*insertSpaceAfterCommaDelimiter*/[1,2, 3];[ 72 , ]; +/////*insertSpaceAfterSemicolonInForStatements*/for (i = 0;i; i++); +/////*insertSpaceBeforeAndAfterBinaryOperators*/1+2- 3 +/////*insertSpaceAfterKeywordsInControlFlowStatements*/if (true) { } +/////*insertSpaceAfterFunctionKeywordForAnonymousFunctions*/(function () { }) +/////*insertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis*/(1 ) +/////*insertSpaceAfterOpeningAndBeforeClosingNonemptyBrackets*/[1 ]; [ ]; []; [,]; +/////*insertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces*/`${1}`;`${ 1 }` +/////*insertSpaceAfterTypeAssertion*/const bar = Thing.getFoo(); +/////*placeOpenBraceOnNewLineForFunctions*/class foo { ////} -/////*PlaceOpenBraceOnNewLineForControlBlocks*/if (true) { +/////*placeOpenBraceOnNewLineForControlBlocks*/if (true) { ////} -/////*InsertSpaceAfterOpeningAndBeforeClosingNonemptyBraces*/{ var t = 1}; +/////*insertSpaceAfterOpeningAndBeforeClosingNonemptyBraces*/{ var t = 1}; var {a,b } = { a: 'sw', b:'r' };function f( { a, b}) { } -runTest("InsertSpaceAfterCommaDelimiter", "[1, 2, 3];[72,];", "[1,2,3];[72,];"); -runTest("InsertSpaceAfterSemicolonInForStatements", "for (i = 0; i; i++);", "for (i = 0;i;i++);"); -runTest("InsertSpaceBeforeAndAfterBinaryOperators", "1 + 2 - 3", "1+2-3"); -runTest("InsertSpaceAfterKeywordsInControlFlowStatements", "if (true) { }", "if(true) { }"); -runTest("InsertSpaceAfterFunctionKeywordForAnonymousFunctions", "(function () { })", "(function() { })"); -runTest("InsertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis", " ( 1 )", " (1)"); -runTest("InsertSpaceAfterOpeningAndBeforeClosingNonemptyBrackets", "[ 1 ];[];[];[ , ];", "[1];[];[];[,];"); -runTest("InsertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces", "`${ 1 }`; `${ 1 }`", "`${1}`; `${1}`"); -runTest("InsertSpaceAfterTypeAssertion", "const bar = Thing.getFoo();", "const bar = Thing.getFoo();"); -runTest("PlaceOpenBraceOnNewLineForFunctions", "class foo", "class foo {"); -runTest("PlaceOpenBraceOnNewLineForControlBlocks", "if ( true )", "if ( true ) {"); -runTest("InsertSpaceAfterOpeningAndBeforeClosingNonemptyBraces", "{ var t = 1 };", "{var t = 1};"); +const defaultFormatOption = format.copyFormatOptions(); +runTest("insertSpaceAfterCommaDelimiter", "[1, 2, 3];[72,];", "[1,2,3];[72,];"); +runTest("insertSpaceAfterSemicolonInForStatements", "for (i = 0; i; i++);", "for (i = 0;i;i++);"); +runTest("insertSpaceBeforeAndAfterBinaryOperators", "1 + 2 - 3", "1+2-3"); +runTest("insertSpaceAfterKeywordsInControlFlowStatements", "if (true) { }", "if(true) { }"); +runTest("insertSpaceAfterFunctionKeywordForAnonymousFunctions", "(function () { })", "(function() { })"); +runTest("insertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis", " ( 1 )", " (1)"); +runTest("insertSpaceAfterOpeningAndBeforeClosingNonemptyBrackets", "[ 1 ];[];[];[ , ];", "[1];[];[];[,];"); +runTest("insertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces", "`${ 1 }`; `${ 1 }`", "`${1}`; `${1}`"); +runTest("insertSpaceAfterTypeAssertion", "const bar = Thing.getFoo();", "const bar = Thing.getFoo();"); +runTest("placeOpenBraceOnNewLineForFunctions", "class foo", "class foo {"); +runTest("placeOpenBraceOnNewLineForControlBlocks", "if (true)", "if (true) {"); +runTest("insertSpaceAfterOpeningAndBeforeClosingNonemptyBraces", "{ var t = 1 }; var { a, b } = { a: 'sw', b: 'r' }; function f({ a, b }) { }", "{var t = 1}; var {a, b} = {a: 'sw', b: 'r'}; function f({a, b}) {}"); function runTest(propertyName: string, expectedStringWhenTrue: string, expectedStringWhenFalse: string) { // Go to the correct file @@ -52,4 +53,6 @@ function runTest(propertyName: string, expectedStringWhenTrue: string, expectedS // Verify goTo.marker(propertyName); verify.currentLineContentIs(expectedStringWhenTrue); + + format.setOption(propertyName, defaultFormatOption[propertyName]) } \ No newline at end of file diff --git a/tests/cases/fourslash/fourslash.ts b/tests/cases/fourslash/fourslash.ts index 745a746db11..63b38c337d7 100644 --- a/tests/cases/fourslash/fourslash.ts +++ b/tests/cases/fourslash/fourslash.ts @@ -294,9 +294,7 @@ declare namespace FourSlashInterface { setFormatOptions(options: FormatCodeOptions): any; selection(startMarker: string, endMarker: string): void; onType(posMarker: string, key: string): void; - setOption(name: string, value: number): any; - setOption(name: string, value: string): any; - setOption(name: string, value: boolean): any; + setOption(name: string, value: number | string | boolean): void; } class cancellation { resetCancelled(): void;