Commit 2ed88c44 authored by hluk's avatar hluk Committed by hjk
Browse files

FakeVim: Don't overuse cursor setting and scrolling in editor



Set new text cursor and scroll in editor widget only if it's only
needed. Sometimes these action can have side-effects.

Set new cursor and scroll especially after no pending input and commands
are available to process for FakeVim.

Change-Id: I47df4d0328ac990e2e5c6981955ea7d1bd608c71
Reviewed-by: default avatarhjk <hjk121@nokiamail.com>
parent 8b8a5db1
......@@ -2948,3 +2948,357 @@ void FakeVimPlugin::test_macros()
KEYS("gg@x", "abc" N X "def");
data.doCommand("unmap <S-down>");
}
void FakeVimPlugin::test_vim_qtcreator()
{
TestData data;
setup(&data);
// Pass input keys in insert mode to underlying editor widget.
data.doCommand("set passkeys");
data.setText("" N "");
KEYS("i" "void f(int arg1) {<cr>// TODO<cr>;",
"void f(int arg1) {" N
" // TODO" N
" ;" X N
"}" N
"");
KEYS("cc" "assert(arg1 != 0",
"void f(int arg1) {" N
" // TODO" N
" assert(arg1 != 0" X ")" N
"}" N
"");
KEYS("k" "." "A;",
"void f(int arg1) {" N
" assert(arg1 != 0);" X N
" assert(arg1 != 0)" N
"}" N
"");
KEYS("j.",
"void f(int arg1) {" N
" assert(arg1 != 0);" N
" assert(arg1 != 0)" X ";" N
"}" N
"");
KEYS("4b2#",
"void f(int " X "arg1) {" N
" assert(arg1 != 0);" N
" assert(arg1 != 0);" N
"}" N
"");
KEYS("e" "a, int arg2 = 0<esc>" "n",
"void f(int " X "arg1, int arg2 = 0) {" N
" assert(arg1 != 0);" N
" assert(arg1 != 0);" N
"}" N
"");
// Record macro.
KEYS("2j" "qa" "<C-A>" "f!" "2s>=<esc>" "q",
"void f(int arg1, int arg2 = 0) {" N
" assert(arg1 != 0);" N
" assert(arg2 >" X "= 0);" N
"}" N
"");
// Replay macro.
KEYS("n" "@a",
"void f(int arg1, int arg2 = 0) {" N
" assert(arg2 >" X "= 0);" N
" assert(arg2 >= 0);" N
"}" N
"");
// Undo.
KEYS("u",
"void f(int arg1, int arg2 = 0) {" N
" assert(" X "arg1 != 0);" N
" assert(arg2 >= 0);" N
"}" N
"");
KEYS("u",
"void f(int arg1, int arg2 = 0) {" N
" assert(arg1 != 0);" N
" assert(arg2 " X "!= 0);" N
"}" N
"");
KEYS("u",
"void f(int arg1, int arg2 = 0) {" N
" assert(arg1 != 0);" N
" assert(" X "arg1 != 0);" N
"}" N
"");
KEYS("u",
"void f(int arg1" X ") {" N
" assert(arg1 != 0);" N
" assert(arg1 != 0);" N
"}" N
"");
KEYS("u",
"void f(int arg1) {" N
" assert(arg1 != 0);" N
" assert(arg1 != 0" X ")" N
"}" N
"");
KEYS("u",
"void f(int arg1) {" N
" assert(arg1 != 0" X ")" N
" assert(arg1 != 0)" N
"}" N
"");
KEYS("u",
"void f(int arg1) {" N
" " X "// TODO" N
" assert(arg1 != 0)" N
"}" N
"");
KEYS("u",
"void f(int arg1) {" N
" // TODO" N
" " X ";" N
"}" N
"");
// Redo and occasional undo.
KEYS("<C-R>",
"void f(int arg1) {" N
" // TODO" N
" " X "assert(arg1 != 0)" N
"}" N
"");
KEYS("<C-R>",
"void f(int arg1) {" N
" " X "assert(arg1 != 0)" N
" assert(arg1 != 0)" N
"}" N
"");
KEYS("<C-R>",
"void f(int arg1) {" N
" assert(arg1 != 0)" X ";" N
" assert(arg1 != 0)" N
"}" N
"");
KEYS("u",
"void f(int arg1) {" N
" assert(arg1 != 0" X ")" N
" assert(arg1 != 0)" N
"}" N
"");
KEYS("<C-R>",
"void f(int arg1) {" N
" assert(arg1 != 0)" X ";" N
" assert(arg1 != 0)" N
"}" N
"");
KEYS("<C-R>",
"void f(int arg1) {" N
" assert(arg1 != 0);" N
" assert(arg1 != 0)" X ";" N
"}" N
"");
KEYS("<C-R>",
"void f(int arg1" X ", int arg2 = 0) {" N
" assert(arg1 != 0);" N
" assert(arg1 != 0);" N
"}" N
"");
KEYS("<C-R>",
"void f(int arg1, int arg2 = 0) {" N
" assert(arg1 != 0);" N
" assert(" X "arg2 != 0);" N
"}" N
"");
KEYS("<C-R>",
"void f(int arg1, int arg2 = 0) {" N
" assert(arg1 != 0);" N
" assert(arg2 " X ">= 0);" N
"}" N
"");
KEYS("<C-R>",
"void f(int arg1, int arg2 = 0) {" N
" assert(" X "arg2 >= 0);" N
" assert(arg2 >= 0);" N
"}" N
"");
KEYS("3u",
"void f(int arg1, int arg2 = 0) {" N
" assert(arg1 != 0);" N
" assert(" X "arg1 != 0);" N
"}" N
"");
// Repeat last command.
KEYS("w.",
"void f(int arg1, int arg2 = 0) {" N
" assert(arg1 != 0);" N
" assert(arg1 >" X "= 0);" N
"}" N
"");
KEYS("kdd",
"void f(int arg1, int arg2 = 0) {" N
" " X "assert(arg1 >= 0);" N
"}" N
"");
// Make mistakes.
KEYS("4<esc>3<esc>" "2oif (arg3<bs>2<bs>1 > 0) return true;<esc>",
"void f(int arg1, int arg2 = 0) {" N
" assert(arg1 >= 0);" N
" if (arg1 > 0) return true;" N
" if (arg1 > 0) return true" X ";" N
"}" N
"");
// Jumps around and change stuff.
KEYS("gg" "ciw" "bool",
"bool" X " f(int arg1, int arg2 = 0) {" N
" assert(arg1 >= 0);" N
" if (arg1 > 0) return true;" N
" if (arg1 > 0) return true;" N
"}" N
"");
KEYS("`'",
"bool f(int arg1, int arg2 = 0) {" N
" assert(arg1 >= 0);" N
" if (arg1 > 0) return true;" N
" if (arg1 > 0) return true" X ";" N
"}" N
"");
KEYS("caW" " false;",
"bool f(int arg1, int arg2 = 0) {" N
" assert(arg1 >= 0);" N
" if (arg1 > 0) return true;" N
" if (arg1 > 0) return false;" X N
"}" N
"");
KEYS("k.",
"bool f(int arg1, int arg2 = 0) {" N
" assert(arg1 >= 0);" N
" if (arg1 > 0) return false" X ";" N
" if (arg1 > 0) return false;" N
"}" N
"");
// Undo/redo again.
KEYS("u",
"bool f(int arg1, int arg2 = 0) {" N
" assert(arg1 >= 0);" N
" if (arg1 > 0) return" X " true;" N
" if (arg1 > 0) return false;" N
"}" N
"");
KEYS("u",
"bool f(int arg1, int arg2 = 0) {" N
" assert(arg1 >= 0);" N
" if (arg1 > 0) return true;" N
" if (arg1 > 0) return" X " true;" N
"}" N
"");
KEYS("<C-R>",
"bool f(int arg1, int arg2 = 0) {" N
" assert(arg1 >= 0);" N
" if (arg1 > 0) return true;" N
" if (arg1 > 0) return" X " false;" N
"}" N
"");
KEYS("u",
"bool f(int arg1, int arg2 = 0) {" N
" assert(arg1 >= 0);" N
" if (arg1 > 0) return true;" N
" if (arg1 > 0) return" X " true;" N
"}" N
"");
KEYS("u",
X "void f(int arg1, int arg2 = 0) {" N
" assert(arg1 >= 0);" N
" if (arg1 > 0) return true;" N
" if (arg1 > 0) return true;" N
"}" N
"");
// Record long insert mode.
KEYS("qb" "4s" "bool" "<down>" "Q_<insert>ASSERT" "<down><down>" "<insert><bs>2"
"<c-o>2w<delete>1" "<c-o>:s/true/false<cr><esc>" "q",
"bool f(int arg1, int arg2 = 0) {" N
" Q_ASSERT(arg1 >= 0);" N
" if (arg1 > 0) return true;" N
" " X " if (arg2 > 1) return false;" N
"}" N
"");
KEYS("u",
"bool f(int arg1, int arg2 = 0) {" N
" Q_ASSERT(arg1 >= 0);" N
" if (arg1 > 0) return true;" N
X " if (arg2 > 1) return true;" N
"}" N
"");
KEYS("u",
"bool f(int arg1, int arg2 = 0) {" N
" Q_ASSERT(arg1 >= 0);" N
" if (arg1 > 0) return true;" N
" if (arg2 > " X "0) return true;" N
"}" N
"");
KEYS("u",
"bool f(int arg1, int arg2 = 0) {" N
" Q_ASSERT(arg1 >= 0);" N
" if (arg1 > 0) return true;" N
" if (arg1" X " > 0) return true;" N
"}" N
"");
KEYS("u",
"bool f(int arg1, int arg2 = 0) {" N
" " X "assert(arg1 >= 0);" N
" if (arg1 > 0) return true;" N
" if (arg1 > 0) return true;" N
"}" N
"");
KEYS("u",
X "void f(int arg1, int arg2 = 0) {" N
" assert(arg1 >= 0);" N
" if (arg1 > 0) return true;" N
" if (arg1 > 0) return true;" N
"}" N
"");
// Replay.
KEYS("@b",
"bool f(int arg1, int arg2 = 0) {" N
" Q_ASSERT(arg1 >= 0);" N
" if (arg1 > 0) return true;" N
" " X " if (arg2 > 1) return false;" N
"}" N
"");
// Return to the first change.
KEYS("99u" "<C-R>",
X "void f(int arg1) {" N
" // TODO" N
" ;" N
"}" N
"");
KEYS("<C-R>",
"void f(int arg1) {" N
" // TODO" N
" " X "assert(arg1 != 0)" N
"}" N
"");
KEYS("<C-R>",
"void f(int arg1) {" N
" " X "assert(arg1 != 0)" N
" assert(arg1 != 0)" N
"}" N
"");
// Return to the last change.
KEYS("99<C-R>",
"bool f(int arg1, int arg2 = 0) {" N
" Q_ASSERT(arg1 >= 0);" N
" if (arg1 > 0) return true;" N
" if (arg2 > 1) return false;" N
"}" N
"");
}
This diff is collapsed.
......@@ -125,6 +125,9 @@ public slots:
// Set text cursor position. Keeps anchor if in visual mode.
void setTextCursorPosition(int position);
QTextCursor textCursor() const;
void setTextCursor(const QTextCursor &cursor);
bool jumpToLocalMark(QChar mark, bool backTickMode);
signals:
......
......@@ -1406,10 +1406,10 @@ void FakeVimPluginPrivate::findNext(bool reverse)
void FakeVimPluginPrivate::foldToggle(int depth)
{
IEditor *ieditor = EditorManager::currentEditor();
BaseTextEditorWidget *editor = qobject_cast<BaseTextEditorWidget *>(ieditor->widget());
QTC_ASSERT(editor != 0, return);
FakeVimHandler *handler = m_editorToHandler.value(ieditor, 0);
QTC_ASSERT(handler != 0, return);
QTextBlock block = editor->textCursor().block();
QTextBlock block = handler->textCursor().block();
fold(depth, !BaseTextDocumentLayout::isFolded(block));
}
......@@ -1437,6 +1437,8 @@ void FakeVimPluginPrivate::foldAll(bool fold)
void FakeVimPluginPrivate::fold(int depth, bool fold)
{
IEditor *ieditor = EditorManager::currentEditor();
FakeVimHandler *handler = m_editorToHandler.value(ieditor, 0);
QTC_ASSERT(handler != 0, return);
BaseTextEditorWidget *editor = qobject_cast<BaseTextEditorWidget *>(ieditor->widget());
QTC_ASSERT(editor != 0, return);
......@@ -1445,7 +1447,7 @@ void FakeVimPluginPrivate::fold(int depth, bool fold)
qobject_cast<BaseTextDocumentLayout*>(doc->documentLayout());
QTC_ASSERT(documentLayout != 0, return);
QTextBlock block = editor->textCursor().block();
QTextBlock block = handler->textCursor().block();
int indent = BaseTextDocumentLayout::foldingIndent(block);
if (fold) {
if (BaseTextDocumentLayout::isFolded(block)) {
......@@ -1496,10 +1498,10 @@ void FakeVimPluginPrivate::fold(int depth, bool fold)
void FakeVimPluginPrivate::foldGoTo(int count, bool current)
{
IEditor *ieditor = EditorManager::currentEditor();
BaseTextEditorWidget *editor = qobject_cast<BaseTextEditorWidget *>(ieditor->widget());
QTC_ASSERT(editor != 0, return);
FakeVimHandler *handler = m_editorToHandler.value(ieditor, 0);
QTC_ASSERT(handler != 0, return);
QTextCursor tc = editor->textCursor();
QTextCursor tc = handler->textCursor();
QTextBlock block = tc.block();
int pos = -1;
......@@ -1546,7 +1548,7 @@ void FakeVimPluginPrivate::foldGoTo(int count, bool current)
if (pos != -1) {
tc.setPosition(pos, QTextCursor::KeepAnchor);
editor->setTextCursor(tc);
handler->setTextCursor(tc);
}
}
......
......@@ -142,6 +142,8 @@ private slots:
void test_macros();
void test_vim_qtcreator();
// special tests
void test_i_cw_i();
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment