krasimir created this revision. Herald added a project: clang. Herald added a subscriber: cfe-commits. krasimir requested review of this revision.
In JavaScript breaking before a `@tag` in a comment puts it on a new line, and machinery that parses these comments will fail to understand such comments. This adapts clang-format to not break before `@`. Similar functionality exists for not breaking before `{`. Repository: rG LLVM Github Monorepo https://reviews.llvm.org/D91078 Files: clang/lib/Format/BreakableToken.cpp clang/unittests/Format/FormatTestJS.cpp Index: clang/unittests/Format/FormatTestJS.cpp =================================================================== --- clang/unittests/Format/FormatTestJS.cpp +++ clang/unittests/Format/FormatTestJS.cpp @@ -187,22 +187,11 @@ format("/** @returns j */", getGoogleJSStyleWithColumns(20))); // Break a single line long jsdoc comment pragma. - EXPECT_EQ("/**\n" - " * @returns {string} jsdoc line 12\n" - " */", - format("/** @returns {string} jsdoc line 12 */", - getGoogleJSStyleWithColumns(20))); EXPECT_EQ("/**\n" " * @returns {string}\n" " * jsdoc line 12\n" " */", format("/** @returns {string} jsdoc line 12 */", - getGoogleJSStyleWithColumns(25))); - - EXPECT_EQ("/**\n" - " * @returns {string} jsdoc line 12\n" - " */", - format("/** @returns {string} jsdoc line 12 */", getGoogleJSStyleWithColumns(20))); // FIXME: this overcounts the */ as a continuation of the 12 when breaking. @@ -2194,6 +2183,16 @@ " */", getGoogleJSStyleWithColumns(ColumnLimit)); } + // don't break before @tags + verifyFormat("/**\n" + " * This\n" + " * tag @param\n" + " * stays.\n" + " */", + "/**\n" + " * This tag @param stays.\n" + " */", + getGoogleJSStyleWithColumns(13)); verifyFormat("/**\n" " * @see http://very/very/long/url/is/long\n" " */", Index: clang/lib/Format/BreakableToken.cpp =================================================================== --- clang/lib/Format/BreakableToken.cpp +++ clang/lib/Format/BreakableToken.cpp @@ -124,9 +124,10 @@ continue; } - // Avoid ever breaking before a { in JavaScript. + // Avoid ever breaking before a @tag or a { in JavaScript. if (Style.Language == FormatStyle::LK_JavaScript && - SpaceOffset + 1 < Text.size() && Text[SpaceOffset + 1] == '{') { + SpaceOffset + 1 < Text.size() && + (Text[SpaceOffset + 1] == '{' || Text[SpaceOffset + 1] == '@')) { SpaceOffset = Text.find_last_of(Blanks, SpaceOffset); continue; }
Index: clang/unittests/Format/FormatTestJS.cpp =================================================================== --- clang/unittests/Format/FormatTestJS.cpp +++ clang/unittests/Format/FormatTestJS.cpp @@ -187,22 +187,11 @@ format("/** @returns j */", getGoogleJSStyleWithColumns(20))); // Break a single line long jsdoc comment pragma. - EXPECT_EQ("/**\n" - " * @returns {string} jsdoc line 12\n" - " */", - format("/** @returns {string} jsdoc line 12 */", - getGoogleJSStyleWithColumns(20))); EXPECT_EQ("/**\n" " * @returns {string}\n" " * jsdoc line 12\n" " */", format("/** @returns {string} jsdoc line 12 */", - getGoogleJSStyleWithColumns(25))); - - EXPECT_EQ("/**\n" - " * @returns {string} jsdoc line 12\n" - " */", - format("/** @returns {string} jsdoc line 12 */", getGoogleJSStyleWithColumns(20))); // FIXME: this overcounts the */ as a continuation of the 12 when breaking. @@ -2194,6 +2183,16 @@ " */", getGoogleJSStyleWithColumns(ColumnLimit)); } + // don't break before @tags + verifyFormat("/**\n" + " * This\n" + " * tag @param\n" + " * stays.\n" + " */", + "/**\n" + " * This tag @param stays.\n" + " */", + getGoogleJSStyleWithColumns(13)); verifyFormat("/**\n" " * @see http://very/very/long/url/is/long\n" " */", Index: clang/lib/Format/BreakableToken.cpp =================================================================== --- clang/lib/Format/BreakableToken.cpp +++ clang/lib/Format/BreakableToken.cpp @@ -124,9 +124,10 @@ continue; } - // Avoid ever breaking before a { in JavaScript. + // Avoid ever breaking before a @tag or a { in JavaScript. if (Style.Language == FormatStyle::LK_JavaScript && - SpaceOffset + 1 < Text.size() && Text[SpaceOffset + 1] == '{') { + SpaceOffset + 1 < Text.size() && + (Text[SpaceOffset + 1] == '{' || Text[SpaceOffset + 1] == '@')) { SpaceOffset = Text.find_last_of(Blanks, SpaceOffset); continue; }
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits