Commit 76d40842 authored by hluk's avatar hluk Committed by hjk
Browse files

FakeVim: Bracket movement commands



Added commands ]], [[, [], ][ for movement.

Task-number: QTCREATORBUG-9527
Change-Id: I0c363427505c9ef6274ed1cf735c9fbc43cb4bd9
Reviewed-by: default avatarhjk <hjk121@nokiamail.com>
parent debfc171
......@@ -454,6 +454,79 @@ void FakeVimPlugin::test_vim_movement()
data.setText("abc def");
KEYS("}", "abc de" X "f");
KEYS("{", X "abc def");
// bracket movement commands
data.setText(
"void a()" N
"{" N
"}" N "" N "int b()" N
"{ return 0; }" N "" N "int c()" N
"{ return 0;" N
"}" N
"");
KEYS("]]",
"void a()" N
X "{" N
"}" N "" N "int b()" N
"{ return 0; }" N "" N "int c()" N
"{ return 0;" N
"}" N
"");
KEYS("]]",
"void a()" N
"{" N
"}" N "" N "int b()" N
X "{ return 0; }" N "" N "int c()" N
"{ return 0;" N
"}" N
"");
KEYS("2[[",
X "void a()" N
"{" N
"}" N "" N "int b()" N
"{ return 0; }" N "" N "int c()" N
"{ return 0;" N
"}" N
"");
KEYS("4]]",
"void a()" N
"{" N
"}" N "" N "int b()" N
"{ return 0; }" N "" N "int c()" N
"{ return 0;" N
"}" N
X "");
KEYS("2[]",
"void a()" N
"{" N
X "}" N "" N "int b()" N
"{ return 0; }" N "" N "int c()" N
"{ return 0;" N
"}" N
"");
KEYS("][",
"void a()" N
"{" N
"}" N "" N "int b()" N
"{ return 0; }" N "" N "int c()" N
"{ return 0;" N
X "}" N
"");
KEYS("][",
"void a()" N
"{" N
"}" N "" N "int b()" N
"{ return 0; }" N "" N "int c()" N
"{ return 0;" N
"}" N
X "");
}
void FakeVimPlugin::test_vim_insert()
......@@ -809,6 +882,61 @@ void FakeVimPlugin::test_vim_delete()
data.setText("abc" N "def");
KEYS("2lvox", "a" X "b" N "def");
KEYS("vlox", "a" X "def");
// bracket movement command
data.setText(
"void a()" N
"{" N
"}" N "" N "int b()" N
"{ return 0; }" N "" N "int c()" N
"{ return 0;" N
"}" N
"");
KEYS("d]]",
X "{" N
"}" N "" N "int b()" N
"{ return 0; }" N "" N "int c()" N
"{ return 0;" N
"}" N
"");
KEYS("u",
X "void a()" N
"{" N
"}" N "" N "int b()" N
"{ return 0; }" N "" N "int c()" N
"{ return 0;" N
"}" N
"");
// When ]] is used after an operator, then also stops below a '}' in the first column.
KEYS("jd]]",
"void a()" N
X "" N "int b()" N
"{ return 0; }" N "" N "int c()" N
"{ return 0;" N
"}" N
"");
KEYS("u",
"void a()" N
X "{" N
"}" N "" N "int b()" N
"{ return 0; }" N "" N "int c()" N
"{ return 0;" N
"}" N
"");
// do nothing on failed movement
KEYS("Gd5[[",
"void a()" N
"{" N
"}" N "" N "int b()" N
"{ return 0; }" N "" N "int c()" N
"{ return 0;" N
"}" N
X "");
}
void FakeVimPlugin::test_vim_delete_inner_word()
......
......@@ -584,6 +584,40 @@ static void searchBackward(QTextCursor *tc, QRegExp &needleExp, int *repeat)
tc->setPosition(block.position() + i);
}
// Commands [[, []
static void bracketSearchBackward(QTextCursor *tc, const QString &needleExp, int repeat)
{
QRegExp re(needleExp);
QTextCursor tc2 = *tc;
tc2.setPosition(tc2.position() - 1);
searchBackward(&tc2, re, &repeat);
if (repeat <= 1)
tc->setPosition(tc2.isNull() ? 0 : tc2.position(), KeepAnchor);
}
// Commands ][, ]]
// When ]] is used after an operator, then also stops below a '}' in the first column.
static void bracketSearchForward(QTextCursor *tc, const QString &needleExp, int repeat,
bool searchWithCommand)
{
QRegExp re(searchWithCommand ? QString(_("^\\}|^\\{")) : needleExp);
QTextCursor tc2 = *tc;
tc2.setPosition(tc2.position() + 1);
searchForward(&tc2, re, &repeat);
if (repeat <= 1) {
if (tc2.isNull()) {
tc->setPosition(tc->document()->characterCount() - 1, KeepAnchor);
} else {
tc->setPosition(tc2.position() - 1, KeepAnchor);
if (searchWithCommand && tc->document()->characterAt(tc->position()).unicode() == '}') {
QTextBlock block = tc->block().next();
if (block.isValid())
tc->setPosition(block.position(), KeepAnchor);
}
}
}
}
static bool substituteText(QString *text, QRegExp &pattern, const QString &replacement,
bool global)
{
......@@ -2897,7 +2931,7 @@ void FakeVimHandler::Private::fixSelection()
}
}
if (g.movetype == MoveExclusive) {
if (g.movetype == MoveExclusive && g.subsubmode == NoSubSubMode) {
if (anchor() < position() && atBlockStart()) {
// Exlusive motion ending at the beginning of line
// becomes inclusive and end is moved to end of previous line.
......@@ -3321,14 +3355,22 @@ bool FakeVimHandler::Private::handleCommandSubSubMode(const Input &input)
}
} else if (g.subsubmode == OpenSquareSubSubMode || CloseSquareSubSubMode) {
int pos = position();
if ((input.is('{') && g.subsubmode == OpenSquareSubSubMode))
if (input.is('{') && g.subsubmode == OpenSquareSubSubMode)
searchBalanced(false, QLatin1Char('{'), QLatin1Char('}'));
else if ((input.is('}') && g.subsubmode == CloseSquareSubSubMode))
else if (input.is('}') && g.subsubmode == CloseSquareSubSubMode)
searchBalanced(true, QLatin1Char('}'), QLatin1Char('{'));
else if ((input.is('(') && g.subsubmode == OpenSquareSubSubMode))
else if (input.is('(') && g.subsubmode == OpenSquareSubSubMode)
searchBalanced(false, QLatin1Char('('), QLatin1Char(')'));
else if ((input.is(')') && g.subsubmode == CloseSquareSubSubMode))
else if (input.is(')') && g.subsubmode == CloseSquareSubSubMode)
searchBalanced(true, QLatin1Char(')'), QLatin1Char('('));
else if (input.is('[') && g.subsubmode == OpenSquareSubSubMode)
bracketSearchBackward(&m_cursor, _("^\\{"), count());
else if (input.is('[') && g.subsubmode == CloseSquareSubSubMode)
bracketSearchForward(&m_cursor, _("^\\}"), count(), false);
else if (input.is(']') && g.subsubmode == OpenSquareSubSubMode)
bracketSearchBackward(&m_cursor, _("^\\}"), count());
else if (input.is(']') && g.subsubmode == CloseSquareSubSubMode)
bracketSearchForward(&m_cursor, _("^\\{"), count(), g.submode != NoSubMode);
else if (input.is('z'))
emit q->foldGoTo(g.subsubmode == OpenSquareSubSubMode ? -count() : count(), true);
handled = pos != position();
......@@ -3629,8 +3671,6 @@ bool FakeVimHandler::Private::handleMovement(const Input &input)
movePageUp(count);
handleStartOfLine();
movement = _("b");
} else if (input.isKey(Key_BracketLeft) || input.isKey(Key_BracketRight)) {
} else {
handled = false;
}
......
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