From ea09299d1e5f03b481d36e65e8d46932d5fbd8ec Mon Sep 17 00:00:00 2001 From: Vladimir Matveev Date: Sun, 22 Feb 2015 16:15:54 -0800 Subject: [PATCH 1/2] added rule to insert space after let\const in variable declaration context --- src/services/formatting/formattingContext.ts | 2 +- src/services/formatting/rules.ts | 8 +++++++ .../fourslash/formattingInDestructuring1.ts | 22 +++++++++++++++++++ 3 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 tests/cases/fourslash/formattingInDestructuring1.ts diff --git a/src/services/formatting/formattingContext.ts b/src/services/formatting/formattingContext.ts index b7573526f78fc..52de4105df610 100644 --- a/src/services/formatting/formattingContext.ts +++ b/src/services/formatting/formattingContext.ts @@ -29,7 +29,7 @@ module ts.formatting { private contextNodeBlockIsOnOneLine: boolean; private nextNodeBlockIsOnOneLine: boolean; - constructor(private sourceFile: SourceFile, public formattingRequestKind: FormattingRequestKind) { + constructor(public sourceFile: SourceFile, public formattingRequestKind: FormattingRequestKind) { } public updateContext(currentRange: TextRangeWithKind, currentTokenParent: Node, nextRange: TextRangeWithKind, nextTokenParent: Node, commonParent: Node) { diff --git a/src/services/formatting/rules.ts b/src/services/formatting/rules.ts index b5470579787f4..697463831e186 100644 --- a/src/services/formatting/rules.ts +++ b/src/services/formatting/rules.ts @@ -92,6 +92,7 @@ module ts.formatting { public NoSpaceBeforeComma: Rule; public SpaceAfterCertainKeywords: Rule; + public SpaceAfterLetConstInVariableDeclaration: Rule; public NoSpaceBeforeOpenParenInFuncCall: Rule; public SpaceAfterFunctionInFuncDecl: Rule; public NoSpaceBeforeOpenParenInFuncDecl: Rule; @@ -283,6 +284,7 @@ module ts.formatting { this.NoSpaceBeforeComma = new Rule(RuleDescriptor.create2(Shared.TokenRange.Any, SyntaxKind.CommaToken), RuleOperation.create2(new RuleOperationContext(Rules.IsSameLineTokenContext), RuleAction.Delete)); this.SpaceAfterCertainKeywords = new Rule(RuleDescriptor.create4(Shared.TokenRange.FromTokens([SyntaxKind.VarKeyword, SyntaxKind.ThrowKeyword, SyntaxKind.NewKeyword, SyntaxKind.DeleteKeyword, SyntaxKind.ReturnKeyword, SyntaxKind.TypeOfKeyword]), Shared.TokenRange.Any), RuleOperation.create2(new RuleOperationContext(Rules.IsSameLineTokenContext), RuleAction.Space)); + this.SpaceAfterLetConstInVariableDeclaration = new Rule(RuleDescriptor.create4(Shared.TokenRange.FromTokens([SyntaxKind.LetKeyword, SyntaxKind.ConstKeyword]), Shared.TokenRange.Any), RuleOperation.create2(new RuleOperationContext(Rules.IsSameLineTokenContext, Rules.IsStartOfVariableDeclarationList), RuleAction.Space)); this.NoSpaceBeforeOpenParenInFuncCall = new Rule(RuleDescriptor.create2(Shared.TokenRange.Any, SyntaxKind.OpenParenToken), RuleOperation.create2(new RuleOperationContext(Rules.IsSameLineTokenContext, Rules.IsFunctionCallOrNewContext, Rules.IsPreviousTokenNotComma), RuleAction.Delete)); this.SpaceAfterFunctionInFuncDecl = new Rule(RuleDescriptor.create3(SyntaxKind.FunctionKeyword, Shared.TokenRange.Any), RuleOperation.create2(new RuleOperationContext(Rules.IsFunctionDeclContext), RuleAction.Space)); this.NoSpaceBeforeOpenParenInFuncDecl = new Rule(RuleDescriptor.create2(Shared.TokenRange.Any, SyntaxKind.OpenParenToken), RuleOperation.create2(new RuleOperationContext(Rules.IsSameLineTokenContext, Rules.IsFunctionDeclContext), RuleAction.Delete)); @@ -356,6 +358,7 @@ module ts.formatting { this.SpaceAfterFunctionInFuncDecl, this.NewLineAfterOpenBraceInBlockContext, this.SpaceAfterGetSetInMember, this.NoSpaceBetweenReturnAndSemicolon, this.SpaceAfterCertainKeywords, + this.SpaceAfterLetConstInVariableDeclaration, this.NoSpaceBeforeOpenParenInFuncCall, this.SpaceBeforeBinaryKeywordOperator, this.SpaceAfterBinaryKeywordOperator, this.SpaceAfterVoidOperator, @@ -634,6 +637,11 @@ module ts.formatting { return context.TokensAreOnSameLine(); } + static IsStartOfVariableDeclarationList(context: FormattingContext): boolean { + return context.currentTokenParent.kind === SyntaxKind.VariableDeclarationList && + context.currentTokenParent.getStart(context.sourceFile) === context.currentTokenSpan.pos; + } + static IsNotFormatOnEnter(context: FormattingContext): boolean { return context.formattingRequestKind != FormattingRequestKind.FormatOnEnter; } diff --git a/tests/cases/fourslash/formattingInDestructuring1.ts b/tests/cases/fourslash/formattingInDestructuring1.ts new file mode 100644 index 0000000000000..21865b07f724c --- /dev/null +++ b/tests/cases/fourslash/formattingInDestructuring1.ts @@ -0,0 +1,22 @@ +/// + +////interface let { } +/////*1*/var x: let []; +//// +////function foo() { +//// 'use strict' +/////*2*/ let [x] = []; +/////*3*/ const [x] = []; +/////*4*/ for (let[x] = [];x < 1;) { +//// } +////} + +format.document(); +goTo.marker("1"); +verify.currentLineContentIs("var x: let[];"); +goTo.marker("2"); +verify.currentLineContentIs(" let [x] = [];"); +goTo.marker("3"); +verify.currentLineContentIs(" const [x] = [];"); +goTo.marker("4"); +verify.currentLineContentIs(" for (let [x] = []; x < 1;) {"); \ No newline at end of file From b8a1712688c159efb487be1171691c9ae9489dea Mon Sep 17 00:00:00 2001 From: Vladimir Matveev Date: Sun, 22 Feb 2015 16:44:26 -0800 Subject: [PATCH 2/2] include binding element with initializer to current formatting rule for binary operator, adjust existing SpaceBeforeOpenBraceInFunction rule to recognize blocks --- src/services/formatting/rules.ts | 4 +++- tests/cases/fourslash/formattingInDestructuring2.ts | 9 +++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) create mode 100644 tests/cases/fourslash/formattingInDestructuring2.ts diff --git a/src/services/formatting/rules.ts b/src/services/formatting/rules.ts index 697463831e186..c72839dfcc040 100644 --- a/src/services/formatting/rules.ts +++ b/src/services/formatting/rules.ts @@ -239,7 +239,7 @@ module ts.formatting { // Place a space before open brace in a function declaration this.FunctionOpenBraceLeftTokenRange = Shared.TokenRange.AnyIncludingMultilineComments; - this.SpaceBeforeOpenBraceInFunction = new Rule(RuleDescriptor.create2(this.FunctionOpenBraceLeftTokenRange, SyntaxKind.OpenBraceToken), RuleOperation.create2(new RuleOperationContext(Rules.IsFunctionDeclContext, Rules.IsNotFormatOnEnter, Rules.IsSameLineTokenOrBeforeMultilineBlockContext), RuleAction.Space), RuleFlags.CanDeleteNewLines); + this.SpaceBeforeOpenBraceInFunction = new Rule(RuleDescriptor.create2(this.FunctionOpenBraceLeftTokenRange, SyntaxKind.OpenBraceToken), RuleOperation.create2(new RuleOperationContext(Rules.IsFunctionDeclContext, Rules.IsBeforeBlockContext, Rules.IsNotFormatOnEnter, Rules.IsSameLineTokenOrBeforeMultilineBlockContext), RuleAction.Space), RuleFlags.CanDeleteNewLines); // Place a space before open brace in a TypeScript declaration that has braces as children (class, module, enum, etc) this.TypeScriptOpenBraceLeftTokenRange = Shared.TokenRange.FromTokens([SyntaxKind.Identifier, SyntaxKind.MultiLineCommentTrivia]); @@ -470,6 +470,8 @@ module ts.formatting { // Technically, "of" is not a binary operator, but format it the same way as "in" case SyntaxKind.ForOfStatement: return context.currentTokenSpan.kind === SyntaxKind.OfKeyword || context.nextTokenSpan.kind === SyntaxKind.OfKeyword; + case SyntaxKind.BindingElement: + return context.currentTokenSpan.kind === SyntaxKind.EqualsToken || context.nextTokenSpan.kind === SyntaxKind.EqualsToken; } return false; } diff --git a/tests/cases/fourslash/formattingInDestructuring2.ts b/tests/cases/fourslash/formattingInDestructuring2.ts new file mode 100644 index 0000000000000..753eed44e121b --- /dev/null +++ b/tests/cases/fourslash/formattingInDestructuring2.ts @@ -0,0 +1,9 @@ +/// + +/////*1*/function drawText( { text = "", location: [x, y]= [0, 0], bold = false }) { +//// // Draw text +////} + +format.document(); +goTo.marker("1"); +verify.currentLineContentIs("function drawText({ text = \"\", location: [x, y] = [0, 0], bold = false }) {"); \ No newline at end of file