FakeVim: Skip wrapped parts of lines on up/down movement

Task-number: QTCREATORBUG-9278
Change-Id: Ic3d71b3aa39e90dea242dc25a06fde229e187564
Reviewed-by: hjk <hjk121@nokiamail.com>
This commit is contained in:
hluk
2013-05-09 10:04:40 +02:00
committed by hjk
parent 6d75336da1
commit 1deaed1727

View File

@@ -1526,6 +1526,9 @@ public:
bool end, int count) const; // end or start position of current code block bool end, int count) const; // end or start position of current code block
int lineNumber(const QTextBlock &block) const; int lineNumber(const QTextBlock &block) const;
QTextBlock nextLine(const QTextBlock &block) const; // following line (respects wrapped parts)
QTextBlock previousLine(const QTextBlock &block) const; // previous line (respects wrapped parts)
int linesOnScreen() const; int linesOnScreen() const;
int columnsOnScreen() const; int columnsOnScreen() const;
int linesInDocument() const; int linesInDocument() const;
@@ -2705,14 +2708,25 @@ void FakeVimHandler::Private::pushUndoState(bool overwrite)
void FakeVimHandler::Private::moveDown(int n) void FakeVimHandler::Private::moveDown(int n)
{ {
if (n == 0)
return;
QTextBlock block = m_cursor.block(); QTextBlock block = m_cursor.block();
const int col = position() - block.position(); const int col = position() - block.position();
const int targetLine = qMax(0, lineNumber(block) + n);
block = document()->findBlockByLineNumber(qMax(0, targetLine - 1)); int lines = qAbs(n);
if (!block.isValid()) int position = 0;
block = document()->lastBlock(); while (block.isValid()) {
setPosition(block.position() + qMax(0, qMin(block.length() - 2, col))); position = block.position() + qMax(0, qMin(block.length() - 2, col));
if (block.isVisible()) {
--lines;
if (lines < 0)
break;
}
block = n > 0 ? nextLine(block) : previousLine(block);
}
setPosition(position);
moveToTargetColumn(); moveToTargetColumn();
} }
@@ -4068,9 +4082,9 @@ bool FakeVimHandler::Private::handleChangeDeleteSubModes(const Input &input)
|| (m_submode == DeleteSubMode && input.is('d'))) { || (m_submode == DeleteSubMode && input.is('d'))) {
m_movetype = MoveLineWise; m_movetype = MoveLineWise;
pushUndoState(); pushUndoState();
const int line = cursorLine() + 1; const int anc = firstPositionInLine(cursorLine() + 1);
const int anc = firstPositionInLine(line); moveDown(count() - 1);
const int pos = lastPositionInLine(line + count() - 1); const int pos = lastPositionInLine(cursorLine() + 1);
setAnchorAndPosition(anc, pos); setAnchorAndPosition(anc, pos);
if (m_submode == ChangeSubMode) if (m_submode == ChangeSubMode)
setDotCommand(_("%1cc"), count()); setDotCommand(_("%1cc"), count());
@@ -6982,6 +6996,16 @@ int FakeVimHandler::Private::lineNumber(const QTextBlock &block) const
return block2.firstLineNumber() + 1; return block2.firstLineNumber() + 1;
} }
QTextBlock FakeVimHandler::Private::nextLine(const QTextBlock &block) const
{
return document()->findBlock(block.position() + block.length());
}
QTextBlock FakeVimHandler::Private::previousLine(const QTextBlock &block) const
{
return document()->findBlock(block.position() - 1);
}
int FakeVimHandler::Private::firstPositionInLine(int line, bool onlyVisibleLines) const int FakeVimHandler::Private::firstPositionInLine(int line, bool onlyVisibleLines) const
{ {
QTextBlock block = onlyVisibleLines ? document()->findBlockByLineNumber(line - 1) QTextBlock block = onlyVisibleLines ? document()->findBlockByLineNumber(line - 1)
@@ -6993,8 +7017,11 @@ int FakeVimHandler::Private::lastPositionInLine(int line, bool onlyVisibleLines)
{ {
QTextBlock block; QTextBlock block;
if (onlyVisibleLines) { if (onlyVisibleLines) {
// respect folds block = document()->findBlockByLineNumber(line - 1);
block = document()->findBlockByLineNumber(line); // respect folds and wrapped lines
do {
block = nextLine(block);
} while (block.isValid() && !block.isVisible());
if (block.isValid()) { if (block.isValid()) {
if (line > 0) if (line > 0)
block = block.previous(); block = block.previous();