Skip to content

Commit 23ca368

Browse files
committed
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.
1 parent 62f16be commit 23ca368

File tree

2 files changed

+22
-20
lines changed

2 files changed

+22
-20
lines changed

src/services/formatting/formatting.ts

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1152,23 +1152,6 @@ namespace ts.formatting {
11521152
}
11531153
}
11541154

1155-
/**
1156-
* Gets the indentation level of the multi-line comment enclosing position,
1157-
* and a negative value if the position is not in a multi-line comment.
1158-
*
1159-
* @param precedingToken Must be the result of `findPrecedingToken(position, sourceFile)`.
1160-
*/
1161-
export function getIndentationOfEnclosingMultiLineComment(sourceFile: SourceFile, position: number, precedingToken: Node | undefined, options: EditorSettings): number {
1162-
const range = getRangeOfEnclosingComment(sourceFile, position, /*onlyMultiLine*/ true, precedingToken || null); // tslint:disable-line:no-null-keyword
1163-
if (range) {
1164-
const commentStart = range.pos;
1165-
const commentLineStart = getLineStartPositionForPosition(commentStart, sourceFile);
1166-
const { column, character } = SmartIndenter.findFirstNonWhitespaceCharacterAndColumn(commentLineStart, commentStart, sourceFile, options);
1167-
return column + /*length after whitespace ends*/ range.pos - (commentLineStart + character);
1168-
}
1169-
return -1;
1170-
}
1171-
11721155
/**
11731156
* @param precedingToken pass `null` if preceding token was already computed and result was `undefined`.
11741157
*/

src/services/formatting/smartIndenter.ts

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,28 @@ namespace ts.formatting {
3232
}
3333

3434
const precedingToken = findPrecedingToken(position, sourceFile);
35-
const indentationOfEnclosingMultiLineComment = getIndentationOfEnclosingMultiLineComment(sourceFile, position, precedingToken, options);
36-
if (indentationOfEnclosingMultiLineComment >= 0) {
37-
return indentationOfEnclosingMultiLineComment;
35+
36+
const enclosingCommentRange = getRangeOfEnclosingComment(sourceFile, position, /*onlyMultiLine*/ true, precedingToken || null); // tslint:disable-line:no-null-keyword
37+
if (enclosingCommentRange) {
38+
const previousLine = getLineAndCharacterOfPosition(sourceFile, position).line - 1;
39+
const commentStartLine = getLineAndCharacterOfPosition(sourceFile, enclosingCommentRange.pos).line;
40+
41+
Debug.assert(commentStartLine >= 0);
42+
43+
if (previousLine <= commentStartLine) {
44+
return findFirstNonWhitespaceColumn(getStartPositionOfLine(commentStartLine, sourceFile), position, sourceFile, options);
45+
}
46+
47+
// get first character of previous line -- if it is '*', move back one more character (or stay at 0)
48+
const startPostionOfLine = getStartPositionOfLine(previousLine, sourceFile);
49+
const { column, character } = findFirstNonWhitespaceCharacterAndColumn(startPostionOfLine, position, sourceFile, options);
50+
51+
if (column === 0) {
52+
return column;
53+
}
54+
55+
const firstNonWhitespaceCharacterCode = sourceFile.text.charCodeAt(startPostionOfLine + character);
56+
return firstNonWhitespaceCharacterCode === CharacterCodes.asterisk ? column - 1 : column;
3857
}
3958

4059
if (!precedingToken) {

0 commit comments

Comments
 (0)