From be5557a6445a0276a538f91d409b7fd19a852c2d Mon Sep 17 00:00:00 2001 From: Jason Freeman Date: Thu, 23 Apr 2015 14:55:13 -0700 Subject: [PATCH] Formatting for generators --- src/services/formatting/rules.ts | 14 +++++++++++++- .../fourslash/generatorDeclarationFormatting.ts | 10 ++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 tests/cases/fourslash/generatorDeclarationFormatting.ts diff --git a/src/services/formatting/rules.ts b/src/services/formatting/rules.ts index 0b6a6ad0bdc..b6b33ca51f3 100644 --- a/src/services/formatting/rules.ts +++ b/src/services/formatting/rules.ts @@ -193,12 +193,16 @@ module ts.formatting { // Insert space after function keyword for anonymous functions public SpaceAfterAnonymousFunctionKeyword: Rule; public NoSpaceAfterAnonymousFunctionKeyword: Rule; - + // Insert space after @ in decorator public SpaceBeforeAt: Rule; public NoSpaceAfterAt: Rule; public SpaceAfterDecorator: Rule; + // Generator: function* + public NoSpaceBetweenFunctionKeywordAndStar: Rule; + public SpaceAfterStarInGenerator: Rule; + constructor() { /// /// Common Rules @@ -340,6 +344,9 @@ module ts.formatting { this.NoSpaceAfterAt = new Rule(RuleDescriptor.create3(SyntaxKind.AtToken, Shared.TokenRange.Any), RuleOperation.create2(new RuleOperationContext(Rules.IsSameLineTokenContext), RuleAction.Delete)); this.SpaceAfterDecorator = new Rule(RuleDescriptor.create4(Shared.TokenRange.Any, Shared.TokenRange.FromTokens([SyntaxKind.Identifier, SyntaxKind.ExportKeyword, SyntaxKind.DefaultKeyword, SyntaxKind.ClassKeyword, SyntaxKind.StaticKeyword, SyntaxKind.PublicKeyword, SyntaxKind.PrivateKeyword, SyntaxKind.ProtectedKeyword, SyntaxKind.GetKeyword, SyntaxKind.SetKeyword, SyntaxKind.OpenBracketToken, SyntaxKind.AsteriskToken])), RuleOperation.create2(new RuleOperationContext(Rules.IsEndOfDecoratorContextOnSameLine), RuleAction.Space)); + this.NoSpaceBetweenFunctionKeywordAndStar = new Rule(RuleDescriptor.create1(SyntaxKind.FunctionKeyword, SyntaxKind.AsteriskToken), RuleOperation.create2(new RuleOperationContext(Rules.IsFunctionDeclarationOrFunctionExpressionContext), RuleAction.Delete)); + this.SpaceAfterStarInGenerator = new Rule(RuleDescriptor.create3(SyntaxKind.AsteriskToken, Shared.TokenRange.FromTokens([SyntaxKind.Identifier, SyntaxKind.OpenParenToken])), RuleOperation.create2(new RuleOperationContext(Rules.IsFunctionDeclarationOrFunctionExpressionContext), RuleAction.Space)); + // These rules are higher in priority than user-configurable rules. this.HighPriorityCommonRules = [ @@ -357,6 +364,7 @@ module ts.formatting { this.NoSpaceAfterCloseBrace, this.SpaceAfterOpenBrace, this.SpaceBeforeCloseBrace, this.NewLineBeforeCloseBraceInBlockContext, this.SpaceAfterCloseBrace, this.SpaceBetweenCloseBraceAndElse, this.SpaceBetweenCloseBraceAndWhile, this.NoSpaceBetweenEmptyBraceBrackets, + this.NoSpaceBetweenFunctionKeywordAndStar, this.SpaceAfterStarInGenerator, this.SpaceAfterFunctionInFuncDecl, this.NewLineAfterOpenBraceInBlockContext, this.SpaceAfterGetSetInMember, this.NoSpaceBetweenReturnAndSemicolon, this.SpaceAfterCertainKeywords, @@ -574,6 +582,10 @@ module ts.formatting { return false; } + static IsFunctionDeclarationOrFunctionExpressionContext(context: FormattingContext): boolean { + return context.contextNode.kind === SyntaxKind.FunctionDeclaration || context.contextNode.kind === SyntaxKind.FunctionExpression; + } + static IsTypeScriptDeclWithBlockContext(context: FormattingContext): boolean { return Rules.NodeIsTypeScriptDeclWithBlockContext(context.contextNode); } diff --git a/tests/cases/fourslash/generatorDeclarationFormatting.ts b/tests/cases/fourslash/generatorDeclarationFormatting.ts new file mode 100644 index 00000000000..007380fd0de --- /dev/null +++ b/tests/cases/fourslash/generatorDeclarationFormatting.ts @@ -0,0 +1,10 @@ +/// + +//// function *g() { }/*1*/ +//// var v = function *() { };/*2*/ + +format.document(); +goTo.marker('1'); +verify.currentLineContentIs("function* g() { }"); +goTo.marker('2'); +verify.currentLineContentIs("var v = function* () { };"); \ No newline at end of file