forked from qt-creator/qt-creator
ClangFormat: Fix indentation in the middle of line
Before, indentation applies in the middle of the line if an electric character is typed. Now it happens only if the character is typed at the line's end or start and if new line character was typed. It should reduce jumping while typing. Fixes: QTCREATORBUG-30731 Change-Id: I018cb4a03af5e6450be2cd423cb29bd384048871 Reviewed-by: Christian Kandeler <christian.kandeler@qt.io>
This commit is contained in:
@@ -782,12 +782,15 @@ void ClangFormatBaseIndenterPrivate::indent(const QTextCursor &cursor,
|
||||
const QChar &typedChar,
|
||||
int cursorPositionInEditor)
|
||||
{
|
||||
const QString blockText = cursor.block().text().trimmed();
|
||||
if (cursor.hasSelection()) {
|
||||
indentBlocks(m_doc->findBlock(cursor.selectionStart()),
|
||||
m_doc->findBlock(cursor.selectionEnd()),
|
||||
typedChar,
|
||||
cursorPositionInEditor);
|
||||
} else {
|
||||
} else if (
|
||||
typedChar == QChar::Null || blockText.startsWith(typedChar) || blockText.endsWith(typedChar)
|
||||
|| blockText.isEmpty()) {
|
||||
indentBlocks(cursor.block(), cursor.block(), typedChar, cursorPositionInEditor);
|
||||
}
|
||||
}
|
||||
|
@@ -113,6 +113,9 @@ private slots:
|
||||
void testUtf8SymbolLine();
|
||||
void testFunctionCallClosingParenthesis();
|
||||
void testFunctionCallClosingParenthesisEmptyLine();
|
||||
void testNoIndentationInMiddleOfLine();
|
||||
void testIndentationInTheBegginingOfLine();
|
||||
void testIndentationInMiddleOfLine();
|
||||
|
||||
private:
|
||||
void insertLines(const std::vector<QString> &lines);
|
||||
@@ -909,6 +912,59 @@ void ClangFormatTest::testFunctionCallClosingParenthesisEmptyLine()
|
||||
}));
|
||||
}
|
||||
|
||||
void ClangFormatTest::testNoIndentationInMiddleOfLine()
|
||||
{
|
||||
insertLines({"int main()",
|
||||
"{",
|
||||
" S s = {.i = 1}, .l = 2, .f = 3, .d = 4};",
|
||||
" return 0;",
|
||||
"}"});
|
||||
m_cursor->setPosition(30);
|
||||
m_extendedIndenter->indent(*m_cursor, '}', TextEditor::TabSettings(), 30);
|
||||
QCOMPARE(documentLines(),
|
||||
(std::vector<QString>{"int main()",
|
||||
"{",
|
||||
" S s = {.i = 1}, .l = 2, .f = 3, .d = 4};",
|
||||
" return 0;",
|
||||
"}"}));
|
||||
}
|
||||
|
||||
void ClangFormatTest::testIndentationInMiddleOfLine()
|
||||
{
|
||||
insertLines({"int main()",
|
||||
"{",
|
||||
" S s = {.i = 1,",
|
||||
".l = 2, .f = 3, .d = 4};",
|
||||
" return 0;",
|
||||
"}"});
|
||||
m_cursor->setPosition(32);
|
||||
m_extendedIndenter->indent(*m_cursor, QChar::Null, TextEditor::TabSettings(), 32);
|
||||
QCOMPARE(documentLines(),
|
||||
(std::vector<QString>{"int main()",
|
||||
"{",
|
||||
" S s = {.i = 1,",
|
||||
" .l = 2, .f = 3, .d = 4};",
|
||||
" return 0;",
|
||||
"}"}));
|
||||
}
|
||||
|
||||
void ClangFormatTest::testIndentationInTheBegginingOfLine()
|
||||
{
|
||||
insertLines({"int main()",
|
||||
"{",
|
||||
" if () {",
|
||||
" } else",
|
||||
"}"});
|
||||
m_cursor->setPosition(35);
|
||||
m_extendedIndenter->indent(*m_cursor, '}', TextEditor::TabSettings(), 35);
|
||||
QCOMPARE(documentLines(),
|
||||
(std::vector<QString>{"int main()",
|
||||
"{",
|
||||
" if () {",
|
||||
" } else",
|
||||
"}"}));
|
||||
}
|
||||
|
||||
QObject *createClangFormatTest()
|
||||
{
|
||||
return new ClangFormatTest;
|
||||
|
Reference in New Issue
Block a user