Skip to content

Commit c155de7

Browse files
author
zhengbli
committed
Avoid removing indentation on a new line as trailing white spaces
1 parent 8a72229 commit c155de7

1 file changed

Lines changed: 7 additions & 5 deletions

File tree

src/services/formatting/formatting.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,14 @@ namespace ts.formatting {
7272
if (line === 0) {
7373
return [];
7474
}
75-
// get the span for the previous\current line
75+
// After the enter key, the cursor is now at a new line. The new line should not be formatted,
76+
// otherwise the indentation would be treated as trailing whitespaces and removed. The previous
77+
// line should be formatted, and the one before that should be used as reference.
7678
let span = {
77-
// get start position for the previous line
78-
pos: getStartPositionOfLine(line - 1, sourceFile),
79-
// get end position for the current line (end value is exclusive so add 1 to the result)
80-
end: getEndLinePosition(line, sourceFile) + 1
79+
// get start position for the line before previous line
80+
pos: getStartPositionOfLine(line - 2, sourceFile),
81+
// get end position for the previous line (end value is exclusive so add 1 to the result)
82+
end: getEndLinePosition(line - 1, sourceFile) + 1
8183
}
8284
return formatSpan(span, sourceFile, options, rulesProvider, FormattingRequestKind.FormatOnEnter);
8385
}

0 commit comments

Comments
 (0)