зеркало из https://github.com/microsoft/clang-1.git
Indent all lines in a multi-line comment by the same amount.
Summary: Do this to avoid spoling nicely formatted multi-line comments (e.g. with code examples or similar stuff). Reviewers: djasper Reviewed By: djasper CC: cfe-commits, klimek Differential Revision: http://llvm-reviews.chandlerc.com/D544 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@177153 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Родитель
e310b1a770
Коммит
1fdd8b351e
|
@ -193,36 +193,34 @@ public:
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void indentBlockComment(const FormatToken &Tok, int BaseIndent) {
|
void indentBlockComment(const FormatToken &Tok, int Indent) {
|
||||||
SourceLocation TokenLoc = Tok.Tok.getLocation();
|
SourceLocation TokenLoc = Tok.Tok.getLocation();
|
||||||
|
int IndentDelta = Indent - SourceMgr.getSpellingColumnNumber(TokenLoc) + 1;
|
||||||
const char *Start = SourceMgr.getCharacterData(TokenLoc);
|
const char *Start = SourceMgr.getCharacterData(TokenLoc);
|
||||||
const char *Current = Start;
|
const char *Current = Start;
|
||||||
const char *TokEnd = Current + Tok.TokenLength;
|
const char *TokEnd = Current + Tok.TokenLength;
|
||||||
|
llvm::SmallVector<SourceLocation, 16> LineStarts;
|
||||||
while (Current < TokEnd) {
|
while (Current < TokEnd) {
|
||||||
if (*Current == '\n') {
|
if (*Current == '\n') {
|
||||||
++Current;
|
++Current;
|
||||||
SourceLocation Loc = TokenLoc.getLocWithOffset(Current - Start);
|
LineStarts.push_back(TokenLoc.getLocWithOffset(Current - Start));
|
||||||
int Indent = BaseIndent;
|
// If we need to outdent the line, check that it's indented enough.
|
||||||
int Spaces = 0;
|
for (int i = 0; i < -IndentDelta; ++i, ++Current)
|
||||||
while (Current < TokEnd && *Current == ' ') {
|
if (Current >= TokEnd || *Current != ' ')
|
||||||
++Spaces;
|
return;
|
||||||
++Current;
|
|
||||||
}
|
|
||||||
if (Current < TokEnd && *Current == '*')
|
|
||||||
++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, ""));
|
|
||||||
} else {
|
} else {
|
||||||
++Current;
|
++Current;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for (size_t i = 0; i < LineStarts.size(); ++i) {
|
||||||
|
if (IndentDelta > 0)
|
||||||
|
Replaces.insert(tooling::Replacement(SourceMgr, LineStarts[i], 0,
|
||||||
|
std::string(IndentDelta, ' ')));
|
||||||
|
else if (IndentDelta < 0)
|
||||||
|
Replaces.insert(
|
||||||
|
tooling::Replacement(SourceMgr, LineStarts[i], -IndentDelta, ""));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string getNewLineText(unsigned NewLines, unsigned Spaces) {
|
std::string getNewLineText(unsigned NewLines, unsigned Spaces) {
|
||||||
|
|
|
@ -619,14 +619,6 @@ TEST_F(FormatTest, AlignsMultiLineComments) {
|
||||||
" * comment.\n"
|
" * comment.\n"
|
||||||
" */\n"
|
" */\n"
|
||||||
" void f() {}"));
|
" void f() {}"));
|
||||||
EXPECT_EQ("/*\n"
|
|
||||||
" A comment.\n"
|
|
||||||
" */\n"
|
|
||||||
"void f() {}",
|
|
||||||
format(" /*\n"
|
|
||||||
" A comment.\n"
|
|
||||||
" */\n"
|
|
||||||
" void f() {}"));
|
|
||||||
EXPECT_EQ("class C {\n"
|
EXPECT_EQ("class C {\n"
|
||||||
" /*\n"
|
" /*\n"
|
||||||
" * Another multi-line\n"
|
" * Another multi-line\n"
|
||||||
|
@ -641,6 +633,22 @@ TEST_F(FormatTest, AlignsMultiLineComments) {
|
||||||
" */\n"
|
" */\n"
|
||||||
"void f() {}\n"
|
"void f() {}\n"
|
||||||
"};"));
|
"};"));
|
||||||
|
EXPECT_EQ("/*\n"
|
||||||
|
" 1. This is a comment with non-trivial formatting.\n"
|
||||||
|
" 1.1. We have to indent/outdent all lines equally\n"
|
||||||
|
" 1.1.1. to keep the formatting.\n"
|
||||||
|
" */",
|
||||||
|
format(" /*\n"
|
||||||
|
" 1. This is a comment with non-trivial formatting.\n"
|
||||||
|
" 1.1. We have to indent/outdent all lines equally\n"
|
||||||
|
" 1.1.1. to keep the formatting.\n"
|
||||||
|
" */"));
|
||||||
|
EXPECT_EQ("/*\n"
|
||||||
|
" Don't try to outdent if there's not enough inentation.\n"
|
||||||
|
" */",
|
||||||
|
format(" /*\n"
|
||||||
|
" Don't try to outdent if there's not enough inentation.\n"
|
||||||
|
" */"));
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_F(FormatTest, CommentsInStaticInitializers) {
|
TEST_F(FormatTest, CommentsInStaticInitializers) {
|
||||||
|
|
Загрузка…
Ссылка в новой задаче