From: Daniel Jasper Date: Thu, 4 Sep 2014 14:58:30 +0000 (+0000) Subject: clang-format: [JS] Support comments in dict literals. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=c11fbde24beed434ccc7c4816ead932378454f6d;p=clang clang-format: [JS] Support comments in dict literals. Before: var stuff = { // comment for update update : false, // comment for update modules : false, // comment for update tasks : false }; After: var stuff = { // comment for update update : false, // comment for update modules : false, // comment for update tasks : false }; git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@217157 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Format/TokenAnnotator.cpp b/lib/Format/TokenAnnotator.cpp index 3b92c10fd8..6142e7dc56 100644 --- a/lib/Format/TokenAnnotator.cpp +++ b/lib/Format/TokenAnnotator.cpp @@ -1100,7 +1100,7 @@ public: ++OperatorIndex; } - next(); + next(/*SkipPastLeadingComments=*/false); } } } @@ -1113,7 +1113,9 @@ private: if (Current->Type == TT_ConditionalExpr) return prec::Conditional; else if (Current->is(tok::semi) || Current->Type == TT_InlineASMColon || - Current->Type == TT_SelectorName) + Current->Type == TT_SelectorName || + (Current->is(tok::comment) && Current->getNextNonComment() && + Current->getNextNonComment()->Type == TT_SelectorName)) return 0; else if (Current->Type == TT_RangeBasedForLoopColon) return prec::Comma; @@ -1166,10 +1168,12 @@ private: addFakeParenthesis(Start, prec::Conditional); } - void next() { + void next(bool SkipPastLeadingComments = true) { if (Current) Current = Current->Next; - while (Current && Current->isTrailingComment()) + while (Current && + (Current->NewlinesBefore == 0 || SkipPastLeadingComments) && + Current->isTrailingComment()) Current = Current->Next; } diff --git a/unittests/Format/FormatTestJS.cpp b/unittests/Format/FormatTestJS.cpp index f8802b85d6..4b0d1e77bd 100644 --- a/unittests/Format/FormatTestJS.cpp +++ b/unittests/Format/FormatTestJS.cpp @@ -111,6 +111,14 @@ TEST_F(FormatTestJS, ContainerLiterals) { " f(); //\n" " }\n" "};"); + verifyFormat("var stuff = {\n" + " // comment for update\n" + " update: false,\n" + " // comment for modules\n" + " modules: false,\n" + " // comment for tasks\n" + " tasks: false\n" + "};"); } TEST_F(FormatTestJS, SpacesInContainerLiterals) {