Skip to content

Commit dcc1f14

Browse files
Merge pull request #19619 from uniqueiniquity/indentJsxText
Properly indent JSXText on format document
2 parents 53ad019 + 9f68ff5 commit dcc1f14

File tree

2 files changed

+45
-3
lines changed

2 files changed

+45
-3
lines changed

src/services/formatting/formatting.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -714,6 +714,11 @@ namespace ts.formatting {
714714

715715
processNode(child, childContextNode, childStartLine, undecoratedChildStartLine, childIndentation.indentation, childIndentation.delta);
716716

717+
if (child.kind === SyntaxKind.JsxText) {
718+
const range: TextRange = { pos: child.getStart(), end: child.getEnd() };
719+
indentMultilineCommentOrJsxText(range, childIndentation.indentation, /*firstLineIsIndented*/ true, /*indentFinalLine*/ false);
720+
}
721+
717722
childContextNode = node;
718723

719724
if (isFirstListItem && parent.kind === SyntaxKind.ArrayLiteralExpression && inheritedIndentation === Constants.Unknown) {
@@ -833,7 +838,7 @@ namespace ts.formatting {
833838
switch (triviaItem.kind) {
834839
case SyntaxKind.MultiLineCommentTrivia:
835840
if (triviaInRange) {
836-
indentMultilineComment(triviaItem, commentIndentation, /*firstLineIsIndented*/ !indentNextTokenOrTrivia);
841+
indentMultilineCommentOrJsxText(triviaItem, commentIndentation, /*firstLineIsIndented*/ !indentNextTokenOrTrivia);
837842
}
838843
indentNextTokenOrTrivia = false;
839844
break;
@@ -985,7 +990,7 @@ namespace ts.formatting {
985990
return indentationString !== sourceFile.text.substr(startLinePosition, indentationString.length);
986991
}
987992

988-
function indentMultilineComment(commentRange: TextRange, indentation: number, firstLineIsIndented: boolean) {
993+
function indentMultilineCommentOrJsxText(commentRange: TextRange, indentation: number, firstLineIsIndented: boolean, indentFinalLine = true) {
989994
// split comment in lines
990995
let startLine = sourceFile.getLineAndCharacterOfPosition(commentRange.pos).line;
991996
const endLine = sourceFile.getLineAndCharacterOfPosition(commentRange.end).line;
@@ -1006,7 +1011,9 @@ namespace ts.formatting {
10061011
startPos = getStartPositionOfLine(line + 1, sourceFile);
10071012
}
10081013

1009-
parts.push({ pos: startPos, end: commentRange.end });
1014+
if (indentFinalLine) {
1015+
parts.push({ pos: startPos, end: commentRange.end });
1016+
}
10101017
}
10111018

10121019
const startLinePos = getStartPositionOfLine(startLine, sourceFile);
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
//@Filename: file.tsx
4+
////function foo() {
5+
//// return (
6+
//// <div>
7+
////hello
8+
////goodbye
9+
//// </div>
10+
//// )
11+
////}
12+
13+
verify.currentFileContentIs(
14+
`function foo() {
15+
return (
16+
<div>
17+
hello
18+
goodbye
19+
</div>
20+
)
21+
}`
22+
);
23+
24+
format.document();
25+
26+
verify.currentFileContentIs(
27+
`function foo() {
28+
return (
29+
<div>
30+
hello
31+
goodbye
32+
</div>
33+
)
34+
}`
35+
);

0 commit comments

Comments
 (0)