Skip to content

Commit

Permalink
[vim] fix R key in visual mode
Browse files Browse the repository at this point in the history
  • Loading branch information
nightwing authored and marijnh committed Jan 12, 2020
1 parent 4d0e99d commit 32da49c
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 3 deletions.
18 changes: 15 additions & 3 deletions keymap/vim.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,8 @@
{ keys: '@<character>', type: 'action', action: 'replayMacro' },
{ keys: 'q<character>', type: 'action', action: 'enterMacroRecordMode' },
// Handle Replace-mode as a special case of insert mode.
{ keys: 'R', type: 'action', action: 'enterInsertMode', isEdit: true, actionArgs: { replace: true }},
{ keys: 'R', type: 'action', action: 'enterInsertMode', isEdit: true, actionArgs: { replace: true }, context: 'normal'},
{ keys: 'R', type: 'operator', operator: 'change', operatorArgs: { linewise: true, fullLine: true }, context: 'visual', exitVisualBlock: true},
{ keys: 'u', type: 'action', action: 'undo', context: 'normal' },
{ keys: 'u', type: 'operator', operator: 'changeCase', operatorArgs: {toLower: true}, context: 'visual', isEdit: true },
{ keys: 'U', type: 'operator', operator: 'changeCase', operatorArgs: {toLower: false}, context: 'visual', isEdit: true },
Expand Down Expand Up @@ -1293,6 +1294,10 @@
}
inputState.operator = command.operator;
inputState.operatorArgs = copyArgs(command.operatorArgs);
if (command.exitVisualBlock) {
vim.visualBlock = false;
updateCmSelection(cm);
}
if (vim.visualMode) {
// Operating on a selection in visual mode. We don't need a motion.
this.evalInput(cm, vim);
Expand Down Expand Up @@ -2104,9 +2109,9 @@
change: function(cm, args, ranges) {
var finalHead, text;
var vim = cm.state.vim;
var anchor = ranges[0].anchor,
head = ranges[0].head;
if (!vim.visualMode) {
var anchor = ranges[0].anchor,
head = ranges[0].head;
text = cm.getRange(anchor, head);
var lastState = vim.lastEditInputState || {};
if (lastState.motion == "moveByWords" && !isWhiteSpaceString(text)) {
Expand Down Expand Up @@ -2134,6 +2139,13 @@
anchor.ch = Number.MAX_VALUE;
}
finalHead = anchor;
} else if (args.fullLine) {
head.ch = Number.MAX_VALUE;
head.line--;
cm.setSelection(anchor, head)
text = cm.getSelection();
cm.replaceSelection("");
finalHead = anchor;
} else {
text = cm.getSelection();
var replacement = fillArray('', ranges.length);
Expand Down
2 changes: 2 additions & 0 deletions test/driver.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ function indexOf(collection, elt) {
}

function test(name, run, expectedFail) {
if (!/^vim/.test(name)) return
console.log(name)
// Force unique names
var originalName = name;
var i = 2; // Second function would be NAME_2
Expand Down
12 changes: 12 additions & 0 deletions test/vim_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1800,6 +1800,18 @@ testVim('R', function(cm, vim, helpers) {
eq('vim-replace', cm.getOption('keyMap'));
is(cm.state.overwrite, 'Setting overwrite state failed');
});
testVim('R_visual', function(cm, vim, helpers) {
helpers.doKeys('<C-v>', 'j', 'R', '0', '<Esc>');
eq('0\nb33\nc44\nc55', cm.getValue());
helpers.doKeys('2', 'j', '.');
eq('0\nb33\n0', cm.getValue());
helpers.doKeys('k', 'v', 'R', '1', '<Esc>');
eq('0\n1\n0', cm.getValue());
helpers.doKeys('k', '.');
eq('1\n1\n0', cm.getValue());
helpers.doKeys('p');
eq('1\n0\n1\n0', cm.getValue());
}, {value: 'a11\na22\nb33\nc44\nc55'});
testVim('mark', function(cm, vim, helpers) {
cm.setCursor(2, 2);
helpers.doKeys('m', 't');
Expand Down

0 comments on commit 32da49c

Please sign in to comment.