diff --git a/src/plugins/fakevim/fakevim_test.cpp b/src/plugins/fakevim/fakevim_test.cpp index 900f44568bf..e86f0168bd2 100644 --- a/src/plugins/fakevim/fakevim_test.cpp +++ b/src/plugins/fakevim/fakevim_test.cpp @@ -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("", X "9"); + KEYS("", X "8"); + KEYS("", X "9"); + KEYS("", "1" X "0"); + KEYS("", "1" X "1"); + KEYS("5", "1" X "6"); + KEYS("10", "2" X "6"); + KEYS("h100", "12" X "6"); + KEYS("100", "2" X "6"); + KEYS("10", "1" X "6"); + KEYS("5", "1" X "1"); + KEYS("5", X "6"); + KEYS("6", X "0"); + KEYS("", "-" X "1"); + KEYS("h10", "-1" X "1"); + KEYS("h100", "-11" X "1"); + KEYS("h889", "-100" X "0"); + + // increase nearest number + data.setText("x-x+x: 1 2 3 -4 5"); + KEYS("8", "x-x+x: " X "9 2 3 -4 5"); + KEYS("l8", "x-x+x: 9 1" X "0 3 -4 5"); + KEYS("l8", "x-x+x: 9 10 1" X "1 -4 5"); + KEYS("l16", "x-x+x: 9 10 11 1" X "2 5"); + KEYS("w18", "x-x+x: 9 10 11 12 -1" X "3"); + KEYS("hh13", "x-x+x: 9 10 11 12 " X "0"); + KEYS("B12", "x-x+x: 9 10 11 " X "0 0"); + KEYS("B11", "x-x+x: 9 10 " X "0 0 0"); + KEYS("B10", "x-x+x: 9 " X "0 0 0 0"); + KEYS("B9", "x-x+x: " X "0 0 0 0 0"); + KEYS("B9", "x-x+x: -" X "9 0 0 0 0"); + + data.setText("-- 1 --"); + KEYS("", "-- " X "0 --"); + KEYS("", "-- -" X "2 --"); + KEYS("2", "-- " X "1 --"); + KEYS("2", "-- " X "4 --"); + KEYS(".", "-- " X "6 --"); +} + void FakeVimPlugin::test_vim_delete() { TestData data; diff --git a/src/plugins/fakevim/fakevimhandler.cpp b/src/plugins/fakevim/fakevimhandler.cpp index 6049d926f33..f2d32c969a3 100644 --- a/src/plugins/fakevim/fakevimhandler.cpp +++ b/src/plugins/fakevim/fakevimhandler.cpp @@ -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 "e); 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", 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", count()); + changeNumberTextObject(-count()); + setDotCommand("%1", 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(); } diff --git a/src/plugins/fakevim/fakevimplugin.h b/src/plugins/fakevim/fakevimplugin.h index 553a23aeb06..e45621f04bd 100644 --- a/src/plugins/fakevim/fakevimplugin.h +++ b/src/plugins/fakevim/fakevimplugin.h @@ -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();