forked from qt-creator/qt-creator
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>
This commit is contained in:
@@ -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
|
||||
|
@@ -37,6 +37,9 @@ private slots:
|
||||
|
||||
void testIndentationClean_data();
|
||||
void testIndentationClean();
|
||||
|
||||
void testFormatting_data();
|
||||
void testFormatting();
|
||||
#endif
|
||||
};
|
||||
|
||||
|
Reference in New Issue
Block a user