diff --git a/src/plugins/texteditor/formattexteditor.cpp b/src/plugins/texteditor/formattexteditor.cpp index 8f2fc358441..19716e22dd4 100644 --- a/src/plugins/texteditor/formattexteditor.cpp +++ b/src/plugins/texteditor/formattexteditor.cpp @@ -215,7 +215,7 @@ void updateEditorText(QPlainTextEdit *editor, const QString &text) } } } - cursor.movePosition(QTextCursor::Right, QTextCursor::KeepAnchor, d.text.size()); + cursor.setPosition(cursor.position() + d.text.size(), QTextCursor::KeepAnchor); cursor.removeSelectedText(); break; } @@ -223,7 +223,7 @@ void updateEditorText(QPlainTextEdit *editor, const QString &text) case Diff::Equal: // Adjust cursor position charactersInfrontOfCursor -= d.text.size(); - cursor.movePosition(QTextCursor::Right, QTextCursor::MoveAnchor, d.text.size()); + cursor.setPosition(cursor.position() + d.text.size(), QTextCursor::MoveAnchor); break; } } @@ -329,3 +329,56 @@ void formatEditorAsync(TextEditorWidget *editor, const Command &command, int sta } } // namespace TextEditor + +#ifdef WITH_TESTS +#include +#include + +namespace TextEditor::Internal { + +void TextEditorPlugin::testFormatting_data() +{ + QTest::addColumn("code"); + QTest::addColumn("result"); + + { + QString code { + "import QtQuick\n\n" + " Item {\n" + " property string cat: [\"👩🏽‍🚒d👩🏽‍🚒d👩🏽‍🚒\"]\n" + " property string dog: cat\n" + "}\n" + }; + + QString result { + "import QtQuick\n\n" + "Item {\n" + " property string cat: [\"👩🏽‍🚒\"]\n" + " property string dog: cat\n" + "}\n" + }; + + QTest::newRow("unicodeCharacterInFormattedCode") << code << result; + } +} + +void TextEditorPlugin::testFormatting() +{ + QFETCH(QString, code); + QFETCH(QString, result); + + QScopedPointer editor(new TextEditorWidget); + QVERIFY(editor.get()); + + QSharedPointer doc(new TextDocument); + doc->setPlainText(code); + editor->setTextDocument(doc); + + TextEditor::updateEditorText(editor.get(), result); + + QCOMPARE(editor->toPlainText(), result); +} + +} // namespace TextEditor::Internal + +#endif diff --git a/src/plugins/texteditor/texteditorplugin.h b/src/plugins/texteditor/texteditorplugin.h index 067e4e96314..3df46c0b578 100644 --- a/src/plugins/texteditor/texteditorplugin.h +++ b/src/plugins/texteditor/texteditorplugin.h @@ -37,6 +37,9 @@ private slots: void testIndentationClean_data(); void testIndentationClean(); + + void testFormatting_data(); + void testFormatting(); #endif };