summaryrefslogtreecommitdiff
path: root/src/plugins/texteditor
diff options
context:
space:
mode:
authorSemih Yavuz <semih.yavuz@qt.io>2023-03-17 23:30:23 +0100
committerSemih Yavuz <semih.yavuz@qt.io>2023-03-22 12:54:11 +0000
commit6deabc4334c1d2e0ebfce13074cb3226237c8656 (patch)
tree136b96af568696247061f3d8c1b1a66e897b9f87 /src/plugins/texteditor
parent661eca5968b1fb3550d250e5a4a5e0a138473659 (diff)
downloadqt-creator-6deabc4334c1d2e0ebfce13074cb3226237c8656.tar.gz
formattexteditor: fix broken formatting
In case of unicode character, moving cursor relatively can result in wrong placement of the cursor. Use absolute positions. Fixes: QTCREATORBUG-28859 Change-Id: Idf68481861fc10c24b1eb330220fba92cadf560a Reviewed-by: Eike Ziller <eike.ziller@qt.io> Reviewed-by: David Schulz <david.schulz@qt.io> Reviewed-by: <github-actions-qt-creator@cristianadam.eu>
Diffstat (limited to 'src/plugins/texteditor')
-rw-r--r--src/plugins/texteditor/formattexteditor.cpp57
-rw-r--r--src/plugins/texteditor/texteditorplugin.h3
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
};