From: Daniel Jasper Date: Thu, 2 Jul 2015 13:08:28 +0000 (+0000) Subject: clang-format: [JS] Fix character counting in template strings. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=b73f25667c9dfb5cf9a136b83c5ddfd9b03668fd;p=clang clang-format: [JS] Fix character counting in template strings. Some counts were off, we don't need to take the leading newlines of the first ` into account and some tests were just wrong. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@241257 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Format/Format.cpp b/lib/Format/Format.cpp index 0dd7ca0cbc..21f3efbb7a 100644 --- a/lib/Format/Format.cpp +++ b/lib/Format/Format.cpp @@ -837,7 +837,7 @@ private: EndBacktick->OriginalColumn + EndBacktick->ColumnWidth; for (auto I = Tokens.rbegin() + 1, E = Tokens.rend(); I != E; I++) { ++TokenCount; - if (I[0]->NewlinesBefore > 0 || I[0]->IsMultiline) + if (I[0]->IsMultiline) IsMultiline = true; // If there was a preceding template string, this must be the start of a @@ -853,9 +853,10 @@ private: EndColumnInFirstLine = I[0]->OriginalColumn + I[0]->ColumnWidth; // If the token has newlines, the token before it (if it exists) is the // rhs end of the previous line. - if (I[0]->NewlinesBefore > 0 && (I + 1 != E)) + if (I[0]->NewlinesBefore > 0 && (I + 1 != E)) { EndColumnInFirstLine = I[1]->OriginalColumn + I[1]->ColumnWidth; - + IsMultiline = true; + } continue; } @@ -888,7 +889,8 @@ private: // until here`; Tokens.back()->ColumnWidth = EndColumnInFirstLine - Tokens.back()->OriginalColumn; - Tokens.back()->LastLineColumnWidth = EndOriginalColumn; + // +1 for the ` itself. + Tokens.back()->LastLineColumnWidth = EndOriginalColumn + 1; Tokens.back()->IsMultiline = true; } else { // Token simply spans from start to end, +1 for the ` itself. @@ -1080,7 +1082,6 @@ private: break; default: FormatTok->Type = TT_ImplicitStringLiteral; - ++Column; break; } } diff --git a/unittests/Format/FormatTestJS.cpp b/unittests/Format/FormatTestJS.cpp index 63bcfdc174..845ae5124b 100644 --- a/unittests/Format/FormatTestJS.cpp +++ b/unittests/Format/FormatTestJS.cpp @@ -795,15 +795,11 @@ TEST_F(FormatTestJS, TemplateStrings) { " ${ name }\n" " !`;")); - // FIXME: +1 / -1 offsets are to work around clang-format miscalculating - // widths for unknown tokens that are not whitespace (e.g. '`'). Remove when - // the code is corrected. - verifyFormat("var x =\n" " `hello ${world}` >= some();", getGoogleJSStyleWithColumns(34)); // Barely doesn't fit. verifyFormat("var x = `hello ${world}` >= some();", - getGoogleJSStyleWithColumns(35 + 1)); // Barely fits. + getGoogleJSStyleWithColumns(35)); // Barely fits. EXPECT_EQ("var x = `hello\n" " ${world}` >=\n" " some();", @@ -818,10 +814,14 @@ TEST_F(FormatTestJS, TemplateStrings) { " ${world}` >= some();", getGoogleJSStyleWithColumns(22))); // Barely fits. - verifyFormat("var x =\n `h`;", getGoogleJSStyleWithColumns(13 - 1)); + verifyFormat("var x =\n" + " `h`;", + getGoogleJSStyleWithColumns(11)); EXPECT_EQ( "var x =\n `multi\n line`;", - format("var x = `multi\n line`;", getGoogleJSStyleWithColumns(14 - 1))); + format("var x = `multi\n line`;", getGoogleJSStyleWithColumns(13))); + verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" + " `aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa`);"); // Make sure template strings get a proper ColumnWidth assigned, even if they // are first token in line.