[PATCH] D33857: Correctly Indent Nested JavaScript Literals.

2017-06-06 Thread Martin Probst via Phabricator via cfe-commits
This revision was automatically updated to reflect the committed changes.
Closed by commit rL304791: clang-format: [JS] Correctly Indent Nested 
JavaScript Literals. (authored by mprobst).

Changed prior to commit:
  https://reviews.llvm.org/D33857?vs=101296&id=101547#toc

Repository:
  rL LLVM

https://reviews.llvm.org/D33857

Files:
  cfe/trunk/lib/Format/ContinuationIndenter.cpp
  cfe/trunk/unittests/Format/FormatTestJS.cpp


Index: cfe/trunk/lib/Format/ContinuationIndenter.cpp
===
--- cfe/trunk/lib/Format/ContinuationIndenter.cpp
+++ cfe/trunk/lib/Format/ContinuationIndenter.cpp
@@ -1036,8 +1036,8 @@
 State.Stack.back().NestedBlockIndent);
   if (Current.isOneOf(tok::l_brace, TT_ArrayInitializerLSquare)) {
 if (Current.opensBlockOrBlockTypeList(Style)) {
-  NewIndent = State.Stack.back().NestedBlockIndent + Style.IndentWidth;
-  NewIndent = std::min(State.Column + 2, NewIndent);
+  NewIndent = Style.IndentWidth +
+  std::min(State.Column, State.Stack.back().NestedBlockIndent);
 } else {
   NewIndent = State.Stack.back().LastSpace + Style.ContinuationIndentWidth;
 }
Index: cfe/trunk/unittests/Format/FormatTestJS.cpp
===
--- cfe/trunk/unittests/Format/FormatTestJS.cpp
+++ cfe/trunk/unittests/Format/FormatTestJS.cpp
@@ -1869,5 +1869,44 @@
   verifyFormat("squared **= 2;");
 }
 
+TEST_F(FormatTestJS, NestedLiterals) {
+  FormatStyle FourSpaces = getGoogleJSStyleWithColumns(15);
+  FourSpaces.IndentWidth = 4;
+  verifyFormat("var l = [\n"
+   "[\n"
+   "1,\n"
+   "],\n"
+   "];", FourSpaces);
+  verifyFormat("var l = [\n"
+   "{\n"
+   "1: 1,\n"
+   "},\n"
+   "];", FourSpaces);
+  verifyFormat("someFunction(\n"
+   "p1,\n"
+   "[\n"
+   "1,\n"
+   "],\n"
+   ");", FourSpaces);
+  verifyFormat("someFunction(\n"
+   "p1,\n"
+   "{\n"
+   "1: 1,\n"
+   "},\n"
+   ");", FourSpaces);
+  verifyFormat("var o = {\n"
+   "1: 1,\n"
+   "2: {\n"
+   "3: 3,\n"
+   "},\n"
+   "};", FourSpaces);
+  verifyFormat("var o = {\n"
+   "1: 1,\n"
+   "2: [\n"
+   "3,\n"
+   "],\n"
+   "};", FourSpaces);
+}
+
 } // end namespace tooling
 } // end namespace clang


Index: cfe/trunk/lib/Format/ContinuationIndenter.cpp
===
--- cfe/trunk/lib/Format/ContinuationIndenter.cpp
+++ cfe/trunk/lib/Format/ContinuationIndenter.cpp
@@ -1036,8 +1036,8 @@
 State.Stack.back().NestedBlockIndent);
   if (Current.isOneOf(tok::l_brace, TT_ArrayInitializerLSquare)) {
 if (Current.opensBlockOrBlockTypeList(Style)) {
-  NewIndent = State.Stack.back().NestedBlockIndent + Style.IndentWidth;
-  NewIndent = std::min(State.Column + 2, NewIndent);
+  NewIndent = Style.IndentWidth +
+  std::min(State.Column, State.Stack.back().NestedBlockIndent);
 } else {
   NewIndent = State.Stack.back().LastSpace + Style.ContinuationIndentWidth;
 }
Index: cfe/trunk/unittests/Format/FormatTestJS.cpp
===
--- cfe/trunk/unittests/Format/FormatTestJS.cpp
+++ cfe/trunk/unittests/Format/FormatTestJS.cpp
@@ -1869,5 +1869,44 @@
   verifyFormat("squared **= 2;");
 }
 
+TEST_F(FormatTestJS, NestedLiterals) {
+  FormatStyle FourSpaces = getGoogleJSStyleWithColumns(15);
+  FourSpaces.IndentWidth = 4;
+  verifyFormat("var l = [\n"
+   "[\n"
+   "1,\n"
+   "],\n"
+   "];", FourSpaces);
+  verifyFormat("var l = [\n"
+   "{\n"
+   "1: 1,\n"
+   "},\n"
+   "];", FourSpaces);
+  verifyFormat("someFunction(\n"
+   "p1,\n"
+   "[\n"
+   "1,\n"
+   "],\n"
+   ");", FourSpaces);
+  verifyFormat("someFunction(\n"
+   "p1,\n"
+   "{\n"
+   "1: 1,\n"
+   "},\n"
+   ");", FourSpaces);
+  verifyFormat("var o = {\n"
+   "1: 1,\n"
+   "2: {\n"
+   "3: 3,\n"
+   "},\n"
+   "};", FourSpaces);
+  verifyFormat("var o = {\n"
+   "1: 1,\n"
+   "2: [\n"
+   "3,\n"
+   "],\n"
+   "};", FourSpaces);
+}
+
 } // end na

[PATCH] D33857: Correctly Indent Nested JavaScript Literals.

2017-06-06 Thread Martin Probst via Phabricator via cfe-commits
mprobst accepted this revision.
mprobst added a comment.
This revision is now accepted and ready to land.

LGTM


Repository:
  rL LLVM

https://reviews.llvm.org/D33857



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits