Use simpler indentation for comments

* When in a multi-line comment, we would have liked to use the start of
the comment as a reference point for the indentation inside the comment,
but determining the number of columns shifted for the comment start
woudl require determining the length w/r/t graphemes, which we do not
currently implement. We would like to avoid taking on a runtime
dependency on a grapheme-parsing library.

Instead, we look at the indentation level on the previoud line or start
of the comment as a reference point, and correct shift for lines
starting with an asterisk.
This commit is contained in:
Arthur Ozga
2017-08-16 17:51:29 -07:00
parent 62f16bee55
commit 23ca368020
2 changed files with 22 additions and 20 deletions

View File

@@ -1152,23 +1152,6 @@ namespace ts.formatting {
}
}
/**
* Gets the indentation level of the multi-line comment enclosing position,
* and a negative value if the position is not in a multi-line comment.
*
* @param precedingToken Must be the result of `findPrecedingToken(position, sourceFile)`.
*/
export function getIndentationOfEnclosingMultiLineComment(sourceFile: SourceFile, position: number, precedingToken: Node | undefined, options: EditorSettings): number {
const range = getRangeOfEnclosingComment(sourceFile, position, /*onlyMultiLine*/ true, precedingToken || null); // tslint:disable-line:no-null-keyword
if (range) {
const commentStart = range.pos;
const commentLineStart = getLineStartPositionForPosition(commentStart, sourceFile);
const { column, character } = SmartIndenter.findFirstNonWhitespaceCharacterAndColumn(commentLineStart, commentStart, sourceFile, options);
return column + /*length after whitespace ends*/ range.pos - (commentLineStart + character);
}
return -1;
}
/**
* @param precedingToken pass `null` if preceding token was already computed and result was `undefined`.
*/

View File

@@ -32,9 +32,28 @@ namespace ts.formatting {
}
const precedingToken = findPrecedingToken(position, sourceFile);
const indentationOfEnclosingMultiLineComment = getIndentationOfEnclosingMultiLineComment(sourceFile, position, precedingToken, options);
if (indentationOfEnclosingMultiLineComment >= 0) {
return indentationOfEnclosingMultiLineComment;
const enclosingCommentRange = getRangeOfEnclosingComment(sourceFile, position, /*onlyMultiLine*/ true, precedingToken || null); // tslint:disable-line:no-null-keyword
if (enclosingCommentRange) {
const previousLine = getLineAndCharacterOfPosition(sourceFile, position).line - 1;
const commentStartLine = getLineAndCharacterOfPosition(sourceFile, enclosingCommentRange.pos).line;
Debug.assert(commentStartLine >= 0);
if (previousLine <= commentStartLine) {
return findFirstNonWhitespaceColumn(getStartPositionOfLine(commentStartLine, sourceFile), position, sourceFile, options);
}
// get first character of previous line -- if it is '*', move back one more character (or stay at 0)
const startPostionOfLine = getStartPositionOfLine(previousLine, sourceFile);
const { column, character } = findFirstNonWhitespaceCharacterAndColumn(startPostionOfLine, position, sourceFile, options);
if (column === 0) {
return column;
}
const firstNonWhitespaceCharacterCode = sourceFile.text.charCodeAt(startPostionOfLine + character);
return firstNonWhitespaceCharacterCode === CharacterCodes.asterisk ? column - 1 : column;
}
if (!precedingToken) {