Revert "ClangFormat: Remove format while typing feature"

This reverts commit 323ba720db.

Reason for revert: The feature is needed

Change-Id: Iaf09ef91c49ff0c85c1b5906a1c1464bd8e4cae6
Reviewed-by: Christian Kandeler <christian.kandeler@qt.io>
This commit is contained in:
Artem Sokolovskii
2022-06-29 09:59:58 +00:00
parent a0de1f1848
commit 8f1a9ea6d7
3 changed files with 20 additions and 8 deletions

View File

@@ -594,6 +594,18 @@ Utils::Text::Replacements ClangFormatBaseIndenter::indentsFor(QTextBlock startBl
const QByteArray buffer = m_doc->toPlainText().toUtf8(); const QByteArray buffer = m_doc->toPlainText().toUtf8();
ReplacementsToKeep replacementsToKeep = ReplacementsToKeep::OnlyIndent; ReplacementsToKeep replacementsToKeep = ReplacementsToKeep::OnlyIndent;
if (formatCodeInsteadOfIndent()
&& (cursorPositionInEditor == -1 || cursorPositionInEditor >= startBlockPosition)
&& (typedChar == ';' || typedChar == '}')) {
// Format before current position only in case the cursor is inside the indented block.
// So if cursor position is less then the block position then the current line is before
// the indented block - don't trigger extra formatting in this case.
// cursorPositionInEditor == -1 means the condition matches automatically.
// Format only before complete statement not to break code.
replacementsToKeep = ReplacementsToKeep::IndentAndBefore;
}
return replacements(buffer, return replacements(buffer,
startBlock, startBlock,
endBlock, endBlock,

View File

@@ -471,7 +471,7 @@ void ClangFormatTest::testIndentAfterFunctionBodyAndNotFormatBefore()
void ClangFormatTest::testReformatToEmptyFunction() void ClangFormatTest::testReformatToEmptyFunction()
{ {
insertLines({"int foo(int a, int b, int c, int d)", "{", " ", "}"}); insertLines({"int foo(int a, int b, int c, int d)", "{", " ", "}"});
m_extendedIndenter->format({{1, 4}}); m_extendedIndenter->indentBlock(m_doc->findBlockByNumber(3), '}', TextEditor::TabSettings());
QCOMPARE(documentLines(), (std::vector<QString>{"int foo(int a, int b, int c, int d) {}"})); QCOMPARE(documentLines(), (std::vector<QString>{"int foo(int a, int b, int c, int d) {}"}));
} }
@@ -483,10 +483,10 @@ void ClangFormatTest::testReformatToNonEmptyFunction()
QCOMPARE(documentLines(), (std::vector<QString>{"int foo(int a, int b) {", " ", "}"})); QCOMPARE(documentLines(), (std::vector<QString>{"int foo(int a, int b) {", " ", "}"}));
} }
void ClangFormatTest::testFormatClosingScope() void ClangFormatTest::testIndentClosingScopeAndFormatBeforeIt()
{ {
insertLines({"if(a && b", " &&c && d", " ) {", "", "}"}); insertLines({"if(a && b", " &&c && d", " ) {", "", "}"});
m_extendedIndenter->format({{1, 5}}); m_extendedIndenter->indentBlock(m_doc->findBlockByNumber(4), '}', TextEditor::TabSettings());
QCOMPARE(documentLines(), (std::vector<QString>{"if (a && b && c && d) {", "}"})); QCOMPARE(documentLines(), (std::vector<QString>{"if (a && b && c && d) {", "}"}));
} }
@@ -504,17 +504,17 @@ void ClangFormatTest::testOnlyIndentIncompleteStatementOnElectricalCharacter()
QCOMPARE(documentLines(), (std::vector<QString>{"{bar();", " foo()", "}"})); QCOMPARE(documentLines(), (std::vector<QString>{"{bar();", " foo()", "}"}));
} }
void ClangFormatTest::testFormatCompleteStatementOnSemicolon() void ClangFormatTest::testIndentAndFormatCompleteStatementOnSemicolon()
{ {
insertLines({"{bar();", "foo();", "}"}); insertLines({"{bar();", "foo();", "}"});
m_extendedIndenter->format({{1, 3}}); m_extendedIndenter->indentBlock(m_doc->findBlockByNumber(1), ';', TextEditor::TabSettings(), 14);
QCOMPARE(documentLines(), (std::vector<QString>{"{", " bar();", " foo();", "}"})); QCOMPARE(documentLines(), (std::vector<QString>{"{", " bar();", " foo();", "}"}));
} }
void ClangFormatTest::testIndentAndFormatCompleteStatementOnClosingScope() void ClangFormatTest::testIndentAndFormatCompleteStatementOnClosingScope()
{ {
insertLines({"{bar();", "foo();", "}"}); insertLines({"{bar();", "foo();", "}"});
m_extendedIndenter->format({{1, 3}}); m_extendedIndenter->indentBlock(m_doc->findBlockByNumber(1), '}', TextEditor::TabSettings(), 16);
QCOMPARE(documentLines(), (std::vector<QString>{"{", " bar();", " foo();", "}"})); QCOMPARE(documentLines(), (std::vector<QString>{"{", " bar();", " foo();", "}"}));
} }

View File

@@ -90,10 +90,10 @@ private slots:
void testIndentAfterFunctionBodyAndNotFormatBefore(); void testIndentAfterFunctionBodyAndNotFormatBefore();
void testReformatToEmptyFunction(); void testReformatToEmptyFunction();
void testReformatToNonEmptyFunction(); void testReformatToNonEmptyFunction();
void testFormatClosingScope(); void testIndentClosingScopeAndFormatBeforeIt();
void testDoNotFormatAfterTheFirstColon(); void testDoNotFormatAfterTheFirstColon();
void testOnlyIndentIncompleteStatementOnElectricalCharacter(); void testOnlyIndentIncompleteStatementOnElectricalCharacter();
void testFormatCompleteStatementOnSemicolon(); void testIndentAndFormatCompleteStatementOnSemicolon();
void testIndentAndFormatCompleteStatementOnClosingScope(); void testIndentAndFormatCompleteStatementOnClosingScope();
void testOnlyIndentClosingParenthesis(); void testOnlyIndentClosingParenthesis();
void testEquallyIndentInsideParenthesis(); void testEquallyIndentInsideParenthesis();