fakevim: Support for count steps in block selections

Added support for combinations like "2vi)" or "3da{".

Change-Id: I548250faca900b6e5c78f1418cbf3cd4591e7e35
Reviewed-by: hjk <qthjk@ovi.com>
This commit is contained in:
Lukas Holecek
2012-08-14 18:53:33 +02:00
committed by hjk
parent a3de38488b
commit e6bb2f644b

View File

@@ -865,7 +865,7 @@ public:
QString lineContents(int line) const; // 1 based line QString lineContents(int line) const; // 1 based line
void setLineContents(int line, const QString &contents); // 1 based line void setLineContents(int line, const QString &contents); // 1 based line
int blockBoundary(const QString &left, const QString &right, int blockBoundary(const QString &left, const QString &right,
bool end) const; // end or start position of current code block bool end, int count) const; // end or start position of current code block
int linesOnScreen() const; int linesOnScreen() const;
int columnsOnScreen() const; int columnsOnScreen() const;
@@ -4911,32 +4911,34 @@ void FakeVimHandler::Private::setLineContents(int line, const QString &contents)
} }
int FakeVimHandler::Private::blockBoundary(const QString &left, int FakeVimHandler::Private::blockBoundary(const QString &left,
const QString &right, bool closing) const const QString &right, bool closing, int count) const
{ {
const QString &begin = closing ? left : right; const QString &begin = closing ? left : right;
const QString &end = closing ? right : left; const QString &end = closing ? right : left;
// match even if cursor is already on opening/closing string // shift cursor if it is already on opening/closing string
// - on opening string:
QTextCursor tc1 = cursor(); QTextCursor tc1 = cursor();
int pos = tc1.position(); int pos = tc1.position();
int max = document()->characterCount();
int sz = left.size(); int sz = left.size();
tc1.setPosition(qMax(pos - sz + 1, 0)); int from = qMax(pos - sz + 1, 0);
tc1 = document()->find(left, tc1); int to = qMin(pos + sz, max);
if (!tc1.isNull() && (tc1.position() - pos) <= sz) { tc1.setPosition(from);
if (!closing) tc1.setPosition(to, KeepAnchor);
return tc1.position() - sz; int i = tc1.selectedText().indexOf(left);
if (i != -1) {
// - on opening string:
tc1.setPosition(from + i + sz);
} else { } else {
// - on closing string:
tc1 = cursor();
sz = right.size(); sz = right.size();
tc1.setPosition(qMax(pos - sz + 1, 0)); from = qMax(pos - sz + 1, 0);
tc1 = document()->find(right, tc1); to = qMin(pos + sz, max);
if (!tc1.isNull() && (tc1.position() - pos) <= sz) { tc1.setPosition(from);
if (closing) tc1.setPosition(to, KeepAnchor);
return tc1.position() - sz; i = tc1.selectedText().indexOf(right);
else if (i != -1) {
tc1.setPosition(tc1.position() - sz); // - on closing string:
tc1.setPosition(from + i);
} else { } else {
tc1 = cursor(); tc1 = cursor();
} }
@@ -4945,11 +4947,13 @@ int FakeVimHandler::Private::blockBoundary(const QString &left,
QTextCursor tc2 = tc1; QTextCursor tc2 = tc1;
QTextDocument::FindFlags flags(closing ? 0 : QTextDocument::FindBackward); QTextDocument::FindFlags flags(closing ? 0 : QTextDocument::FindBackward);
int level = 0; int level = 0;
int counter = 0;
while (true) { while (true) {
tc2 = document()->find(end, tc2, flags); tc2 = document()->find(end, tc2, flags);
if (tc2.isNull()) if (tc2.isNull())
return -1; return -1;
tc1 = document()->find(begin, tc1, flags); if (!tc1.isNull())
tc1 = document()->find(begin, tc1, flags);
while (!tc1.isNull() && (closing ? (tc1 < tc2) : (tc2 < tc1))) { while (!tc1.isNull() && (closing ? (tc1 < tc2) : (tc2 < tc1))) {
++level; ++level;
@@ -4966,7 +4970,9 @@ int FakeVimHandler::Private::blockBoundary(const QString &left,
if (level == 0 if (level == 0
&& (tc1.isNull() || (closing ? (tc2 < tc1) : (tc1 < tc2)))) { && (tc1.isNull() || (closing ? (tc2 < tc1) : (tc1 < tc2)))) {
break; ++counter;
if (counter >= count)
break;
} }
} }
@@ -5245,19 +5251,17 @@ void FakeVimHandler::Private::selectBlockTextObject(bool inner,
QString sleft = QString(QLatin1Char(left)); QString sleft = QString(QLatin1Char(left));
QString sright = QString(QLatin1Char(right)); QString sright = QString(QLatin1Char(right));
int p1 = blockBoundary(sleft, sright, false); int p1 = blockBoundary(sleft, sright, false, count());
if (p1 == -1) if (p1 == -1)
return; return;
int p2 = blockBoundary(sleft, sright, true); int p2 = blockBoundary(sleft, sright, true, count());
if (p2 == -1) if (p2 == -1)
return; return;
if (inner) { if (inner) {
p1 += sleft.size(); p1 += sleft.size();
--p2; --p2;
if (document()->characterAt(p1) == ParagraphSeparator)
++p1;
} else { } else {
p2 -= sright.size() - 1; p2 -= sright.size() - 1;
} }