[PATCH] Multi-line comment alignment
Alexander Kornienko
alexfh at google.com
Thu Mar 14 07:40:08 PDT 2013
Hi djasper,
Aligns continuation lines of multi-line comments to the base
indentation level +1:
class A {
/*
* test
*/
void f() {}
};
The first revision is work in progress. The implementation is not yet complete.
http://llvm-reviews.chandlerc.com/D541
Files:
lib/Format/Format.cpp
unittests/Format/FormatTest.cpp
Index: lib/Format/Format.cpp
===================================================================
--- lib/Format/Format.cpp
+++ lib/Format/Format.cpp
@@ -128,24 +128,25 @@
if (Tok.Parent != NULL || !Comments.empty()) {
if (Style.ColumnLimit >=
Spaces + WhitespaceStartColumn + Tok.FormatTok.TokenLength) {
- Comments.push_back(StoredComment());
- Comments.back().Tok = Tok.FormatTok;
- Comments.back().Spaces = Spaces;
- Comments.back().NewLines = NewLines;
- if (NewLines == 0)
- Comments.back().MinColumn = WhitespaceStartColumn + Spaces;
- else
- Comments.back().MinColumn = Spaces;
- Comments.back().MaxColumn =
- Style.ColumnLimit - Tok.FormatTok.TokenLength;
+ StoredComment SC;
+ SC.Tok = Tok.FormatTok;
+ SC.Spaces = Spaces;
+ SC.NewLines = NewLines;
+ SC.MinColumn = NewLines > 0 ? Spaces : WhitespaceStartColumn + Spaces;
+ SC.MaxColumn = Style.ColumnLimit - Tok.FormatTok.TokenLength;
+ Comments.push_back(SC);
return;
}
}
}
// If this line does not have a trailing comment, align the stored comments.
if (Tok.Children.empty() && !isTrailingComment(Tok))
alignComments();
+
+ if (Tok.Type == TT_BlockComment)
+ alignBlockComment(Tok.FormatTok);
+
storeReplacement(Tok.FormatTok, getNewLineText(NewLines, Spaces));
}
@@ -191,6 +192,33 @@
}
private:
+ void alignBlockComment(const FormatToken &Tok) {
+ int BaseIndent = 0; // FIXME
+ const char *Start = SourceMgr.getCharacterData(Tok.WhiteSpaceStart);
+ const char *Cur = Start + Tok.WhiteSpaceLength;
+
+ for (const char *TokEnd = Cur + Tok.TokenLength; Cur < TokEnd; ++Cur) {
+ if (*Cur == '\n') {
+ ++Cur;
+ int Indent = BaseIndent;
+ int Spaces = 0;
+ while (Cur + Spaces < TokEnd && Cur[Spaces] == ' ')
+ ++Spaces;
+ if (Cur + Spaces < TokEnd && Cur[Spaces] == '*')
+ ++Indent;
+
+ SourceLocation Loc = Tok.WhiteSpaceStart.getLocWithOffset(Cur - Start);
+ if (Spaces < Indent)
+ Replaces.insert(tooling::Replacement(
+ SourceMgr, Loc, 0, std::string(Indent - Spaces, ' ')));
+ else if (Spaces > Indent)
+ Replaces.insert(
+ tooling::Replacement(SourceMgr, Loc, Spaces - Indent, ""));
+ --Cur;
+ }
+ }
+ }
+
std::string getNewLineText(unsigned NewLines, unsigned Spaces) {
return std::string(NewLines, '\n') + std::string(Spaces, ' ');
}
@@ -227,8 +255,7 @@
unsigned MinColumn = 0;
unsigned MaxColumn = UINT_MAX;
comment_iterator Start = Comments.begin();
- for (comment_iterator I = Comments.begin(), E = Comments.end(); I != E;
- ++I) {
+ for (comment_iterator I = Start, E = Comments.end(); I != E; ++I) {
if (I->MinColumn > MaxColumn || I->MaxColumn < MinColumn) {
alignComments(Start, I, MinColumn);
MinColumn = I->MinColumn;
Index: unittests/Format/FormatTest.cpp
===================================================================
--- unittests/Format/FormatTest.cpp
+++ unittests/Format/FormatTest.cpp
@@ -608,6 +608,33 @@
NoBinPacking);
}
+TEST_F(FormatTest, AlignsMultiLineComments) {
+ EXPECT_EQ("/*\n"
+ " * Really multi-line\n"
+ " * comment.\n"
+ " */\n"
+ "void f() {}",
+ format(" /*\n"
+ " * Really multi-line\n"
+ " * comment.\n"
+ " */\n"
+ " void f() {}"));
+ EXPECT_EQ("class C {\n"
+ " /*\n"
+ " * Another multi-line\n"
+ " * comment.\n"
+ " */\n"
+ " void f() {}\n"
+ "};",
+ format("class C {\n"
+ "/*\n"
+ " * Another multi-line\n"
+ " * comment.\n"
+ " */\n"
+ "void f() {}\n"
+ "};"));
+}
+
TEST_F(FormatTest, CommentsInStaticInitializers) {
EXPECT_EQ(
"static SomeType type = { aaaaaaaaaaaaaaaaaaaa, /* comment */\n"
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D541.1.patch
Type: text/x-patch
Size: 4250 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20130314/aeb1e090/attachment.bin>
More information about the cfe-commits
mailing list