FakeVim: Move common movement operations to functions

Change-Id: I06dce44098a535e0d8f49d885b77a4f07731288b
Reviewed-by: hjk <hjk121@nokiamail.com>
This commit is contained in:
Lukas Holecek
2014-11-04 18:53:44 +01:00
committed by hjk
parent 743e1ca67e
commit 5458fdee72

View File

@@ -1764,6 +1764,15 @@ public:
void moveDown(int n = 1); void moveDown(int n = 1);
void moveUpVisually(int n = 1) { moveDownVisually(-n); } void moveUpVisually(int n = 1) { moveDownVisually(-n); }
void moveDownVisually(int n = 1); void moveDownVisually(int n = 1);
void moveVertically(int n = 1) {
if (g.gflag) {
g.movetype = MoveExclusive;
moveDownVisually(n);
} else {
g.movetype = MoveLineWise;
moveDown(n);
}
}
void movePageDown(int count = 1); void movePageDown(int count = 1);
void movePageUp(int count = 1) { movePageDown(-count); } void movePageUp(int count = 1) { movePageDown(-count); }
void dump(const char *msg) const { void dump(const char *msg) const {
@@ -1773,7 +1782,6 @@ public:
<< "VISUAL: " << g.visualMode; << "VISUAL: " << g.visualMode;
} }
void moveRight(int n = 1) { void moveRight(int n = 1) {
//dump("RIGHT 1");
if (isVisualCharMode()) { if (isVisualCharMode()) {
const QTextBlock currentBlock = block(); const QTextBlock currentBlock = block();
const int max = currentBlock.position() + currentBlock.length() - 1; const int max = currentBlock.position() + currentBlock.length() - 1;
@@ -1784,10 +1792,21 @@ public:
} }
if (atEndOfLine()) if (atEndOfLine())
emit q->fold(1, false); emit q->fold(1, false);
//dump("RIGHT 2"); setTargetColumn();
} }
void moveLeft(int n = 1) { void moveLeft(int n = 1) {
m_cursor.movePosition(Left, KeepAnchor, n); m_cursor.movePosition(Left, KeepAnchor, n);
setTargetColumn();
}
void moveToNextCharacter() {
moveRight();
if (atEndOfLine())
moveRight();
}
void moveToPreviousCharacter() {
moveLeft();
if (atBlockStart())
moveLeft();
} }
void setAnchor() { void setAnchor() {
m_cursor.setPosition(position(), MoveAnchor); m_cursor.setPosition(position(), MoveAnchor);
@@ -1820,6 +1839,10 @@ public:
m_cursor = EDITOR(textCursor()); m_cursor = EDITOR(textCursor());
} }
// Update selection, record jump and target column if cursor position
// changes externally (e.g. by code completion).
void updateCursorPosition();
// Values to save when starting FakeVim processing. // Values to save when starting FakeVim processing.
int m_firstVisibleLine; int m_firstVisibleLine;
QTextCursor m_cursor; QTextCursor m_cursor;
@@ -2270,15 +2293,14 @@ void FakeVimHandler::Private::focus()
stopIncrementalFind(); stopIncrementalFind();
if (!isInsertMode()) { if (!isInsertMode()) {
if (g.subsubmode == SearchSubSubMode || g.submode != NoSubMode || g.mode == ExMode) {
if (g.subsubmode == SearchSubSubMode) { if (g.subsubmode == SearchSubSubMode) {
setPosition(m_searchStartPosition); setPosition(m_searchStartPosition);
scrollToLine(m_searchFromScreenLine); scrollToLine(m_searchFromScreenLine);
setTargetColumn(); } else {
setAnchor();
commitCursor();
} else if (g.submode != NoSubMode || g.mode == ExMode) {
leaveVisualMode(); leaveVisualMode();
setPosition(qMin(position(), anchor())); setPosition(qMin(position(), anchor()));
}
setTargetColumn(); setTargetColumn();
setAnchor(); setAnchor();
commitCursor(); commitCursor();
@@ -2312,17 +2334,8 @@ void FakeVimHandler::Private::enterFakeVim()
removeEventFilter(); removeEventFilter();
updateFirstVisibleLine(); updateFirstVisibleLine();
importSelection();
// Position changed externally, e.g. by code completion. updateCursorPosition();
if (position() != m_oldInternalPosition) {
// record external jump to different line
if (m_oldInternalPosition != -1 && lineForPosition(m_oldInternalPosition) != lineForPosition(position()))
recordJump(m_oldInternalPosition);
setTargetColumn();
if (atEndOfLine() && !isVisualMode() && !isInsertMode())
moveLeft();
}
if (m_fakeEnd) if (m_fakeEnd)
moveRight(); moveRight();
@@ -2491,14 +2504,10 @@ void FakeVimHandler::Private::setupWidget()
m_wasReadOnly = EDITOR(isReadOnly()); m_wasReadOnly = EDITOR(isReadOnly());
updateEditor(); updateEditor();
importSelection();
updateMiniBuffer(); updateMiniBuffer();
updateCursorShape(); updateCursorShape();
recordJump(); updateCursorPosition();
setTargetColumn();
if (atEndOfLine() && !isVisualMode() && !isInsertMode())
moveLeft();
leaveFakeVim(); leaveFakeVim();
} }
@@ -3107,6 +3116,21 @@ void FakeVimHandler::Private::movePageDown(int count)
scrollToLine(qMax(0, cursorLine() - screenLines + 1)); scrollToLine(qMax(0, cursorLine() - screenLines + 1));
} }
void FakeVimHandler::Private::updateCursorPosition()
{
importSelection();
if (position() != m_oldInternalPosition) {
// record external jump to different line
if (m_oldInternalPosition != -1 && lineForPosition(m_oldInternalPosition) != lineForPosition(position()))
recordJump(m_oldInternalPosition);
setTargetColumn();
if (atEndOfLine() && !isVisualMode() && !isInsertMode())
moveLeft();
}
}
bool FakeVimHandler::Private::moveToNextParagraph(int count) bool FakeVimHandler::Private::moveToNextParagraph(int count)
{ {
const bool forward = count > 0; const bool forward = count > 0;
@@ -3175,6 +3199,7 @@ void FakeVimHandler::Private::moveBehindEndOfLine()
int pos = qMin(block().position() + block().length() - 1, int pos = qMin(block().position() + block().length() - 1,
lastPositionInDocument() + 1); lastPositionInDocument() + 1);
setPosition(pos); setPosition(pos);
setTargetColumn();
} }
void FakeVimHandler::Private::moveToStartOfLine() void FakeVimHandler::Private::moveToStartOfLine()
@@ -3340,10 +3365,6 @@ void FakeVimHandler::Private::finishMovement(const QString &dotCommandMovement)
dotCommand = _("d"); dotCommand = _("d");
if (g.movetype == MoveLineWise) if (g.movetype == MoveLineWise)
handleStartOfLine(); handleStartOfLine();
if (atEndOfLine())
moveLeft();
else
setTargetColumn();
endEditBlock(); endEditBlock();
} else if (g.submode == YankSubMode) { } else if (g.submode == YankSubMode) {
bool isVisualModeYank = isVisualMode(); bool isVisualModeYank = isVisualMode();
@@ -3697,7 +3718,6 @@ bool FakeVimHandler::Private::handleCount(const Input &input)
bool FakeVimHandler::Private::handleMovement(const Input &input) bool FakeVimHandler::Private::handleMovement(const Input &input)
{ {
bool handled = true; bool handled = true;
QString movement;
int count = this->count(); int count = this->count();
if (handleCount(input)) { if (handleCount(input)) {
@@ -3773,7 +3793,6 @@ bool FakeVimHandler::Private::handleMovement(const Input &input)
} else if (input.is('|')) { } else if (input.is('|')) {
moveToStartOfLine(); moveToStartOfLine();
moveRight(qMin(count, rightDist()) - 1); moveRight(qMin(count, rightDist()) - 1);
setTargetColumn();
} else if (input.is('}')) { } else if (input.is('}')) {
handled = moveToNextParagraph(count); handled = moveToNextParagraph(count);
} else if (input.is('{')) { } else if (input.is('{')) {
@@ -3782,21 +3801,16 @@ bool FakeVimHandler::Private::handleMovement(const Input &input)
moveToStartOfLine(); moveToStartOfLine();
moveDown(); moveDown();
moveToFirstNonBlankOnLine(); moveToFirstNonBlankOnLine();
g.movetype = MoveLineWise;
} else if (input.is('-')) { } else if (input.is('-')) {
moveToStartOfLine(); moveToStartOfLine();
moveUp(count); moveUp(count);
moveToFirstNonBlankOnLine(); moveToFirstNonBlankOnLine();
g.movetype = MoveLineWise;
} else if (input.is('+')) { } else if (input.is('+')) {
moveToStartOfLine(); moveToStartOfLine();
moveDown(count); moveDown(count);
moveToFirstNonBlankOnLine(); moveToFirstNonBlankOnLine();
g.movetype = MoveLineWise;
} else if (input.isKey(Key_Home)) { } else if (input.isKey(Key_Home)) {
moveToStartOfLine(); moveToStartOfLine();
setTargetColumn();
movement = _("<HOME>");
} else if (input.is('$') || input.isKey(Key_End)) { } else if (input.is('$') || input.isKey(Key_End)) {
if (g.gflag) { if (g.gflag) {
if (count > 1) if (count > 1)
@@ -3808,12 +3822,10 @@ bool FakeVimHandler::Private::handleMovement(const Input &input)
moveToEndOfLine(); moveToEndOfLine();
} }
g.movetype = atEmptyLine() ? MoveExclusive : MoveInclusive; g.movetype = atEmptyLine() ? MoveExclusive : MoveInclusive;
setTargetColumn();
if (g.submode == NoSubMode) if (g.submode == NoSubMode)
m_targetColumn = -1; m_targetColumn = -1;
if (isVisualMode()) if (isVisualMode())
m_visualTargetColumn = -1; m_visualTargetColumn = -1;
movement = _("$");
} else if (input.is('%')) { } else if (input.is('%')) {
recordJump(); recordJump();
if (g.mvcount == 0) { if (g.mvcount == 0) {
@@ -3827,37 +3839,22 @@ bool FakeVimHandler::Private::handleMovement(const Input &input)
g.movetype = MoveLineWise; g.movetype = MoveLineWise;
} }
} else if (input.is('b') || input.isShift(Key_Left)) { } else if (input.is('b') || input.isShift(Key_Left)) {
g.movetype = MoveExclusive;
moveToNextWordStart(count, false, false); moveToNextWordStart(count, false, false);
setTargetColumn();
movement = _("b");
} else if (input.is('B')) { } else if (input.is('B')) {
g.movetype = MoveExclusive;
moveToNextWordStart(count, true, false); moveToNextWordStart(count, true, false);
setTargetColumn();
} else if (input.is('e') && g.gflag) { } else if (input.is('e') && g.gflag) {
g.movetype = MoveInclusive;
moveToNextWordEnd(count, false, false); moveToNextWordEnd(count, false, false);
setTargetColumn();
} else if (input.is('e') || input.isShift(Key_Right)) { } else if (input.is('e') || input.isShift(Key_Right)) {
g.movetype = MoveInclusive;
moveToNextWordEnd(count, false, true, false); moveToNextWordEnd(count, false, true, false);
setTargetColumn();
movement = _("e");
} else if (input.is('E') && g.gflag) { } else if (input.is('E') && g.gflag) {
g.movetype = MoveInclusive;
moveToNextWordEnd(count, true, false); moveToNextWordEnd(count, true, false);
setTargetColumn();
} else if (input.is('E')) { } else if (input.is('E')) {
g.movetype = MoveInclusive;
moveToNextWordEnd(count, true, true, false); moveToNextWordEnd(count, true, true, false);
setTargetColumn();
} else if (input.isControl('e')) { } else if (input.isControl('e')) {
// FIXME: this should use the "scroll" option, and "count" // FIXME: this should use the "scroll" option, and "count"
if (cursorLineOnScreen() == 0) if (cursorLineOnScreen() == 0)
moveDown(1); moveDown(1);
scrollDown(1); scrollDown(1);
movement = _("<C-E>");
} else if (input.is('f')) { } else if (input.is('f')) {
g.subsubmode = FtSubSubMode; g.subsubmode = FtSubSubMode;
g.movetype = MoveInclusive; g.movetype = MoveInclusive;
@@ -3894,38 +3891,19 @@ bool FakeVimHandler::Private::handleMovement(const Input &input)
if (m_fakeEnd && block().length() > 1) if (m_fakeEnd && block().length() > 1)
++n; ++n;
moveLeft(n); moveLeft(n);
setTargetColumn();
movement = _("h");
} else if (input.is('H')) { } else if (input.is('H')) {
const CursorPosition pos(lineToBlockNumber(lineOnTop(count)), 0); const CursorPosition pos(lineToBlockNumber(lineOnTop(count)), 0);
setCursorPosition(&m_cursor, pos); setCursorPosition(&m_cursor, pos);
handleStartOfLine(); handleStartOfLine();
} else if (input.is('j') || input.isKey(Key_Down) } else if (input.is('j') || input.isKey(Key_Down)
|| input.isControl('j') || input.isControl('n')) { || input.isControl('j') || input.isControl('n')) {
if (g.gflag) { moveVertically(count);
g.movetype = MoveExclusive;
moveDownVisually(count);
movement = _("gj");
} else {
g.movetype = MoveLineWise;
moveDown(count);
movement = _("j");
}
} else if (input.is('k') || input.isKey(Key_Up) || input.isControl('p')) { } else if (input.is('k') || input.isKey(Key_Up) || input.isControl('p')) {
if (g.gflag) { moveVertically(-count);
g.movetype = MoveExclusive;
moveUpVisually(count);
movement = _("gk");
} else {
g.movetype = MoveLineWise;
moveUp(count);
movement = _("k");
}
} else if (input.is('l') || input.isKey(Key_Right) || input.is(' ')) { } else if (input.is('l') || input.isKey(Key_Right) || input.is(' ')) {
g.movetype = MoveExclusive; g.movetype = MoveExclusive;
bool pastEnd = count >= rightDist() - 1; bool pastEnd = count >= rightDist() - 1;
moveRight(qMax(0, qMin(count, rightDist() - (g.submode == NoSubMode)))); moveRight(qMax(0, qMin(count, rightDist() - (g.submode == NoSubMode))));
setTargetColumn();
if (pastEnd && isVisualMode()) if (pastEnd && isVisualMode())
m_visualTargetColumn = -1; m_visualTargetColumn = -1;
} else if (input.is('L')) { } else if (input.is('L')) {
@@ -3970,7 +3948,6 @@ bool FakeVimHandler::Private::handleMovement(const Input &input)
bool simple = input.is('W'); bool simple = input.is('W');
if (g.submode == ChangeSubMode && !document()->characterAt(position()).isSpace()) { if (g.submode == ChangeSubMode && !document()->characterAt(position()).isSpace()) {
moveToWordEnd(count, simple, true); moveToWordEnd(count, simple, true);
g.movetype = MoveInclusive;
} else { } else {
moveToNextWordStart(count, simple, true); moveToNextWordStart(count, simple, true);
// Command 'dw' deletes to the next word on the same line or to end of line. // Command 'dw' deletes to the next word on the same line or to end of line.
@@ -3978,9 +3955,7 @@ bool FakeVimHandler::Private::handleMovement(const Input &input)
const QTextBlock currentBlock = document()->findBlock(anchor()); const QTextBlock currentBlock = document()->findBlock(anchor());
setPosition(qMin(position(), currentBlock.position() + currentBlock.length())); setPosition(qMin(position(), currentBlock.position() + currentBlock.length()));
} }
g.movetype = MoveExclusive;
} }
setTargetColumn();
} else if (input.is('z')) { } else if (input.is('z')) {
g.movetype = MoveLineWise; g.movetype = MoveLineWise;
g.subsubmode = ZSubSubMode; g.subsubmode = ZSubSubMode;
@@ -3991,11 +3966,9 @@ bool FakeVimHandler::Private::handleMovement(const Input &input)
} else if (input.isKey(Key_PageDown) || input.isControl('f')) { } else if (input.isKey(Key_PageDown) || input.isControl('f')) {
movePageDown(count); movePageDown(count);
handleStartOfLine(); handleStartOfLine();
movement = _("f");
} else if (input.isKey(Key_PageUp) || input.isControl('b')) { } else if (input.isKey(Key_PageUp) || input.isControl('b')) {
movePageUp(count); movePageUp(count);
handleStartOfLine(); handleStartOfLine();
movement = _("b");
} else { } else {
handled = false; handled = false;
} }
@@ -4008,7 +3981,7 @@ bool FakeVimHandler::Private::handleMovement(const Input &input)
const QString dotMovement = const QString dotMovement =
(count > 1 ? QString::number(count) : QString()) (count > 1 ? QString::number(count) : QString())
+ _(g.gflag ? "g" : "") + _(g.gflag ? "g" : "")
+ (movement.isNull() ? QString(input.asChar()) : movement); + input.toString();
finishMovement(dotMovement); finishMovement(dotMovement);
setTargetColumn(); setTargetColumn();
} }
@@ -4774,10 +4747,8 @@ void FakeVimHandler::Private::handleReplaceMode(const Input &input)
g.dotCommand.append(m_buffer->lastInsertion + _("<ESC>")); g.dotCommand.append(m_buffer->lastInsertion + _("<ESC>"));
} else if (input.isKey(Key_Left)) { } else if (input.isKey(Key_Left)) {
moveLeft(); moveLeft();
setTargetColumn();
} else if (input.isKey(Key_Right)) { } else if (input.isKey(Key_Right)) {
moveRight(); moveRight();
setTargetColumn();
} else if (input.isKey(Key_Up)) { } else if (input.isKey(Key_Up)) {
moveUp(); moveUp();
} else if (input.isKey(Key_Down)) { } else if (input.isKey(Key_Down)) {
@@ -4880,7 +4851,6 @@ void FakeVimHandler::Private::finishInsertMode()
g.dotCommand.append(m_buffer->lastInsertion + _("<ESC>")); g.dotCommand.append(m_buffer->lastInsertion + _("<ESC>"));
enterCommandMode(); enterCommandMode();
setTargetColumn();
} }
void FakeVimHandler::Private::handleInsertMode(const Input &input) void FakeVimHandler::Private::handleInsertMode(const Input &input)
@@ -4959,10 +4929,8 @@ void FakeVimHandler::Private::handleInsertMode(const Input &input)
g.mode = ReplaceMode; g.mode = ReplaceMode;
} else if (input.isKey(Key_Left)) { } else if (input.isKey(Key_Left)) {
moveLeft(); moveLeft();
setTargetColumn();
} else if (input.isControl(Key_Left)) { } else if (input.isControl(Key_Left)) {
moveToNextWordStart(1, false, false); moveToNextWordStart(1, false, false);
setTargetColumn();
} else if (input.isKey(Key_Down)) { } else if (input.isKey(Key_Down)) {
g.submode = NoSubMode; g.submode = NoSubMode;
moveDown(); moveDown();
@@ -4971,17 +4939,13 @@ void FakeVimHandler::Private::handleInsertMode(const Input &input)
moveUp(); moveUp();
} else if (input.isKey(Key_Right)) { } else if (input.isKey(Key_Right)) {
moveRight(); moveRight();
setTargetColumn();
} else if (input.isControl(Key_Right)) { } else if (input.isControl(Key_Right)) {
moveToNextWordStart(1, false, true); moveToNextWordStart(1, false, true);
moveRight(); // we need one more move since we are in insert mode moveRight(); // we need one more move since we are in insert mode
setTargetColumn();
} else if (input.isKey(Key_Home)) { } else if (input.isKey(Key_Home)) {
moveToStartOfLine(); moveToStartOfLine();
setTargetColumn();
} else if (input.isKey(Key_End)) { } else if (input.isKey(Key_End)) {
moveBehindEndOfLine(); moveBehindEndOfLine();
setTargetColumn();
m_targetColumn = -1; m_targetColumn = -1;
} else if (input.isReturn() || input.isControl('j') || input.isControl('m')) { } else if (input.isReturn() || input.isControl('j') || input.isControl('m')) {
if (!input.isReturn() || !handleInsertInEditor(input)) { if (!input.isReturn() || !handleInsertInEditor(input)) {
@@ -5528,7 +5492,6 @@ bool FakeVimHandler::Private::handleExSubstituteCommand(const ExCommand &cmd)
setPosition(lastBlock.position()); setPosition(lastBlock.position());
setAnchor(); setAnchor();
moveToFirstNonBlankOnLine(); moveToFirstNonBlankOnLine();
setTargetColumn();
endEditBlock(); endEditBlock();
} }
@@ -5952,7 +5915,6 @@ bool FakeVimHandler::Private::handleExReadCommand(const ExCommand &cmd)
beginEditBlock(); beginEditBlock();
moveToStartOfLine(); moveToStartOfLine();
setTargetColumn();
moveDown(); moveDown();
int pos = position(); int pos = position();
@@ -6355,6 +6317,7 @@ void FakeVimHandler::Private::highlightMatches(const QString &needle)
void FakeVimHandler::Private::moveToFirstNonBlankOnLine() void FakeVimHandler::Private::moveToFirstNonBlankOnLine()
{ {
g.movetype = MoveLineWise;
moveToFirstNonBlankOnLine(&m_cursor); moveToFirstNonBlankOnLine(&m_cursor);
setTargetColumn(); setTargetColumn();
} }
@@ -6453,7 +6416,6 @@ void FakeVimHandler::Private::shiftRegionRight(int repeat)
setPosition(targetPos); setPosition(targetPos);
handleStartOfLine(); handleStartOfLine();
setTargetColumn();
const int lines = endLine - beginLine + 1; const int lines = endLine - beginLine + 1;
if (lines > 2) { if (lines > 2) {
@@ -6667,12 +6629,16 @@ void FakeVimHandler::Private::moveToNextWord(bool end, int count, bool simple, b
void FakeVimHandler::Private::moveToNextWordStart(int count, bool simple, bool forward, bool emptyLines) void FakeVimHandler::Private::moveToNextWordStart(int count, bool simple, bool forward, bool emptyLines)
{ {
g.movetype = MoveExclusive;
moveToNextWord(false, count, simple, forward, emptyLines); moveToNextWord(false, count, simple, forward, emptyLines);
setTargetColumn();
} }
void FakeVimHandler::Private::moveToNextWordEnd(int count, bool simple, bool forward, bool emptyLines) void FakeVimHandler::Private::moveToNextWordEnd(int count, bool simple, bool forward, bool emptyLines)
{ {
g.movetype = MoveInclusive;
moveToNextWord(true, count, simple, forward, emptyLines); moveToNextWord(true, count, simple, forward, emptyLines);
setTargetColumn();
} }
void FakeVimHandler::Private::moveToWordStart(int count, bool simple, bool forward, bool emptyLines) void FakeVimHandler::Private::moveToWordStart(int count, bool simple, bool forward, bool emptyLines)
@@ -7943,6 +7909,7 @@ void FakeVimHandler::Private::jump(int distance)
setCursorPosition(from.top()); setCursorPosition(from.top());
from.pop(); from.pop();
} }
setTargetColumn();
} }
Column FakeVimHandler::Private::indentation(const QString &line) const Column FakeVimHandler::Private::indentation(const QString &line) const
@@ -8065,13 +8032,9 @@ void FakeVimHandler::Private::selectTextObject(bool simple, bool inner)
moveToBoundaryStart(1, simple, false); moveToBoundaryStart(1, simple, false);
setAnchor(); setAnchor();
} else if (forward) { } else if (forward) {
moveRight(); moveToNextCharacter();
if (atEndOfLine())
moveRight();
} else { } else {
moveLeft(); moveToPreviousCharacter();
if (atBlockStart())
moveLeft();
} }
if (inner) { if (inner) {
@@ -8115,15 +8078,10 @@ void FakeVimHandler::Private::selectTextObject(bool simple, bool inner)
} }
if (i + 1 < repeat) { if (i + 1 < repeat) {
if (forward) { if (forward)
moveRight(); moveToNextCharacter();
if (atEndOfLine()) else
moveRight(); moveToPreviousCharacter();
} else {
moveLeft();
if (atBlockStart())
moveLeft();
}
} }
} }
} }
@@ -8132,14 +8090,11 @@ void FakeVimHandler::Private::selectTextObject(bool simple, bool inner)
g.movetype = MoveInclusive; g.movetype = MoveInclusive;
} else { } else {
g.movetype = MoveExclusive; g.movetype = MoveExclusive;
if (isNoVisualMode()) { if (isNoVisualMode())
moveRight(); moveToNextCharacter();
if (atEndOfLine()) else if (isVisualLineMode())
moveRight();
} else if (isVisualLineMode()) {
g.visualMode = VisualCharMode; g.visualMode = VisualCharMode;
} }
}
setTargetColumn(); setTargetColumn();
} }