ClangFormat: Fix indenting 'return' after key words

Change-Id: I9e11b4d299c13ffada897b009fb70c3447213500
Reviewed-by: Christian Kandeler <christian.kandeler@qt.io>
This commit is contained in:
Artem Sokolovskii
2024-06-03 15:45:39 +02:00
parent 9b2bd223dd
commit e8938acca9
2 changed files with 53 additions and 3 deletions

View File

@@ -210,6 +210,21 @@ static bool comesDirectlyAfterIf(const QTextDocument *doc, int pos)
return pos > 0 && doc->characterAt(pos) == 'f' && doc->characterAt(pos - 1) == 'i'; 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 &currentBlock) static CharacterContext characterContext(const QTextBlock &currentBlock)
{ {
QTextBlock previousNonEmptyBlock = reverseFindLastEmptyBlock(currentBlock); QTextBlock previousNonEmptyBlock = reverseFindLastEmptyBlock(currentBlock);
@@ -220,8 +235,9 @@ static CharacterContext characterContext(const QTextBlock &currentBlock)
if (prevLineText.isEmpty()) if (prevLineText.isEmpty())
return CharacterContext::NewStatementOrContinuation; return CharacterContext::NewStatementOrContinuation;
if ((currentBlock.text().trimmed().isEmpty() || currentBlock.text().trimmed().endsWith(")")) const QString currentBlockText = currentBlock.text().trimmed();
&& prevLineText.endsWith("{")) if ((currentBlockText.isEmpty() || currentBlockText.endsWith(")"))
&& prevLineText.endsWith("{") && !startsWithKeyWords(currentBlockText))
return CharacterContext::BracketAfterFunctionCall; return CharacterContext::BracketAfterFunctionCall;
const QChar firstNonWhitespaceChar = findFirstNonWhitespaceCharacter(currentBlock); const QChar firstNonWhitespaceChar = findFirstNonWhitespaceCharacter(currentBlock);

View File

@@ -114,8 +114,10 @@ private slots:
void testFunctionCallClosingParenthesis(); void testFunctionCallClosingParenthesis();
void testFunctionCallClosingParenthesisEmptyLine(); void testFunctionCallClosingParenthesisEmptyLine();
void testNoIndentationInMiddleOfLine(); void testNoIndentationInMiddleOfLine();
void testIndentationInTheBegginingOfLine();
void testIndentationInMiddleOfLine(); void testIndentationInMiddleOfLine();
void testIndentationInTheBegginingOfLine();
void testIndentationReturnAfterIf();
void testIndentationReturnAfterIfSomthingFunction();
private: private:
void insertLines(const std::vector<QString> &lines); 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() QObject *createClangFormatTest()
{ {
return new ClangFormatTest; return new ClangFormatTest;