Skip to content

Commit d119de8

Browse files
committed
added support for smart indentation in the middle of list items, updated test baselines
1 parent e9227d6 commit d119de8

File tree

1 file changed

+23
-17
lines changed

1 file changed

+23
-17
lines changed

src/services/formatting/smartIndenter.ts

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ module ts.formatting {
2929
var listStartLine = getStartLineForNode(precedingListItem.parent, sourceFile);
3030

3131
if (precedingListItemStartLineAndChar.line !== listStartLine) {
32-
32+
return findFirstNonWhitespaceCharacterInLine(precedingListItemStartLineAndChar.line, precedingListItemStartLineAndChar.character, sourceFile);
3333
// previous list item starts on the different line with list, find first non-whitespace character in this line and use its position as indentation
3434
var lineStartPosition = sourceFile.getPositionFromLineAndCharacter(precedingListItemStartLineAndChar.line, 1);
3535
for (var i = 0; i < precedingListItemStartLineAndChar.character; ++i) {
@@ -52,18 +52,13 @@ module ts.formatting {
5252

5353
while (current) {
5454
if (isPositionBelongToNode(current, position, sourceFile)) {
55-
5655
currentStartLine = getStartLineForNode(current, sourceFile);
5756

5857
if (discardInitialIndentationIfNextTokenIsOpenOrCloseBrace(precedingToken, current, lineAtPosition, sourceFile)) {
5958
indentation = 0;
6059
}
6160
else {
62-
indentation =
63-
isNodeContentIndented(current, previous) &&
64-
lineAtPosition !== currentStartLine
65-
? options.indentSpaces
66-
: 0;
61+
indentation = isNodeContentIndented(current, previous) && lineAtPosition !== currentStartLine ? options.indentSpaces : 0;
6762
}
6863

6964
break;
@@ -73,6 +68,12 @@ module ts.formatting {
7368
return customIndentation;
7469
}
7570

71+
// check if current node is a list item - if yes, take indentation from it
72+
var customIndentation = getCustomIndentationForListItem(current, sourceFile);
73+
if (customIndentation !== -1) {
74+
return customIndentation;
75+
}
76+
7677
previous = current;
7778
current = current.parent;
7879
}
@@ -89,12 +90,15 @@ module ts.formatting {
8990
// walk upwards and collect indentations for pairs of parent-child nodes
9091
// indentation is not added if parent and child nodes start on the same line or if parent is IfStatement and child starts on the same line with 'else clause'
9192
while (parent) {
93+
94+
// check if current node is a list item - if yes, take indentation from it
9295
var customIndentation = getCustomIndentationForListItem(current, sourceFile);
9396
if (customIndentation !== -1) {
9497
return customIndentation + indentation;
9598
}
9699

97100
parentStartLine = sourceFile.getLineAndCharacterFromPosition(parent.getStart(sourceFile)).line;
101+
// increase indentation if parent node wants its content to be indented and parent and child nodes don't start on the same line
98102
var increaseIndentation =
99103
isNodeContentIndented(parent, current) &&
100104
parentStartLine !== currentStartLine &&
@@ -218,16 +222,7 @@ module ts.formatting {
218222
for (var i = index - 1; i >= 0; --i) {
219223
var prevLineAndCol = sourceFile.getLineAndCharacterFromPosition(list[i].getStart(sourceFile));
220224
if (lineAndCol.line !== prevLineAndCol.line) {
221-
// find the line start position
222-
var lineStart = sourceFile.getPositionFromLineAndCharacter(lineAndCol.line, 1);
223-
for (var i = 0; i <= lineAndCol.character; ++i) {
224-
if (!isWhiteSpace(sourceFile.text.charCodeAt(lineStart + i))) {
225-
return i;
226-
}
227-
}
228-
// code is unreachable because the range that we check above includes at least one non-whitespace character at the very end
229-
Debug.fail("Unreachable code")
230-
225+
return findFirstNonWhitespaceCharacterInLine(lineAndCol.line, lineAndCol.character, sourceFile);
231226
}
232227
lineAndCol = prevLineAndCol;
233228
}
@@ -236,6 +231,17 @@ module ts.formatting {
236231
}
237232
}
238233

234+
function findFirstNonWhitespaceCharacterInLine(line: number, maxCharacter: number, sourceFile: SourceFile): number {
235+
var lineStart = sourceFile.getPositionFromLineAndCharacter(line, 1);
236+
for (var i = 0; i < maxCharacter; ++i) {
237+
if (!isWhiteSpace(sourceFile.text.charCodeAt(lineStart + i))) {
238+
return i;
239+
}
240+
}
241+
242+
return maxCharacter;
243+
}
244+
239245
function findNextToken(previousToken: Node, parent: Node): Node {
240246
return find(parent);
241247

0 commit comments

Comments
 (0)