Skip to content

Commit ac0044f

Browse files
mightyguavamarijnh
authored andcommitted
[vim keymap] Add {,} motions
1 parent 6ed23aa commit ac0044f

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

keymap/vim.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,10 @@
115115
motion: 'moveByWords',
116116
motionArgs: { forward: false, wordEnd: true, bigWord: true,
117117
inclusive: true }},
118+
{ keys: ['{'], type: 'motion', motion: 'moveByParagraph',
119+
motionArgs: { forward: false }},
120+
{ keys: ['}'], type: 'motion', motion: 'moveByParagraph',
121+
motionArgs: { forward: true }},
118122
{ keys: ['Ctrl-f'], type: 'motion',
119123
motion: 'moveByPage', motionArgs: { forward: true }},
120124
{ keys: ['Ctrl-b'], type: 'motion',
@@ -933,6 +937,22 @@
933937
cm.setCursor(curStart);
934938
return curEnd;
935939
},
940+
moveByParagraph: function(cm, motionArgs) {
941+
var line = cm.getCursor().line;
942+
var repeat = motionArgs.repeat;
943+
var inc = motionArgs.forward ? 1 : -1;
944+
for (var i = 0; i < repeat; i++) {
945+
if ((!motionArgs.forward && line === 0) ||
946+
(motionArgs.forward && line == cm.lineCount() - 1)) {
947+
break;
948+
}
949+
line += inc;
950+
while (line !== 0 && line != cm.lineCount - 1 && cm.getLine(line)) {
951+
line += inc;
952+
}
953+
}
954+
return { line: line, ch: 0 };
955+
},
936956
moveByWords: function(cm, motionArgs) {
937957
return moveToWord(cm, motionArgs.repeat, !!motionArgs.forward,
938958
!!motionArgs.wordEnd, !!motionArgs.bigWord);

test/vim_test.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,28 @@ testVim('Changing lines after Eol operation', function(cm, vim, helpers) {
266266
// same place we were at on line 1
267267
helpers.assertCursorAt({ line: 5, ch: lines[2].length - 2 });
268268
});
269+
testVim('}', function(cm, vim, helpers) {
270+
cm.setCursor(0, 0);
271+
helpers.doKeys('}');
272+
helpers.assertCursorAt(1, 0);
273+
cm.setCursor(0, 0);
274+
helpers.doKeys('2', '}');
275+
helpers.assertCursorAt(4, 0);
276+
cm.setCursor(0, 0);
277+
helpers.doKeys('6', '}');
278+
helpers.assertCursorAt(5, 0);
279+
}, { value: 'a\n\nb\nc\n\nd' });
280+
testVim('{', function(cm, vim, helpers) {
281+
cm.setCursor(5, 0);
282+
helpers.doKeys('{');
283+
helpers.assertCursorAt(4, 0);
284+
cm.setCursor(5, 0);
285+
helpers.doKeys('2', '{');
286+
helpers.assertCursorAt(1, 0);
287+
cm.setCursor(5, 0);
288+
helpers.doKeys('6', '{');
289+
helpers.assertCursorAt(0, 0);
290+
}, { value: 'a\n\nb\nc\n\nd' });
269291

270292
// Operator tests
271293
testVim('dl', function(cm, vim, helpers) {

0 commit comments

Comments
 (0)