Skip to content

Commit 93659d9

Browse files
nightwingmarijnh
authored andcommitted
[vim] fix behavior of ' and ` marks
1 parent 32da49c commit 93659d9

File tree

2 files changed

+40
-17
lines changed

2 files changed

+40
-17
lines changed

keymap/vim.js

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -595,9 +595,16 @@
595595
}
596596
return mark;
597597
}
598+
function find(cm, offset) {
599+
var oldPointer = pointer;
600+
var mark = move(cm, offset);
601+
pointer = oldPointer;
602+
return mark && mark.find();
603+
}
598604
return {
599605
cachedCursor: undefined, //used for # and * jumps
600606
add: add,
607+
find: find,
601608
move: move
602609
};
603610
};
@@ -4336,25 +4343,25 @@
43364343
}
43374344

43384345
function getMarkPos(cm, vim, markName) {
4339-
if (markName == '\'') {
4340-
var history = cm.doc.history.done;
4341-
var event = history[history.length - 2];
4342-
return event && event.ranges && event.ranges[0].head;
4346+
if (markName == '\'' || markName == '`') {
4347+
return vimGlobalState.jumpList.find(cm, -1) || Pos(0, 0);
43434348
} else if (markName == '.') {
4344-
if (cm.doc.history.lastModTime == 0) {
4345-
return // If no changes, bail out; don't bother to copy or reverse history array.
4346-
} else {
4347-
var changeHistory = cm.doc.history.done.filter(function(el){ if (el.changes !== undefined) { return el } });
4348-
changeHistory.reverse();
4349-
var lastEditPos = changeHistory[0].changes[0].to;
4350-
}
4351-
return lastEditPos;
4349+
return getLastEditPos(cm);
43524350
}
43534351

43544352
var mark = vim.marks[markName];
43554353
return mark && mark.find();
43564354
}
43574355

4356+
function getLastEditPos(cm) {
4357+
var done = cm.doc.history.done;
4358+
for (var i = done.length; i--;) {
4359+
if (done[i].changes) {
4360+
return copyCursor(done[i].changes[0].to);
4361+
}
4362+
}
4363+
}
4364+
43584365
var ExCommandDispatcher = function() {
43594366
this.buildCommandMap_();
43604367
};

test/vim_test.js

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1825,15 +1825,31 @@ testVim('mark', function(cm, vim, helpers) {
18251825
helpers.assertCursorAt(2, 3);
18261826
});
18271827
testVim('mark\'', function(cm, vim, helpers) {
1828+
// motions that do not update jumplist
18281829
cm.setCursor(2, 2);
1829-
cm.setCursor(0, 0);
18301830
helpers.doKeys('`', '\'');
1831+
helpers.assertCursorAt(0, 0);
1832+
helpers.doKeys('j', '3', 'l');
1833+
helpers.doKeys('`', '`');
18311834
helpers.assertCursorAt(2, 2);
1832-
cm.setCursor(2, 0);
1833-
cm.replaceRange(' h', cm.getCursor());
1834-
cm.setCursor(0, 0);
1835+
helpers.doKeys('`', '`');
1836+
helpers.assertCursorAt(1, 3);
1837+
// motions that update jumplist
1838+
cm.openDialog = helpers.fakeOpenDialog('=');
1839+
helpers.doKeys('/');
1840+
helpers.assertCursorAt(6, 20);
1841+
helpers.doKeys('`', '`');
1842+
helpers.assertCursorAt(1, 3);
18351843
helpers.doKeys('\'', '\'');
1836-
helpers.assertCursorAt(2, 3);
1844+
helpers.assertCursorAt(6, 2);
1845+
helpers.doKeys('\'', '`');
1846+
helpers.assertCursorAt(1, 1);
1847+
// edits
1848+
helpers.doKeys('g', 'I', '\n', '<Esc>', 'l');
1849+
helpers.doKeys('`', '`');
1850+
helpers.assertCursorAt(7, 2);
1851+
helpers.doKeys('`', '`');
1852+
helpers.assertCursorAt(2, 1);
18371853
});
18381854
testVim('mark.', function(cm, vim, helpers) {
18391855
cm.setCursor(0, 0);

0 commit comments

Comments
 (0)