aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDavid Schulz <david.schulz@digia.com>2013-08-08 08:43:04 +0200
committerDavid Schulz <david.schulz@digia.com>2013-08-08 09:20:07 +0200
commit11d2d7447671c5aaf8e8b718df0fdf8e58f76c80 (patch)
tree4f97cfd0673d932522854821b689da963fefb381
parent48f161641ab2fbec0cb1f9473aff830887c6a113 (diff)
Editor: Keep selection on updateCurrentSelection().
Task-number: QTCREATORBUG-9959 Change-Id: Ib3324e78bc36790e588c56af7474d3eb968032dc Reviewed-by: Nikolai Kosjar <nikolai.kosjar@digia.com>
-rw-r--r--src/plugins/texteditor/texteditorplugin.cpp16
1 files changed, 9 insertions, 7 deletions
diff --git a/src/plugins/texteditor/texteditorplugin.cpp b/src/plugins/texteditor/texteditorplugin.cpp
index ce2da09161..5bd897625e 100644
--- a/src/plugins/texteditor/texteditorplugin.cpp
+++ b/src/plugins/texteditor/texteditorplugin.cpp
@@ -322,19 +322,21 @@ void TextEditorPlugin::updateVariable(const QByteArray &variable)
void TextEditorPlugin::updateCurrentSelection(const QString &text)
{
- Core::IEditor *iface = Core::EditorManager::currentEditor();
- ITextEditor *editor = qobject_cast<ITextEditor *>(iface);
- if (editor) {
- int pos = editor->position();
+ if (ITextEditor *editor = qobject_cast<ITextEditor *>(Core::EditorManager::currentEditor())) {
+ const int pos = editor->position();
int anchor = editor->position(ITextEditor::Anchor);
if (anchor < 0) // no selection
anchor = pos;
- int selectionLength = anchor-pos;
- if (selectionLength < 0)
+ int selectionLength = pos - anchor;
+ const bool selectionInTextDirection = selectionLength >= 0;
+ if (!selectionInTextDirection)
selectionLength = -selectionLength;
- int start = qMin(pos, anchor);
+ const int start = qMin(pos, anchor);
editor->setCursorPosition(start);
editor->replace(selectionLength, text);
+ const int replacementEnd = editor->position();
+ editor->setCursorPosition(selectionInTextDirection ? start : replacementEnd);
+ editor->select(selectionInTextDirection ? replacementEnd : start);
}
}