From 582ff0d4be6f8a030f581b7e3b6e1b9b79a0c37f Mon Sep 17 00:00:00 2001 From: Andrew Bradley Date: Mon, 23 Oct 2017 21:12:49 -0400 Subject: [PATCH] Tweak #2 --- src/services/formatting/formattingContext.ts | 14 +++++++++++++- src/services/formatting/rules.ts | 12 ++++++++---- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/src/services/formatting/formattingContext.ts b/src/services/formatting/formattingContext.ts index 043df72f4342b..2858f83d3f74a 100644 --- a/src/services/formatting/formattingContext.ts +++ b/src/services/formatting/formattingContext.ts @@ -12,6 +12,7 @@ namespace ts.formatting { private contextNodeAllOnSameLine: boolean; private nextNodeAllOnSameLine: boolean; private tokensAreOnSameLine: boolean; + private spanBetweenTokensIsOnSingleLine: boolean; private contextNodeBlockIsOnOneLine: boolean; private nextNodeBlockIsOnOneLine: boolean; @@ -35,6 +36,7 @@ namespace ts.formatting { this.contextNodeAllOnSameLine = undefined; this.nextNodeAllOnSameLine = undefined; this.tokensAreOnSameLine = undefined; + this.spanBetweenTokensIsOnSingleLine = undefined; this.contextNodeBlockIsOnOneLine = undefined; this.nextNodeBlockIsOnOneLine = undefined; } @@ -65,6 +67,16 @@ namespace ts.formatting { return this.tokensAreOnSameLine; } + public SpanBetweenTokensIsOnSingleLine(): boolean { + if (this.spanBetweenTokensIsOnSingleLine === undefined) { + const startLine = this.sourceFile.getLineAndCharacterOfPosition(this.currentTokenSpan.end).line; + const endLine = this.sourceFile.getLineAndCharacterOfPosition(this.nextTokenSpan.pos).line; + this.spanBetweenTokensIsOnSingleLine = (startLine === endLine); + } + + return this.spanBetweenTokensIsOnSingleLine; + } + public ContextNodeBlockIsOnOneLine() { if (this.contextNodeBlockIsOnOneLine === undefined) { this.contextNodeBlockIsOnOneLine = this.BlockIsOnOneLine(this.contextNode); @@ -98,4 +110,4 @@ namespace ts.formatting { return false; } } -} \ No newline at end of file +} diff --git a/src/services/formatting/rules.ts b/src/services/formatting/rules.ts index d41d9dfbfd868..183f28c8487ce 100644 --- a/src/services/formatting/rules.ts +++ b/src/services/formatting/rules.ts @@ -459,10 +459,10 @@ namespace ts.formatting { this.NoSpaceBeforeCloseBracket = new Rule(RuleDescriptor.create2(Shared.TokenRange.Any, SyntaxKind.CloseBracketToken), RuleOperation.create2(new RuleOperationContext(Rules.IsOptionDisabledOrUndefined("insertSpaceAfterOpeningAndBeforeClosingNonemptyBrackets"), Rules.IsNonJsxSameLineTokenContext), RuleAction.Delete)); // Insert space after opening and before closing template string braces - this.NoSpaceAfterTemplateHeadAndMiddle = new Rule(RuleDescriptor.create4(Shared.TokenRange.FromTokens([SyntaxKind.TemplateHead, SyntaxKind.TemplateMiddle]), Shared.TokenRange.Any), RuleOperation.create2(new RuleOperationContext(Rules.IsOptionDisabledOrUndefined("insertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces"), Rules.IsNonJsxSameLineTokenContext), RuleAction.Delete)); - this.SpaceAfterTemplateHeadAndMiddle = new Rule(RuleDescriptor.create4(Shared.TokenRange.FromTokens([SyntaxKind.TemplateHead, SyntaxKind.TemplateMiddle]), Shared.TokenRange.Any), RuleOperation.create2(new RuleOperationContext(Rules.IsOptionEnabled("insertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces"), Rules.IsNonJsxSameLineTokenContext), RuleAction.Space)); - this.NoSpaceBeforeTemplateMiddleAndTail = new Rule(RuleDescriptor.create4(Shared.TokenRange.Any, Shared.TokenRange.FromTokens([SyntaxKind.TemplateMiddle, SyntaxKind.TemplateTail])), RuleOperation.create2(new RuleOperationContext(Rules.IsOptionDisabledOrUndefined("insertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces"), Rules.IsNonJsxSameLineTokenContext), RuleAction.Delete)); - this.SpaceBeforeTemplateMiddleAndTail = new Rule(RuleDescriptor.create4(Shared.TokenRange.Any, Shared.TokenRange.FromTokens([SyntaxKind.TemplateMiddle, SyntaxKind.TemplateTail])), RuleOperation.create2(new RuleOperationContext(Rules.IsOptionEnabled("insertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces"), Rules.IsNonJsxSameLineTokenContext), RuleAction.Space)); + this.NoSpaceAfterTemplateHeadAndMiddle = new Rule(RuleDescriptor.create4(Shared.TokenRange.FromTokens([SyntaxKind.TemplateHead, SyntaxKind.TemplateMiddle]), Shared.TokenRange.Any), RuleOperation.create2(new RuleOperationContext(Rules.IsOptionDisabledOrUndefined("insertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces"), Rules.IsNonJsxAndSpanBetweenIsSingleLineTokenContext), RuleAction.Delete)); + this.SpaceAfterTemplateHeadAndMiddle = new Rule(RuleDescriptor.create4(Shared.TokenRange.FromTokens([SyntaxKind.TemplateHead, SyntaxKind.TemplateMiddle]), Shared.TokenRange.Any), RuleOperation.create2(new RuleOperationContext(Rules.IsOptionEnabled("insertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces"), Rules.IsNonJsxAndSpanBetweenIsSingleLineTokenContext), RuleAction.Space)); + this.NoSpaceBeforeTemplateMiddleAndTail = new Rule(RuleDescriptor.create4(Shared.TokenRange.Any, Shared.TokenRange.FromTokens([SyntaxKind.TemplateMiddle, SyntaxKind.TemplateTail])), RuleOperation.create2(new RuleOperationContext(Rules.IsOptionDisabledOrUndefined("insertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces"), Rules.IsNonJsxAndSpanBetweenIsSingleLineTokenContext), RuleAction.Delete)); + this.SpaceBeforeTemplateMiddleAndTail = new Rule(RuleDescriptor.create4(Shared.TokenRange.Any, Shared.TokenRange.FromTokens([SyntaxKind.TemplateMiddle, SyntaxKind.TemplateTail])), RuleOperation.create2(new RuleOperationContext(Rules.IsOptionEnabled("insertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces"), Rules.IsNonJsxAndSpanBetweenIsSingleLineTokenContext), RuleAction.Space)); // No space after { and before } in JSX expression this.NoSpaceAfterOpenBraceInJsxExpression = new Rule(RuleDescriptor.create3(SyntaxKind.OpenBraceToken, Shared.TokenRange.Any), RuleOperation.create2(new RuleOperationContext(Rules.IsOptionDisabledOrUndefined("insertSpaceAfterOpeningAndBeforeClosingJsxExpressionBraces"), Rules.IsNonJsxSameLineTokenContext, Rules.IsJsxExpressionContext), RuleAction.Delete)); @@ -817,6 +817,10 @@ namespace ts.formatting { return context.TokensAreOnSameLine() && context.contextNode.kind !== SyntaxKind.JsxText; } + static IsNonJsxAndSpanBetweenIsSingleLineTokenContext(context: FormattingContext): boolean { + return context.SpanBetweenTokensIsOnSingleLine() && context.contextNode.kind !== SyntaxKind.JsxText; + } + static IsNonJsxElementContext(context: FormattingContext): boolean { return context.contextNode.kind !== SyntaxKind.JsxElement; }