ClangFormat: Clean up some code and and few unit-tests

Check the context related stuff and remove the restriction
to format after the stream operator.

Change-Id: Iaa2d32c9bffe7a0eeb7e8ade39f109b529e1eab0
Reviewed-by: Marco Bubke <marco.bubke@qt.io>
This commit is contained in:
Ivan Donchevskii
2019-02-14 13:02:21 +01:00
parent 9bcc871ece
commit 7f461b2e22
2 changed files with 71 additions and 18 deletions

View File

@@ -403,6 +403,7 @@ static bool doNotIndentInContext(QTextDocument *doc, int pos)
const QChar character = doc->characterAt(pos); const QChar character = doc->characterAt(pos);
const QTextBlock currentBlock = doc->findBlock(pos); const QTextBlock currentBlock = doc->findBlock(pos);
const QString text = currentBlock.text().left(pos - currentBlock.position()); const QString text = currentBlock.text().left(pos - currentBlock.position());
// NOTE: check if "<<" and ">>" always work correctly.
switch (character.toLatin1()) { switch (character.toLatin1()) {
default: default:
break; break;
@@ -417,12 +418,6 @@ static bool doNotIndentInContext(QTextDocument *doc, int pos)
if (pos > 0 && doc->characterAt(pos - 1) != ':') if (pos > 0 && doc->characterAt(pos - 1) != ':')
return true; return true;
break; break;
case '<':
case '>':
// "<<" and ">>" could be problematic
if (pos > 0 && doc->characterAt(pos - 1) == character)
return true;
break;
} }
return false; return false;
@@ -432,16 +427,19 @@ void ClangFormatBaseIndenter::indentBlock(const QTextBlock &block,
const QChar &typedChar, const QChar &typedChar,
int cursorPositionInEditor) int cursorPositionInEditor)
{ {
QTextBlock currentBlock = block;
const int blockPosition = currentBlock.position();
trimFirstNonEmptyBlock(currentBlock);
if (typedChar != QChar::Null && cursorPositionInEditor > 0 if (typedChar != QChar::Null && cursorPositionInEditor > 0
&& m_doc->characterAt(cursorPositionInEditor - 1) == typedChar && m_doc->characterAt(cursorPositionInEditor - 1) == typedChar
&& doNotIndentInContext(m_doc, cursorPositionInEditor - 1)) { && doNotIndentInContext(m_doc, cursorPositionInEditor - 1)) {
return; return;
} }
const int blockPosition = block.position();
trimFirstNonEmptyBlock(block);
if (cursorPositionInEditor >= 0)
cursorPositionInEditor += block.position() - blockPosition;
else
cursorPositionInEditor = block.position();
if (formatWhileTyping() if (formatWhileTyping()
&& (cursorPositionInEditor == -1 || cursorPositionInEditor >= blockPosition) && (cursorPositionInEditor == -1 || cursorPositionInEditor >= blockPosition)
&& (typedChar == QChar::Null || typedChar == ';' || typedChar == '}')) { && (typedChar == QChar::Null || typedChar == ';' || typedChar == '}')) {
@@ -451,12 +449,7 @@ void ClangFormatBaseIndenter::indentBlock(const QTextBlock &block,
// cursorPositionInEditor == -1 means the consition matches automatically. // cursorPositionInEditor == -1 means the consition matches automatically.
// Format only before newline or complete statement not to break code. // Format only before newline or complete statement not to break code.
if (cursorPositionInEditor >= 0) indentBeforeCursor(block, typedChar, cursorPositionInEditor);
cursorPositionInEditor += currentBlock.position() - blockPosition;
else
cursorPositionInEditor = currentBlock.position();
indentBeforeCursor(currentBlock, typedChar, cursorPositionInEditor);
return; return;
} }
@@ -464,11 +457,11 @@ void ClangFormatBaseIndenter::indentBlock(const QTextBlock &block,
const int utf8Offset = Utils::Text::utf8NthLineOffset(m_doc, buffer, block.blockNumber() + 1); const int utf8Offset = Utils::Text::utf8NthLineOffset(m_doc, buffer, block.blockNumber() + 1);
QTC_ASSERT(utf8Offset >= 0, return;); QTC_ASSERT(utf8Offset >= 0, return;);
applyReplacements(currentBlock, applyReplacements(block,
replacements(buffer, replacements(buffer,
utf8Offset, utf8Offset,
0, 0,
currentBlock, block,
cursorPositionInEditor, cursorPositionInEditor,
ReplacementsToKeep::OnlyIndent, ReplacementsToKeep::OnlyIndent,
typedChar)); typedChar));
@@ -484,7 +477,13 @@ void ClangFormatBaseIndenter::indentBlock(const QTextBlock &block,
int ClangFormatBaseIndenter::indentFor(const QTextBlock &block, int cursorPositionInEditor) int ClangFormatBaseIndenter::indentFor(const QTextBlock &block, int cursorPositionInEditor)
{ {
const int blockPosition = block.position();
trimFirstNonEmptyBlock(block); trimFirstNonEmptyBlock(block);
if (cursorPositionInEditor >= 0)
cursorPositionInEditor += block.position() - blockPosition;
else
cursorPositionInEditor = block.position();
const QByteArray buffer = m_doc->toPlainText().toUtf8(); const QByteArray buffer = m_doc->toPlainText().toUtf8();
const int utf8Offset = Utils::Text::utf8NthLineOffset(m_doc, buffer, block.blockNumber() + 1); const int utf8Offset = Utils::Text::utf8NthLineOffset(m_doc, buffer, block.blockNumber() + 1);
QTC_ASSERT(utf8Offset >= 0, return 0;); QTC_ASSERT(utf8Offset >= 0, return 0;);

View File

@@ -408,6 +408,60 @@ TEST_F(ClangFormat, IndentIfBodyAndFormatBeforeIt)
"}")); "}"));
} }
TEST_F(ClangFormat, DoNotFormatAfterTheFirstColon)
{
insertLines({"{",
" Qt:",
"}"});
extendedIndenter.indentBlock(doc.findBlockByNumber(1), ':', TextEditor::TabSettings(), 9);
ASSERT_THAT(documentLines(), ElementsAre("{",
" Qt:",
"}"));
}
TEST_F(ClangFormat, OnlyIndentIncompleteStatementOnElectricalCharacter)
{
insertLines({"{bar();",
"foo()",
"}"});
extendedIndenter.indentBlock(doc.findBlockByNumber(1), '(', TextEditor::TabSettings(), 12);
ASSERT_THAT(documentLines(), ElementsAre("{bar();",
" foo()",
"}"));
}
TEST_F(ClangFormat, IndentAndFormatCompleteStatementOnSemicolon)
{
insertLines({"{bar();",
"foo();",
"}"});
extendedIndenter.indentBlock(doc.findBlockByNumber(1), ';', TextEditor::TabSettings(), 14);
ASSERT_THAT(documentLines(), ElementsAre("{",
" bar();",
" foo();",
"}"));
}
TEST_F(ClangFormat, IndentAndFormatCompleteStatementOnClosingScope)
{
insertLines({"{bar();",
"foo();",
"}"});
extendedIndenter.indentBlock(doc.findBlockByNumber(1), '}', TextEditor::TabSettings(), 16);
ASSERT_THAT(documentLines(), ElementsAre("{",
" bar();",
" foo();",
"}"));
}
TEST_F(ClangFormat, FormatBasicFile) TEST_F(ClangFormat, FormatBasicFile)
{ {
insertLines({"int main()", insertLines({"int main()",