From f962aba24abe1d04c36551c193bcd12b0e0161b5 Mon Sep 17 00:00:00 2001 From: uniqueiniquity Date: Mon, 30 Oct 2017 16:44:44 -0700 Subject: [PATCH 1/5] Indent all lines of single JsxText node --- src/services/formatting/formatting.ts | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/services/formatting/formatting.ts b/src/services/formatting/formatting.ts index 3808cb7894..18f463b9fa 100644 --- a/src/services/formatting/formatting.ts +++ b/src/services/formatting/formatting.ts @@ -714,6 +714,11 @@ namespace ts.formatting { processNode(child, childContextNode, childStartLine, undecoratedChildStartLine, childIndentation.indentation, childIndentation.delta); + if (child.kind === SyntaxKind.JsxText) { + const range = { pos: child.getStart(), end: child.getEnd() }; + indentMultilineCommentOrJsxText(range, childIndentation.indentation, /*firstLineIsIndented*/ true, /*indentFinalLine*/ false); + } + childContextNode = node; if (isFirstListItem && parent.kind === SyntaxKind.ArrayLiteralExpression && inheritedIndentation === Constants.Unknown) { @@ -833,7 +838,7 @@ namespace ts.formatting { switch (triviaItem.kind) { case SyntaxKind.MultiLineCommentTrivia: if (triviaInRange) { - indentMultilineComment(triviaItem, commentIndentation, /*firstLineIsIndented*/ !indentNextTokenOrTrivia); + indentMultilineCommentOrJsxText(triviaItem, commentIndentation, /*firstLineIsIndented*/ !indentNextTokenOrTrivia); } indentNextTokenOrTrivia = false; break; @@ -985,7 +990,7 @@ namespace ts.formatting { return indentationString !== sourceFile.text.substr(startLinePosition, indentationString.length); } - function indentMultilineComment(commentRange: TextRange, indentation: number, firstLineIsIndented: boolean) { + function indentMultilineCommentOrJsxText(commentRange: TextRange, indentation: number, firstLineIsIndented: boolean, indentFinalLine = true) { // split comment in lines let startLine = sourceFile.getLineAndCharacterOfPosition(commentRange.pos).line; const endLine = sourceFile.getLineAndCharacterOfPosition(commentRange.end).line; @@ -1006,7 +1011,9 @@ namespace ts.formatting { startPos = getStartPositionOfLine(line + 1, sourceFile); } - parts.push({ pos: startPos, end: commentRange.end }); + if (indentFinalLine) { + parts.push({ pos: startPos, end: commentRange.end }); + } } const startLinePos = getStartPositionOfLine(startLine, sourceFile); From 9b9032f8c55f87ad3f47f93bb25e4d0b0bc2538d Mon Sep 17 00:00:00 2001 From: uniqueiniquity Date: Mon, 30 Oct 2017 17:09:20 -0700 Subject: [PATCH 2/5] Add JSXText indentation test --- tests/cases/fourslash/indentationInJsx3.ts | 31 ++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 tests/cases/fourslash/indentationInJsx3.ts diff --git a/tests/cases/fourslash/indentationInJsx3.ts b/tests/cases/fourslash/indentationInJsx3.ts new file mode 100644 index 0000000000..3c75356a12 --- /dev/null +++ b/tests/cases/fourslash/indentationInJsx3.ts @@ -0,0 +1,31 @@ +/// + +//@Filename: file.tsx +////function foo () { +//// return ( +////
+////hello +////goodbye +////
+//// ) +////} + +goTo.position(21); +verify.textAtCaretIs("return"); +goTo.position(38); +verify.textAtCaretIs("
"); +goTo.position(44); +verify.textAtCaretIs("hello"); +goTo.position(50); +verify.textAtCaretIs("goodbye"); + +format.document(); + +goTo.position(21); +verify.textAtCaretIs("return"); +goTo.position(38); +verify.textAtCaretIs("
"); +goTo.position(56); +verify.textAtCaretIs("hello"); +goTo.position(74); +verify.textAtCaretIs("goodbye"); \ No newline at end of file From b6ea2f955a90f7e2e45278af040dceb452d7b0e5 Mon Sep 17 00:00:00 2001 From: uniqueiniquity Date: Tue, 31 Oct 2017 14:52:15 -0700 Subject: [PATCH 3/5] Refactor test and annotate object literal --- src/services/formatting/formatting.ts | 2 +- tests/cases/fourslash/indentationInJsx3.ts | 28 ++++++++-------------- 2 files changed, 11 insertions(+), 19 deletions(-) diff --git a/src/services/formatting/formatting.ts b/src/services/formatting/formatting.ts index 18f463b9fa..fdd70cda46 100644 --- a/src/services/formatting/formatting.ts +++ b/src/services/formatting/formatting.ts @@ -715,7 +715,7 @@ namespace ts.formatting { processNode(child, childContextNode, childStartLine, undecoratedChildStartLine, childIndentation.indentation, childIndentation.delta); if (child.kind === SyntaxKind.JsxText) { - const range = { pos: child.getStart(), end: child.getEnd() }; + const range: TextRange = { pos: child.getStart(), end: child.getEnd() }; indentMultilineCommentOrJsxText(range, childIndentation.indentation, /*firstLineIsIndented*/ true, /*indentFinalLine*/ false); } diff --git a/tests/cases/fourslash/indentationInJsx3.ts b/tests/cases/fourslash/indentationInJsx3.ts index 3c75356a12..1c93cbc24b 100644 --- a/tests/cases/fourslash/indentationInJsx3.ts +++ b/tests/cases/fourslash/indentationInJsx3.ts @@ -4,28 +4,20 @@ ////function foo () { //// return ( ////
-////hello -////goodbye +/////*0*/hello +/////*1*/goodbye ////
//// ) ////} -goTo.position(21); -verify.textAtCaretIs("return"); -goTo.position(38); -verify.textAtCaretIs("
"); -goTo.position(44); -verify.textAtCaretIs("hello"); -goTo.position(50); -verify.textAtCaretIs("goodbye"); +goTo.marker('0'); +verify.currentLineContentIs("hello"); +goTo.marker('1'); +verify.currentLineContentIs("goodbye"); format.document(); -goTo.position(21); -verify.textAtCaretIs("return"); -goTo.position(38); -verify.textAtCaretIs("
"); -goTo.position(56); -verify.textAtCaretIs("hello"); -goTo.position(74); -verify.textAtCaretIs("goodbye"); \ No newline at end of file +goTo.marker('0'); +verify.currentLineContentIs(" hello"); +goTo.marker('1'); +verify.currentLineContentIs(" goodbye"); \ No newline at end of file From 20e1f5258b552f62ece82915aa0cef3a92fe90b4 Mon Sep 17 00:00:00 2001 From: uniqueiniquity Date: Tue, 31 Oct 2017 16:09:30 -0700 Subject: [PATCH 4/5] Update test --- tests/cases/fourslash/indentationInJsx3.ts | 30 +++++++++++++++------- 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/tests/cases/fourslash/indentationInJsx3.ts b/tests/cases/fourslash/indentationInJsx3.ts index 1c93cbc24b..b5d5e77221 100644 --- a/tests/cases/fourslash/indentationInJsx3.ts +++ b/tests/cases/fourslash/indentationInJsx3.ts @@ -1,7 +1,7 @@ /// //@Filename: file.tsx -////function foo () { +////function foo() { //// return ( ////
/////*0*/hello @@ -10,14 +10,26 @@ //// ) ////} -goTo.marker('0'); -verify.currentLineContentIs("hello"); -goTo.marker('1'); -verify.currentLineContentIs("goodbye"); +verify.currentFileContentIs( +`function foo() { + return ( +
+hello +goodbye +
+ ) +}` +); format.document(); -goTo.marker('0'); -verify.currentLineContentIs(" hello"); -goTo.marker('1'); -verify.currentLineContentIs(" goodbye"); \ No newline at end of file +verify.currentFileContentIs( +`function foo() { + return ( +
+ hello + goodbye +
+ ) +}` +); \ No newline at end of file From 9f68ff5b0f700084cd02dfe93db56438dfc58bb5 Mon Sep 17 00:00:00 2001 From: uniqueiniquity Date: Tue, 31 Oct 2017 16:10:17 -0700 Subject: [PATCH 5/5] Remove markers --- tests/cases/fourslash/indentationInJsx3.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/cases/fourslash/indentationInJsx3.ts b/tests/cases/fourslash/indentationInJsx3.ts index b5d5e77221..0c57b3b31e 100644 --- a/tests/cases/fourslash/indentationInJsx3.ts +++ b/tests/cases/fourslash/indentationInJsx3.ts @@ -4,8 +4,8 @@ ////function foo() { //// return ( ////
-/////*0*/hello -/////*1*/goodbye +////hello +////goodbye ////
//// ) ////}