Commit 1e6cd8af authored by Lukas Holecek's avatar Lukas Holecek Committed by hjk
Browse files

fakevim: Improved number transformations in Vim



Allow increasing/decreasing the first number after cursor position
(cursor doesn't have to be on the number).

Increase/decrease number by [count].

Change-Id: I5f4d41fed43556a46c3b63bea4efcdd6a66a81a4
Reviewed-by: default avatarhjk <qthjk@ovi.com>
parent b9e550f7
......@@ -224,6 +224,52 @@ void FakeVimPlugin::test_vim_fFtT()
KEYS("F(", "123()456" N "a" X "(b(c)d)e");
}
void FakeVimPlugin::test_vim_transform_numbers()
{
TestData data;
setup(&data);
data.setText("8");
KEYS("<c-a>", X "9");
KEYS("<c-x>", X "8");
KEYS("<c-a>", X "9");
KEYS("<c-a>", "1" X "0");
KEYS("<c-a>", "1" X "1");
KEYS("5<c-a>", "1" X "6");
KEYS("10<c-a>", "2" X "6");
KEYS("h100<c-a>", "12" X "6");
KEYS("100<c-x>", "2" X "6");
KEYS("10<c-x>", "1" X "6");
KEYS("5<c-x>", "1" X "1");
KEYS("5<c-x>", X "6");
KEYS("6<c-x>", X "0");
KEYS("<c-x>", "-" X "1");
KEYS("h10<c-x>", "-1" X "1");
KEYS("h100<c-x>", "-11" X "1");
KEYS("h889<c-x>", "-100" X "0");
// increase nearest number
data.setText("x-x+x: 1 2 3 -4 5");
KEYS("8<c-a>", "x-x+x: " X "9 2 3 -4 5");
KEYS("l8<c-a>", "x-x+x: 9 1" X "0 3 -4 5");
KEYS("l8<c-a>", "x-x+x: 9 10 1" X "1 -4 5");
KEYS("l16<c-a>", "x-x+x: 9 10 11 1" X "2 5");
KEYS("w18<c-x>", "x-x+x: 9 10 11 12 -1" X "3");
KEYS("hh13<c-a>", "x-x+x: 9 10 11 12 " X "0");
KEYS("B12<c-x>", "x-x+x: 9 10 11 " X "0 0");
KEYS("B11<c-x>", "x-x+x: 9 10 " X "0 0 0");
KEYS("B10<c-x>", "x-x+x: 9 " X "0 0 0 0");
KEYS("B9<c-x>", "x-x+x: " X "0 0 0 0 0");
KEYS("B9<c-x>", "x-x+x: -" X "9 0 0 0 0");
data.setText("-- 1 --");
KEYS("<c-x>", "-- " X "0 --");
KEYS("<c-x><c-x>", "-- -" X "2 --");
KEYS("2<c-a><c-a>", "-- " X "1 --");
KEYS("<c-a>2<c-a>", "-- " X "4 --");
KEYS(".", "-- " X "6 --");
}
void FakeVimPlugin::test_vim_delete()
{
TestData data;
......
......@@ -1336,7 +1336,7 @@ public:
void selectSentenceTextObject(bool inner);
void selectParagraphTextObject(bool inner);
void selectBlockTextObject(bool inner, char left, char right);
void changeNumberTextObject(bool doIncrement);
void changeNumberTextObject(int count);
void selectQuotedStringTextObject(bool inner, const QString &quote);
Q_SLOT void importSelection();
......@@ -2894,8 +2894,9 @@ EventResult FakeVimHandler::Private::handleCommandMode1(const Input &input)
m_lastInsertion.clear();
updateMiniBuffer();
} else if (input.isControl('a')) {
changeNumberTextObject(true);
changeNumberTextObject(count());
setDotCommand("%1<c-a>", count());
finishMovement();
} else if (input.is('b') || input.isShift(Key_Left)) {
m_movetype = MoveExclusive;
moveToNextWordStart(count(), false, false);
......@@ -3347,8 +3348,9 @@ EventResult FakeVimHandler::Private::handleCommandMode2(const Input &input)
setDotCommand("%1x", count());
finishMovement();
} else if (input.isControl('x')) {
changeNumberTextObject(false);
setDotCommand("%1<c-a>", count());
changeNumberTextObject(-count());
setDotCommand("%1<c-x>", count());
finishMovement();
} else if (input.is('X')) {
if (leftDist() > 0) {
setAnchor();
......@@ -6079,36 +6081,33 @@ static bool isSign(const QChar c)
return c.unicode() == '-' || c.unicode() == '+';
}
void FakeVimHandler::Private::changeNumberTextObject(bool doIncrement)
void FakeVimHandler::Private::changeNumberTextObject(int count)
{
QTextCursor tc = cursor();
int pos = tc.position();
const int n = lastPositionInDocument();
const int n = lastPositionInLine(lineForPosition(pos));
QTextDocument *doc = document();
QChar c = doc->characterAt(pos);
if (!c.isNumber()) {
if (pos == n || !isSign(c))
while (!c.isNumber()) {
if (pos == n)
return;
++pos;
c = doc->characterAt(pos);
if (!c.isNumber())
return;
}
int p1 = pos;
while (p1 >= 1 && doc->characterAt(p1 - 1).isNumber())
--p1;
if (p1 >= 1 && isSign(doc->characterAt(p1 - 1)))
--p1;
int p2 = pos;
while (p2 <= n - 1 && doc->characterAt(p2 + 1).isNumber())
int p2 = pos + 1;
while (p2 <= n - 1 && doc->characterAt(p2).isNumber())
++p2;
++p2;
setAnchorAndPosition(p2, p1);
setAnchorAndPosition(p1, p2);
QString orig = selectText(currentRange());
int value = orig.toInt();
value = doIncrement ? value + 1 : value - 1;
QString repl = QString::fromLatin1("%1").arg(value, orig.size(), 10, QLatin1Char('0'));
value += count;
QString repl = QString::fromLatin1("%1").arg(value);
replaceText(currentRange(), repl);
moveLeft();
}
......
......@@ -62,6 +62,7 @@ private:
private slots:
void test_vim_movement();
void test_vim_fFtT();
void test_vim_transform_numbers();
void test_vim_delete();
void test_vim_delete_inner_word();
void test_vim_delete_a_word();
......
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