forked from qt-creator/qt-creator
ClangFormat: Fix indenting 'return' after key words
Change-Id: I9e11b4d299c13ffada897b009fb70c3447213500 Reviewed-by: Christian Kandeler <christian.kandeler@qt.io>
This commit is contained in:
@@ -210,6 +210,21 @@ static bool comesDirectlyAfterIf(const QTextDocument *doc, int pos)
|
||||
return pos > 0 && doc->characterAt(pos) == 'f' && doc->characterAt(pos - 1) == 'i';
|
||||
}
|
||||
|
||||
static bool startsWithKeyWord(const QString &keyWord, const QString &text)
|
||||
{
|
||||
if (text.size() <= keyWord.size())
|
||||
return false;
|
||||
|
||||
const QChar chAfter = text.at(keyWord.size());
|
||||
return text.startsWith(keyWord) && !chAfter.isDigit() && !chAfter.isLetter() && chAfter != '_';
|
||||
}
|
||||
|
||||
static bool startsWithKeyWords(const QString &text)
|
||||
{
|
||||
return startsWithKeyWord("if", text) || startsWithKeyWord("while", text)
|
||||
|| startsWithKeyWord("for", text);
|
||||
}
|
||||
|
||||
static CharacterContext characterContext(const QTextBlock ¤tBlock)
|
||||
{
|
||||
QTextBlock previousNonEmptyBlock = reverseFindLastEmptyBlock(currentBlock);
|
||||
@@ -220,8 +235,9 @@ static CharacterContext characterContext(const QTextBlock ¤tBlock)
|
||||
if (prevLineText.isEmpty())
|
||||
return CharacterContext::NewStatementOrContinuation;
|
||||
|
||||
if ((currentBlock.text().trimmed().isEmpty() || currentBlock.text().trimmed().endsWith(")"))
|
||||
&& prevLineText.endsWith("{"))
|
||||
const QString currentBlockText = currentBlock.text().trimmed();
|
||||
if ((currentBlockText.isEmpty() || currentBlockText.endsWith(")"))
|
||||
&& prevLineText.endsWith("{") && !startsWithKeyWords(currentBlockText))
|
||||
return CharacterContext::BracketAfterFunctionCall;
|
||||
|
||||
const QChar firstNonWhitespaceChar = findFirstNonWhitespaceCharacter(currentBlock);
|
||||
|
@@ -114,8 +114,10 @@ private slots:
|
||||
void testFunctionCallClosingParenthesis();
|
||||
void testFunctionCallClosingParenthesisEmptyLine();
|
||||
void testNoIndentationInMiddleOfLine();
|
||||
void testIndentationInTheBegginingOfLine();
|
||||
void testIndentationInMiddleOfLine();
|
||||
void testIndentationInTheBegginingOfLine();
|
||||
void testIndentationReturnAfterIf();
|
||||
void testIndentationReturnAfterIfSomthingFunction();
|
||||
|
||||
private:
|
||||
void insertLines(const std::vector<QString> &lines);
|
||||
@@ -965,6 +967,38 @@ void ClangFormatTest::testIndentationInTheBegginingOfLine()
|
||||
"}"}));
|
||||
}
|
||||
|
||||
void ClangFormatTest::testIndentationReturnAfterIf()
|
||||
{
|
||||
insertLines({"int main()",
|
||||
"{",
|
||||
" if (true)",
|
||||
" return 0;",
|
||||
"}"});
|
||||
m_indenter->indent(*m_cursor, QChar::Null, TextEditor::TabSettings());
|
||||
QCOMPARE(documentLines(),
|
||||
(std::vector<QString>{"int main()",
|
||||
"{",
|
||||
" if (true)",
|
||||
" return 0;",
|
||||
"}"}));
|
||||
}
|
||||
|
||||
void ClangFormatTest::testIndentationReturnAfterIfSomthingFunction()
|
||||
{
|
||||
insertLines({"int main()",
|
||||
"{",
|
||||
" if_somthing()",
|
||||
" return 0;",
|
||||
"}"});
|
||||
m_indenter->indent(*m_cursor, QChar::Null, TextEditor::TabSettings());
|
||||
QCOMPARE(documentLines(),
|
||||
(std::vector<QString>{"int main()",
|
||||
"{",
|
||||
" if_somthing()",
|
||||
" return 0;",
|
||||
"}"}));
|
||||
}
|
||||
|
||||
QObject *createClangFormatTest()
|
||||
{
|
||||
return new ClangFormatTest;
|
||||
|
Reference in New Issue
Block a user