diff options
Diffstat (limited to 'src/plugins')
-rw-r--r-- | src/plugins/texteditor/formattexteditor.cpp | 57 | ||||
-rw-r--r-- | src/plugins/texteditor/texteditorplugin.h | 3 |
2 files changed, 58 insertions, 2 deletions
diff --git a/src/plugins/texteditor/formattexteditor.cpp b/src/plugins/texteditor/formattexteditor.cpp index 8f2fc35844..19716e22dd 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 <texteditorplugin.h> +#include <QTest> + +namespace TextEditor::Internal { + +void TextEditorPlugin::testFormatting_data() +{ + QTest::addColumn<QString>("code"); + QTest::addColumn<QString>("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<TextEditorWidget> editor(new TextEditorWidget); + QVERIFY(editor.get()); + + QSharedPointer<TextDocument> 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 067e4e9631..3df46c0b57 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 }; |