From 8960ab97129d8f135131f8351e55ead70a5f0b6b Mon Sep 17 00:00:00 2001 From: Vladimir Matveev Date: Mon, 10 Nov 2014 13:30:00 -0800 Subject: [PATCH] addressed CR feedback, added comments, dropped unused code --- src/compiler/types.ts | 8 +++++++ src/services/formatting.ts | 39 +++++++++++++++++++++++++++++------ src/services/services.ts | 31 +--------------------------- src/services/smartIndenter.ts | 2 +- 4 files changed, 43 insertions(+), 37 deletions(-) diff --git a/src/compiler/types.ts b/src/compiler/types.ts index c69944f88a7..c30afb9360a 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -1124,7 +1124,15 @@ module ts { messageText: string; category: DiagnosticCategory; code: number; + /** + * Early error - any error (can be produced at parsing\binding\typechecking step) that blocks emit + */ isEarly?: boolean; + /** + * Parse error - error produced by parser when it scanner returns a token + * that parser does not understand in its current state + * (as opposed to grammar error when parser can interpret the token but interpretation is not legal from the grammar perespective) + */ isParseError?: boolean; } diff --git a/src/services/formatting.ts b/src/services/formatting.ts index f4acd193b02..7b689544724 100644 --- a/src/services/formatting.ts +++ b/src/services/formatting.ts @@ -26,13 +26,24 @@ module ts.formatting { * { var x; * } * Normally indentation is applied only to the first token in line so at glance 'var' should not be touched. - * However if some format rule removes new line between ')' and '{' 'var' will become + * However if some format rule adds new line between '}' and 'var' 'var' will become * the first token in line so it should be indented */ interface DynamicIndentation { getIndentationForToken(tokenLine: number, tokenKind: SyntaxKind): number; getIndentationForComment(owningToken: SyntaxKind): number; + /** + * Indentation for open and close tokens of the node if it is block or another node that needs special indentation + * ... { + * ......... + * ....} + * ____ - indentation + * ____ - delta + **/ getIndentation(): number; + /** + * Prefered relative indentation for child nodes + */ getDelta(): number; recomputeIndentation(lineAddedByFormatting: boolean): void; } @@ -99,6 +110,14 @@ module ts.formatting { } // walk up and search for the parent node that ends at the same position with precedingToken. + // for cases like this + // + // var x = 1; + // while (true) { + // } + // after typing close curly in while statement we want to reformat just the while statement. + // However if we just walk upwards searching for the parent that has the same end value - + // we'll end up with the whole source file. isListElement allows to stop on the list element level var current = precedingToken; while (current && current.parent && @@ -138,7 +157,14 @@ module ts.formatting { function find(n: Node): Node { var candidate = forEachChild(n, c => startEndContainsRange(c.getStart(sourceFile), c.end, range) && c); - return (candidate && find(candidate)) || n; + if (candidate) { + var result = find(candidate); + if (result) { + return result; + } + } + + return n; } } @@ -230,9 +256,11 @@ module ts.formatting { // local functions /** Tries to compute the indentation for a list element. - * If list element is not in range then function will pick its actual indentation + * If list element is not in range then + * function will pick its actual indentation * so it can be pushed downstream as inherited indentation. - * If list element is in the range - its indentation will be equal to inherited indentation from its predecessors. + * If list element is in the range - its indentation will be equal + * to inherited indentation from its predecessors. */ function tryComputeIndentationForListItem(startPos: number, endPos: number, @@ -440,8 +468,7 @@ module ts.formatting { var childStartPos = child.getStart(sourceFile); var childStart = sourceFile.getLineAndCharacterFromPosition(childStartPos); - var childIndentationAmount = - isListItem + var childIndentationAmount = isListItem ? tryComputeIndentationForListItem(childStartPos, child.end, parentStartLine, originalRange, inheritedIndentation) : Constants.Unknown; diff --git a/src/services/services.ts b/src/services/services.ts index ac2e9fec42e..292b7dc8780 100644 --- a/src/services/services.ts +++ b/src/services/services.ts @@ -959,15 +959,6 @@ module ts { ConvertTabsToSpaces: boolean; } - export function copyEditorOptions(o: EditorOptions): EditorOptions { - return { - IndentSize: o.IndentSize, - TabSize: o.TabSize, - NewLineCharacter: o.NewLineCharacter, - ConvertTabsToSpaces: o.ConvertTabsToSpaces - }; - } - export interface FormatCodeOptions extends EditorOptions { InsertSpaceAfterCommaDelimiter: boolean; InsertSpaceAfterSemicolonInForStatements: boolean; @@ -979,23 +970,6 @@ module ts { PlaceOpenBraceOnNewLineForControlBlocks: boolean; } - export function copyFormatCodeOptions(o: FormatCodeOptions): FormatCodeOptions { - return { - IndentSize: o.IndentSize, - TabSize: o.TabSize, - NewLineCharacter: o.NewLineCharacter, - ConvertTabsToSpaces: o.ConvertTabsToSpaces, - InsertSpaceAfterCommaDelimiter: o.InsertSpaceAfterCommaDelimiter, - InsertSpaceAfterSemicolonInForStatements: o.InsertSpaceAfterSemicolonInForStatements, - InsertSpaceBeforeAndAfterBinaryOperators: o.InsertSpaceBeforeAndAfterBinaryOperators, - InsertSpaceAfterKeywordsInControlFlowStatements: o.InsertSpaceAfterKeywordsInControlFlowStatements, - InsertSpaceAfterFunctionKeywordForAnonymousFunctions: o.InsertSpaceAfterFunctionKeywordForAnonymousFunctions, - InsertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis: o.InsertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis, - PlaceOpenBraceOnNewLineForFunctions: o.PlaceOpenBraceOnNewLineForFunctions, - PlaceOpenBraceOnNewLineForControlBlocks: o.PlaceOpenBraceOnNewLineForControlBlocks - }; - } - export interface DefinitionInfo { fileName: string; textSpan: TypeScript.TextSpan; @@ -5224,7 +5198,7 @@ module ts { var start = new Date().getTime(); - var result = formatting.SmartIndenter.getIndentation(position, sourceFile, copyEditorOptions(editorOptions)); + var result = formatting.SmartIndenter.getIndentation(position, sourceFile, editorOptions); host.log("getIndentationAtPosition: computeIndentation : " + (new Date().getTime() - start)); return result; @@ -5232,7 +5206,6 @@ module ts { function getFormattingEditsForRange(fileName: string, start: number, end: number, options: FormatCodeOptions): TextChange[] { fileName = switchToForwardSlashes(fileName); - var options = copyFormatCodeOptions(options); var sourceFile = getCurrentSourceFile(fileName); return formatting.formatSelection(start, end, sourceFile, getRuleProvider(options), options); } @@ -5241,7 +5214,6 @@ module ts { fileName = switchToForwardSlashes(fileName); var sourceFile = getCurrentSourceFile(fileName); - var options = copyFormatCodeOptions(options) return formatting.formatDocument(sourceFile, getRuleProvider(options), options); } @@ -5249,7 +5221,6 @@ module ts { fileName = switchToForwardSlashes(fileName); var sourceFile = getCurrentSourceFile(fileName); - var options = copyFormatCodeOptions(options); if (key === "}") { return formatting.formatOnClosingCurly(position, sourceFile, getRuleProvider(options), options); diff --git a/src/services/smartIndenter.ts b/src/services/smartIndenter.ts index 97539b12a03..8e3208c21e5 100644 --- a/src/services/smartIndenter.ts +++ b/src/services/smartIndenter.ts @@ -298,7 +298,7 @@ module ts.formatting { return column; } - export function nodeContentIsAlwaysIndented(kind: SyntaxKind): boolean { + function nodeContentIsAlwaysIndented(kind: SyntaxKind): boolean { switch (kind) { case SyntaxKind.ClassDeclaration: case SyntaxKind.InterfaceDeclaration: