[PATCH] Multi-line comment alignment
Alexander Kornienko
alexfh at google.com
Thu Mar 14 08:26:27 PDT 2013
Implemented base indentation + indentation based on whether lines start with * or not.
Hi djasper,
http://llvm-reviews.chandlerc.com/D541
CHANGE SINCE LAST DIFF
http://llvm-reviews.chandlerc.com/D541?vs=1287&id=1290#toc
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, Spaces);
+
storeReplacement(Tok.FormatTok, getNewLineText(NewLines, Spaces));
}
@@ -191,6 +192,37 @@
}
private:
+ void alignBlockComment(const FormatToken &Tok, int BaseIndent) {
+ SourceLocation TokenLoc = Tok.Tok.getLocation();
+ const char *Start = SourceMgr.getCharacterData(TokenLoc);
+ const char *Cur = Start;
+
+ for (const char *TokEnd = Cur + Tok.TokenLength; Cur < TokEnd; ++Cur) {
+ if (*Cur == '\n') {
+ ++Cur;
+ SourceLocation Loc = TokenLoc.getLocWithOffset(Cur - Start);
+ int Indent = BaseIndent;
+ int Spaces = 0;
+ while (Cur < TokEnd && *Cur == ' ') {
+ ++Spaces;
+ ++Cur;
+ }
+ if (Cur < TokEnd && *Cur == '*')
+ ++Indent;
+ else
+ Indent += 3;
+
+ 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 +259,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,41 @@
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("/*\n"
+ " A comment.\n"
+ " */\n"
+ "void f() {}",
+ format(" /*\n"
+ " A 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.2.patch
Type: text/x-patch
Size: 4531 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20130314/23263cae/attachment.bin>
More information about the cfe-commits
mailing list