From 043dde4f200c893326f813c6c2920e754d5cea9c Mon Sep 17 00:00:00 2001 From: Daniel Jasper Date: Tue, 6 May 2014 13:54:10 +0000 Subject: [PATCH] clang-format: [JS] Don't indent in goog.scope blocks. 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 | 22 ++++++++++++++++++++-- unittests/Format/FormatTestJS.cpp | 7 +++++++ 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/lib/Format/UnwrappedLineParser.cpp b/lib/Format/UnwrappedLineParser.cpp index 0467b3a166..5b6727bf9e 100644 --- a/lib/Format/UnwrappedLineParser.cpp +++ b/lib/Format/UnwrappedLineParser.cpp @@ -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(); } diff --git a/unittests/Format/FormatTestJS.cpp b/unittests/Format/FormatTestJS.cpp index e7ca14f595..4e8908685f 100644 --- a/unittests/Format/FormatTestJS.cpp +++ b/unittests/Format/FormatTestJS.cpp @@ -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 -- 2.40.0