Merge pull request #2117 from Microsoft/formattingTabsInMultilineComments

use character instead of column when formatting multiline comments with ...
This commit is contained in:
Vladimir Matveev
2015-02-24 09:57:14 -08:00
6 changed files with 127 additions and 11 deletions

View File

@@ -1480,6 +1480,16 @@ module FourSlash {
return runningOffset;
}
public copyFormatOptions(): ts.FormatCodeOptions {
return ts.clone(this.formatCodeOptions);
}
public setFormatOptions(formatCodeOptions: ts.FormatCodeOptions): ts.FormatCodeOptions {
var oldFormatCodeOptions = this.formatCodeOptions;
this.formatCodeOptions = formatCodeOptions;
return oldFormatCodeOptions;
}
public formatDocument() {
this.scenarioActions.push('<FormatDocument />');

View File

@@ -842,9 +842,9 @@ module ts.formatting {
var startLinePos = getStartPositionOfLine(startLine, sourceFile);
var nonWhitespaceColumnInFirstPart =
SmartIndenter.findFirstNonWhitespaceColumn(startLinePos, parts[0].pos, sourceFile, options);
SmartIndenter.findFirstNonWhitespaceCharacterAndColumn(startLinePos, parts[0].pos, sourceFile, options);
if (indentation === nonWhitespaceColumnInFirstPart) {
if (indentation === nonWhitespaceColumnInFirstPart.column) {
return;
}
@@ -855,21 +855,21 @@ module ts.formatting {
}
// shift all parts on the delta size
var delta = indentation - nonWhitespaceColumnInFirstPart;
var delta = indentation - nonWhitespaceColumnInFirstPart.column;
for (var i = startIndex, len = parts.length; i < len; ++i, ++startLine) {
var startLinePos = getStartPositionOfLine(startLine, sourceFile);
var nonWhitespaceColumn =
var nonWhitespaceCharacterAndColumn =
i === 0
? nonWhitespaceColumnInFirstPart
: SmartIndenter.findFirstNonWhitespaceColumn(parts[i].pos, parts[i].end, sourceFile, options);
: SmartIndenter.findFirstNonWhitespaceCharacterAndColumn(parts[i].pos, parts[i].end, sourceFile, options);
var newIndentation = nonWhitespaceColumn + delta;
var newIndentation = nonWhitespaceCharacterAndColumn.column + delta;
if (newIndentation > 0) {
var indentationString = getIndentationString(newIndentation, options);
recordReplace(startLinePos, nonWhitespaceColumn, indentationString);
recordReplace(startLinePos, nonWhitespaceCharacterAndColumn.character, indentationString);
}
else {
recordDelete(startLinePos, nonWhitespaceColumn);
recordDelete(startLinePos, nonWhitespaceCharacterAndColumn.character);
}
}
}

View File

@@ -306,12 +306,20 @@ module ts.formatting {
return findFirstNonWhitespaceColumn(lineStart, lineStart + lineAndCharacter.character, sourceFile, options);
}
export function findFirstNonWhitespaceColumn(startPos: number, endPos: number, sourceFile: SourceFile, options: EditorOptions): number {
/*
Character is the actual index of the character since the beginning of the line.
Column - position of the character after expanding tabs to spaces
"0\t2$"
value of 'character' for '$' is 3
value of 'column' for '$' is 6 (assuming that tab size is 4)
*/
export function findFirstNonWhitespaceCharacterAndColumn(startPos: number, endPos: number, sourceFile: SourceFile, options: EditorOptions) {
var character = 0;
var column = 0;
for (var pos = startPos; pos < endPos; ++pos) {
var ch = sourceFile.text.charCodeAt(pos);
if (!isWhiteSpace(ch)) {
return column;
break;
}
if (ch === CharacterCodes.tab) {
@@ -320,8 +328,14 @@ module ts.formatting {
else {
column++;
}
character++;
}
return column;
return { column, character };
}
export function findFirstNonWhitespaceColumn(startPos: number, endPos: number, sourceFile: SourceFile, options: EditorOptions): number {
return findFirstNonWhitespaceCharacterAndColumn(startPos, endPos, sourceFile, options).column;
}
function nodeContentIsAlwaysIndented(kind: SyntaxKind): boolean {