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: http://review.tizen.org/git/?a=commitdiff_plain;h=553a5b00412c34e2b9101a993a6e7368a03fca68;p=platform%2Fupstream%2Fllvm.git 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. llvm-svn: 241257 --- diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp index 0dd7ca0..21f3efb 100644 --- a/clang/lib/Format/Format.cpp +++ b/clang/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/clang/unittests/Format/FormatTestJS.cpp b/clang/unittests/Format/FormatTestJS.cpp index 63bcfdc..845ae51 100644 --- a/clang/unittests/Format/FormatTestJS.cpp +++ b/clang/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.