From 3f11941d8ab0bbd61bf8c05f08bdeebf46256d2d Mon Sep 17 00:00:00 2001 From: Daniel Jasper Date: Tue, 31 Jan 2017 14:39:33 +0000 Subject: [PATCH] clang-format: [JS] Indent expressions in ${} relative to their surrounding This only affects expressions inside ${} scopes of template strings. Here, we want to indent relative to the surrounding template string and not the surrounding expression. Otherwise, this can create quite a mess. Before: var f = ` aaaaaaaaaaaaaaaaaa: ${someFunction( aaaaa + // bbbb)}`; After: var f = ` aaaaaaaaaaaaaaaaaa: ${someFunction( aaaaa + // bbbb)}`; llvm-svn: 293636 --- clang/lib/Format/ContinuationIndenter.cpp | 15 +++++- clang/unittests/Format/FormatTestJS.cpp | 57 +++++++++++++++++++++++ 2 files changed, 70 insertions(+), 2 deletions(-) diff --git a/clang/lib/Format/ContinuationIndenter.cpp b/clang/lib/Format/ContinuationIndenter.cpp index 13a617766913..995ff06b540c 100644 --- a/clang/lib/Format/ContinuationIndenter.cpp +++ b/clang/lib/Format/ContinuationIndenter.cpp @@ -985,12 +985,23 @@ void ContinuationIndenter::moveStatePastScopeOpener(LineState &State, // int> v); // FIXME: We likely want to do this for more combinations of brackets. // Verify that it is wanted for ObjC, too. - if (Current.Tok.getKind() == tok::less && - Current.ParentBracket == tok::l_paren) { + if (Current.is(tok::less) && Current.ParentBracket == tok::l_paren) { NewIndent = std::max(NewIndent, State.Stack.back().Indent); LastSpace = std::max(LastSpace, State.Stack.back().Indent); } + // JavaScript template strings are special as we always want to indent + // nested expressions relative to the ${}. Otherwise, this can create quite + // a mess. + if (Current.is(TT_TemplateString)) { + unsigned Column = Current.IsMultiline + ? Current.LastLineColumnWidth + : State.Column + Current.ColumnWidth; + NewIndent = Column; + LastSpace = Column; + NestedBlockIndent = Column; + } + AvoidBinPacking = (State.Line->MustBeDeclaration && !Style.BinPackParameters) || (!State.Line->MustBeDeclaration && !Style.BinPackArguments) || diff --git a/clang/unittests/Format/FormatTestJS.cpp b/clang/unittests/Format/FormatTestJS.cpp index 6cc3bcd16c46..9feda05226c0 100644 --- a/clang/unittests/Format/FormatTestJS.cpp +++ b/clang/unittests/Format/FormatTestJS.cpp @@ -1401,6 +1401,63 @@ TEST_F(FormatTestJS, TemplateStrings) { " aaaaaaaaaaaaa:${ aaaaaaa. aaaaa} aaaaaaaa`;"); } +TEST_F(FormatTestJS, TemplateStringMultiLineExpression) { + verifyFormat("var f = `aaaaaaaaaaaaaaaaaa: ${aaaaa + //\n" + " bbbb}`;", + "var f = `aaaaaaaaaaaaaaaaaa: ${aaaaa + //\n" + " bbbb}`;"); + verifyFormat("var f = `aaaaaaaaaaaaaaaaaa: ${ //\n" + " aaaaa + //\n" + " bbbb}`;", + "var f = `aaaaaaaaaaaaaaaaaa: ${ //\n" + " aaaaa + //\n" + " bbbb}`;"); + verifyFormat("var f = `\n" + " aaaaaaaaaaaaaaaaaa: ${aaaaa + //\n" + " bbbb}`;", + "var f = `\n" + " aaaaaaaaaaaaaaaaaa: ${ aaaaa + //\n" + " bbbb }`;"); + verifyFormat("var f = `\n" + " aaaaaaaaaaaaaaaaaa: ${ //\n" + " aaaaa + //\n" + " bbbb}`;", + "var f = `\n" + " aaaaaaaaaaaaaaaaaa: ${ //\n" + " aaaaa + //\n" + " bbbb}` ;"); + verifyFormat("var f = `\n" + " aaaaaaaaaaaaaaaaaa: ${someFunction(\n" + " aaaaa + //\n" + " bbbb)}`;", + "var f = `\n" + " aaaaaaaaaaaaaaaaaa: ${ someFunction (\n" + " aaaaa + //\n" + " bbbb)}`;"); + verifyFormat("var f = `\n" + " aaaaaaaaaaaaaaaaaa: ${ //\n" + " someFunction(\n" + " aaaaa + //\n" + " bbbb)}`;", + "var f = `\n" + " aaaaaaaaaaaaaaaaaa: ${ //\n" + " someFunction (\n" + " aaaaa + //\n" + " bbbb)}`;"); + verifyFormat("var f = `\n" + " aaaaaaaaaaaaaaaaaa: ${ //\n" + " someFunction({\n" + " aaaa: aaaaa,\n" + " bbbb: bbbbb,\n" + " })}`;", + "var f = `\n" + " aaaaaaaaaaaaaaaaaa: ${ //\n" + " someFunction ({\n" + " aaaa: aaaaa,\n" + " bbbb: bbbbb,\n" + " })}`;"); +} + TEST_F(FormatTestJS, TemplateStringASI) { verifyFormat("var x = `hello${world}`;", "var x = `hello${\n" " world\n"