]> granicus.if.org Git - clang/commitdiff
clang-format: [JS] Don't indent in goog.scope blocks.
authorDaniel Jasper <djasper@google.com>
Tue, 6 May 2014 13:54:10 +0000 (13:54 +0000)
committerDaniel Jasper <djasper@google.com>
Tue, 6 May 2014 13:54:10 +0000 (13:54 +0000)
Before:
  goog.scope(function() {
    var x = a.b;
    var y = c.d;
  });  // goog.scope

After:
  goog.scope(function() {
  var x = a.b;
  var y = c.d;
  });  // goog.scope

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@208088 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Format/UnwrappedLineParser.cpp
unittests/Format/FormatTestJS.cpp

index 0467b3a1662886537106a76fd38f4a883dc83af7..5b6727bf9e430ce5fca2cd0fcaf544dd9a8587b9 100644 (file)
@@ -415,16 +415,34 @@ void UnwrappedLineParser::parseBlock(bool MustBeDeclaration, bool AddLevel,
   Line->Level = InitialLevel;
 }
 
+static bool IsGoogScope(const UnwrappedLine &Line) {
+  if (Line.Tokens.size() < 4)
+    return false;
+  auto I = Line.Tokens.begin();
+  if (I->Tok->TokenText != "goog")
+    return false;
+  ++I;
+  if (I->Tok->isNot(tok::period))
+    return false;
+  ++I;
+  if (I->Tok->TokenText != "scope")
+    return false;
+  ++I;
+  return I->Tok->is(tok::l_paren);
+}
+
 void UnwrappedLineParser::parseChildBlock() {
   FormatTok->BlockKind = BK_Block;
   nextToken();
   {
+    bool GoogScope =
+        Style.Language == FormatStyle::LK_JavaScript && IsGoogScope(*Line);
     ScopedLineState LineState(*this);
     ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
                                             /*MustBeDeclaration=*/false);
-    Line->Level += 1;
+    Line->Level += GoogScope ? 0 : 1;
     parseLevel(/*HasOpeningBrace=*/true);
-    Line->Level -= 1;
+    Line->Level -= GoogScope ? 0 : 1;
   }
   nextToken();
 }
index e7ca14f595d1a54d1ae6c8d9c8aa98e73865409e..4e8908685f01c299fbe497b5fb97c2321e2057e0 100644 (file)
@@ -91,5 +91,12 @@ TEST_F(FormatTestJS, SingleQuoteStrings) {
   verifyFormat("this.function('', true);");
 }
 
+TEST_F(FormatTestJS, GoogScopes) {
+  verifyFormat("goog.scope(function() {\n"
+               "var x = a.b;\n"
+               "var y = c.d;\n"
+               "});  // goog.scope");
+}
+
 } // end namespace tooling
 } // end namespace clang