From 40358a1e65d3d67edb62d8aa09547bfeda54d990 Mon Sep 17 00:00:00 2001 From: Vladimir Matveev Date: Tue, 14 Oct 2014 16:48:28 -0700 Subject: [PATCH] fix issues in formattingContext - 35 failing tests so far --- src/services/formatting/format.ts | 1 + .../formatting/new/formattingContext.ts | 26 +++++++------- src/services/formatting/new/rules.ts | 34 ++++++++++++++++--- 3 files changed, 42 insertions(+), 19 deletions(-) diff --git a/src/services/formatting/format.ts b/src/services/formatting/format.ts index 72c55332a3c..6ad5384be0f 100644 --- a/src/services/formatting/format.ts +++ b/src/services/formatting/format.ts @@ -226,6 +226,7 @@ module ts.formatting { while (currentTokenInfo.token && node.end >= currentTokenInfo.token.end) { currentTokenInfo = consumeCurrentToken(node, childContextNode, indentation); + childContextNode = node; } /// Local functions diff --git a/src/services/formatting/new/formattingContext.ts b/src/services/formatting/new/formattingContext.ts index ffac516690b..bd146397bbf 100644 --- a/src/services/formatting/new/formattingContext.ts +++ b/src/services/formatting/new/formattingContext.ts @@ -17,17 +17,17 @@ module ts.formatting { export class FormattingContext { - public currentTokenSpan: TextRangeWithKind = null; - public nextTokenSpan: TextRangeWithKind = null; - public contextNode: Node = null; - public currentTokenParent: Node = null; - public nextTokenParent: Node = null; + public currentTokenSpan: TextRangeWithKind; + public nextTokenSpan: TextRangeWithKind; + public contextNode: Node; + public currentTokenParent: Node; + public nextTokenParent: Node; - private contextNodeAllOnSameLine: boolean = null; - private nextNodeAllOnSameLine: boolean = null; - private tokensAreOnSameLine: boolean = null; - private contextNodeBlockIsOnOneLine: boolean = null; - private nextNodeBlockIsOnOneLine: boolean = null; + private contextNodeAllOnSameLine: boolean; + private nextNodeAllOnSameLine: boolean; + private tokensAreOnSameLine: boolean; + private contextNodeBlockIsOnOneLine: boolean; + private nextNodeBlockIsOnOneLine: boolean; constructor(private sourceFile: SourceFile, public formattingRequestKind: FormattingRequestKind) { } @@ -98,9 +98,7 @@ module ts.formatting { return this.nextNodeBlockIsOnOneLine; } - public NodeIsOnOneLine(node: Node): boolean { - return; - + private NodeIsOnOneLine(node: Node): boolean { var startLine = this.sourceFile.getLineAndCharacterFromPosition(node.getStart(this.sourceFile)).line; var endLine = this.sourceFile.getLineAndCharacterFromPosition(node.getEnd()).line; //var startLine = this.snapshot.getLineNumberFromPosition(node.start()); @@ -111,7 +109,7 @@ module ts.formatting { // Now we know we have a block (or a fake block represented by some other kind of node with an open and close brace as children). // IMPORTANT!!! This relies on the invariant that IsBlockContext must return true ONLY for nodes with open and close braces as immediate children - public BlockIsOnOneLine(node: Node): boolean { + private BlockIsOnOneLine(node: Node): boolean { var openBrace = findChildOfKind(node, SyntaxKind.OpenBraceToken, this.sourceFile); var closeBrace = findChildOfKind(node, SyntaxKind.CloseBraceToken, this.sourceFile); if (openBrace && closeBrace) { diff --git a/src/services/formatting/new/rules.ts b/src/services/formatting/new/rules.ts index aef884331b7..77030fe7a70 100644 --- a/src/services/formatting/new/rules.ts +++ b/src/services/formatting/new/rules.ts @@ -559,6 +559,11 @@ module ts.formatting { case SyntaxKind.Block: case SyntaxKind.SwitchStatement: case SyntaxKind.ObjectLiteral: + case SyntaxKind.TryBlock: + case SyntaxKind.CatchBlock: + case SyntaxKind.FinallyBlock: + case SyntaxKind.FunctionBlock: + case SyntaxKind.ModuleBlock: return true; } @@ -668,15 +673,34 @@ module ts.formatting { return context.contextNode.kind === SyntaxKind.TypeLiteral;// && context.contextNode.parent.kind !== SyntaxKind.InterfaceDeclaration; } - static IsTypeArgumentOrParameter(tokenKind: SyntaxKind, parentKind: SyntaxKind): boolean { - return; - //return ((tokenKind === SyntaxKind.LessThanToken || tokenKind === SyntaxKind.GreaterThanToken) && + static IsTypeArgumentOrParameter(token: TextRangeWithKind, parent: Node): boolean { + if (token.kind !== SyntaxKind.LessThanToken && token.kind !== SyntaxKind.GreaterThanToken) { + return false; + } + switch (parent.kind) { + case SyntaxKind.TypeReference: + case SyntaxKind.ClassDeclaration: + case SyntaxKind.InterfaceDeclaration: + case SyntaxKind.FunctionDeclaration: + case SyntaxKind.FunctionExpression: + case SyntaxKind.ArrowFunction: + case SyntaxKind.Method: + case SyntaxKind.CallSignature: + case SyntaxKind.ConstructSignature: + case SyntaxKind.CallExpression: + case SyntaxKind.NewExpression: + return true; + default: + return false; + + } + //return ((token.kind === SyntaxKind.LessThanToken || token.kind === SyntaxKind.GreaterThanToken) && // (parentKind === SyntaxKind.TypeParameterList || parentKind === SyntaxKind.TypeArgumentList)); } static IsTypeArgumentOrParameterContext(context: FormattingContext): boolean { - return Rules.IsTypeArgumentOrParameter(context.currentTokenSpan.kind, context.currentTokenParent.kind) || - Rules.IsTypeArgumentOrParameter(context.nextTokenSpan.kind, context.nextTokenParent.kind); + return Rules.IsTypeArgumentOrParameter(context.currentTokenSpan, context.currentTokenParent) || + Rules.IsTypeArgumentOrParameter(context.nextTokenSpan, context.nextTokenParent); } static IsVoidOpContext(context: FormattingContext): boolean {