diff --git a/src/services/formatting/format.ts b/src/services/formatting/format.ts index 00a8227cf52..80f6f531934 100644 --- a/src/services/formatting/format.ts +++ b/src/services/formatting/format.ts @@ -1,5 +1,6 @@ /// /// +/// /// /// @@ -52,39 +53,6 @@ module ts.formatting { return formatSpan(span, sourceFile, options, rulesProvider, FormattingRequestKind.FormatSelection); } - function getEndLinePosition(line: number, sourceFile: SourceFile): number { - var lineStarts = sourceFile.getLineStarts(); - if (line === lineStarts.length - 1) { - // last line - return EOF - return sourceFile.text.length - 1; - } - else { - // current line start - var start = lineStarts[line]; - // take the start position of the next line -1 = it should be some line break - var pos = lineStarts[line + 1] - 1; - Debug.assert(isLineBreak(sourceFile.text.charCodeAt(pos))); - // walk backwards skipping line breaks, stop the the beginning of current line. - // i.e: - // - // $ <- end of line for this position should match the start position - while (start <= pos && isLineBreak(sourceFile.text.charCodeAt(pos))) { - pos--; - } - return pos; - } - } - - function getStartPositionOfLine(line: number, sourceFile: SourceFile): number { - return sourceFile.getLineStarts()[line]; - } - - function getStartLinePositionForPosition(position: number, sourceFile: SourceFile): number { - var lineStarts = sourceFile.getLineStarts(); - var line = getNonAdjustedLineAndCharacterFromPosition(position, sourceFile).line; - return lineStarts[line]; - } - function formatOutermostParent(position: number, expectedLastToken: SyntaxKind, sourceFile: SourceFile, options: FormatCodeOptions, rulesProvider: RulesProvider, requestKind: FormattingRequestKind): TextChange[]{ var parent = findOutermostParent(position, expectedLastToken, sourceFile); if (!parent) { @@ -115,20 +83,6 @@ module ts.formatting { return current; } - function rangeContainsRange(initial: TextRange, candidate: TextRange): boolean { - return startEndContainsRange(initial.pos, initial.end, candidate); - } - - function startEndContainsRange(start: number, end: number, candidate: TextRange): boolean { - return start <= candidate.pos && end >= candidate.end; - } - - function rangeOverlapsWithRange(r1: TextRange, r2: TextRange): boolean { - var start = Math.max(r1.pos, r2.pos); - var end = Math.min(r1.end, r2.end); - return start < end; - } - function isListElement(parent: Node, node: Node): boolean { switch (parent.kind) { case SyntaxKind.ClassDeclaration: @@ -156,16 +110,6 @@ module ts.formatting { } } - function getIndentationForNode(n: Node, ignoreActualIndentationRange: TextRange, sourceFile: SourceFile, options: FormatCodeOptions): number { - var start = sourceFile.getLineAndCharacterFromPosition(n.getStart(sourceFile)); - return SmartIndenter.getIndentationForNode(n, start, ignoreActualIndentationRange, /*indentationDelta*/ 0, sourceFile, options); - } - - function getNonAdjustedLineAndCharacterFromPosition(position: number, sourceFile: SourceFile): LineAndCharacter { - var lineAndChar = sourceFile.getLineAndCharacterFromPosition(position); - return { line: lineAndChar.line - 1, character: lineAndChar.character - 1 }; - } - function formatSpan(originalRange: TextRange, sourceFile: SourceFile, options: FormatCodeOptions, @@ -176,7 +120,7 @@ module ts.formatting { var formattingContext = new FormattingContext(sourceFile, requestKind); var enclosingNode = findEnclosingNode(originalRange, sourceFile); - var initialIndentation = getIndentationForNode(enclosingNode, originalRange, sourceFile, options); + var initialIndentation = SmartIndenter.getIndentationForNode(enclosingNode, originalRange, sourceFile, options); var formattingScanner = getFormattingScanner(sourceFile, enclosingNode, originalRange); diff --git a/src/services/formatting/lineMapUtilities.ts b/src/services/formatting/lineMapUtilities.ts new file mode 100644 index 00000000000..c5dfa5dae52 --- /dev/null +++ b/src/services/formatting/lineMapUtilities.ts @@ -0,0 +1,42 @@ +/// + +module ts.formatting { + + export function getEndLinePosition(line: number, sourceFile: SourceFile): number { + var lineStarts = sourceFile.getLineStarts(); + if (line === lineStarts.length - 1) { + // last line - return EOF + return sourceFile.text.length - 1; + } + else { + // current line start + var start = lineStarts[line]; + // take the start position of the next line -1 = it should be some line break + var pos = lineStarts[line + 1] - 1; + Debug.assert(isLineBreak(sourceFile.text.charCodeAt(pos))); + // walk backwards skipping line breaks, stop the the beginning of current line. + // i.e: + // + // $ <- end of line for this position should match the start position + while (start <= pos && isLineBreak(sourceFile.text.charCodeAt(pos))) { + pos--; + } + return pos; + } + } + + export function getNonAdjustedLineAndCharacterFromPosition(position: number, sourceFile: SourceFile): LineAndCharacter { + var lineAndChar = sourceFile.getLineAndCharacterFromPosition(position); + return { line: lineAndChar.line - 1, character: lineAndChar.character - 1 }; + } + + export function getStartPositionOfLine(line: number, sourceFile: SourceFile): number { + return sourceFile.getLineStarts()[line]; + } + + export function getStartLinePositionForPosition(position: number, sourceFile: SourceFile): number { + var lineStarts = sourceFile.getLineStarts(); + var line = getNonAdjustedLineAndCharacterFromPosition(position, sourceFile).line; + return lineStarts[line]; + } +} \ No newline at end of file diff --git a/src/services/formatting/smartIndenter.ts b/src/services/formatting/smartIndenter.ts index 57f49bc43ca..69fbf71df18 100644 --- a/src/services/formatting/smartIndenter.ts +++ b/src/services/formatting/smartIndenter.ts @@ -65,10 +65,15 @@ module ts.formatting { return 0; } - return getIndentationForNode(current, currentStart, /*ignoreActualIndentationRange*/ undefined, indentationDelta, sourceFile, options); + return getIndentationForNodeWorker(current, currentStart, /*ignoreActualIndentationRange*/ undefined, indentationDelta, sourceFile, options); } - export function getIndentationForNode( + export function getIndentationForNode(n: Node, ignoreActualIndentationRange: TextRange, sourceFile: SourceFile, options: FormatCodeOptions): number { + var start = sourceFile.getLineAndCharacterFromPosition(n.getStart(sourceFile)); + return getIndentationForNodeWorker(n, start, ignoreActualIndentationRange, /*indentationDelta*/ 0, sourceFile, options); + } + + function getIndentationForNodeWorker( current: Node, currentStart: LineAndCharacter, ignoreActualIndentationRange: TextRange, diff --git a/src/services/utilities.ts b/src/services/utilities.ts index 6965685884c..82a9a85d7ae 100644 --- a/src/services/utilities.ts +++ b/src/services/utilities.ts @@ -5,6 +5,20 @@ module ts { list: Node; } + export function rangeContainsRange(r1: TextRange, r2: TextRange): boolean { + return startEndContainsRange(r1.pos, r1.end, r2); + } + + export function startEndContainsRange(start: number, end: number, range: TextRange): boolean { + return start <= range.pos && end >= range.end; + } + + export function rangeOverlapsWithRange(r1: TextRange, r2: TextRange): boolean { + var start = Math.max(r1.pos, r2.pos); + var end = Math.min(r1.end, r2.end); + return start < end; + } + export function findListItemInfo(node: Node): ListItemInfo { var syntaxList = findContainingList(node); var children = syntaxList.getChildren();