[vim] implement gi gI gJ

This commit is contained in:
nightwing 2020-01-12 00:52:24 +04:00 committed by Marijn Haverbeke
parent 93659d990f
commit a173882ef0
2 changed files with 32 additions and 4 deletions

View File

@ -164,7 +164,9 @@
{ keys: 'A', type: 'action', action: 'enterInsertMode', isEdit: true, actionArgs: { insertAt: 'eol' }, context: 'normal' },
{ keys: 'A', type: 'action', action: 'enterInsertMode', isEdit: true, actionArgs: { insertAt: 'endOfSelectedArea' }, context: 'visual' },
{ keys: 'i', type: 'action', action: 'enterInsertMode', isEdit: true, actionArgs: { insertAt: 'inplace' }, context: 'normal' },
{ keys: 'gi', type: 'action', action: 'enterInsertMode', isEdit: true, actionArgs: { insertAt: 'lastEdit' }, context: 'normal' },
{ keys: 'I', type: 'action', action: 'enterInsertMode', isEdit: true, actionArgs: { insertAt: 'firstNonBlank'}, context: 'normal' },
{ keys: 'gI', type: 'action', action: 'enterInsertMode', isEdit: true, actionArgs: { insertAt: 'bol'}, context: 'normal' },
{ keys: 'I', type: 'action', action: 'enterInsertMode', isEdit: true, actionArgs: { insertAt: 'startOfSelectedArea' }, context: 'visual' },
{ keys: 'o', type: 'action', action: 'newLineAndEnterInsertMode', isEdit: true, interlaceInsertRepeat: true, actionArgs: { after: true }, context: 'normal' },
{ keys: 'O', type: 'action', action: 'newLineAndEnterInsertMode', isEdit: true, interlaceInsertRepeat: true, actionArgs: { after: false }, context: 'normal' },
@ -174,6 +176,7 @@
{ keys: '<C-q>', type: 'action', action: 'toggleVisualMode', actionArgs: { blockwise: true }},
{ keys: 'gv', type: 'action', action: 'reselectLastSelection' },
{ keys: 'J', type: 'action', action: 'joinLines', isEdit: true },
{ keys: 'gJ', type: 'action', action: 'joinLines', actionArgs: { keepSpaces: true }, isEdit: true },
{ keys: 'p', type: 'action', action: 'paste', isEdit: true, actionArgs: { after: true, isEdit: true }},
{ keys: 'P', type: 'action', action: 'paste', isEdit: true, actionArgs: { after: false, isEdit: true }},
{ keys: 'r<character>', type: 'action', action: 'replace', isEdit: true },
@ -2380,6 +2383,8 @@
var height = cm.listSelections().length;
if (insertAt == 'eol') {
head = Pos(head.line, lineLength(cm, head.line));
} else if (insertAt == 'bol') {
head = Pos(head.line, 0);
} else if (insertAt == 'charAfter') {
head = offsetCursor(head, 0, 1);
} else if (insertAt == 'firstNonBlank') {
@ -2418,6 +2423,8 @@
if (vim.visualMode){
return;
}
} else if (insertAt == 'lastEdit') {
head = getLastEditPos(cm) || head;
}
cm.setOption('disableInput', false);
if (actionArgs && actionArgs.replace) {
@ -2526,7 +2533,9 @@
var tmp = Pos(curStart.line + 1,
lineLength(cm, curStart.line + 1));
var text = cm.getRange(curStart, tmp);
text = text.replace(/\n\s*/g, ' ');
text = actionArgs.keepSpaces
? text.replace(/\n\r?/g, '')
: text.replace(/\n\s*/g, ' ');
cm.replaceRange(text, curStart, tmp);
}
var curFinalPos = Pos(curStart.line, finalCh);

View File

@ -1608,13 +1608,13 @@ testVim('i_forward_delete', function(cm, vim, helpers) {
}, { value: 'A1234\nBCD'});
testVim('forward_delete', function(cm, vim, helpers) {
cm.setCursor(0, 3);
helpers.doInsertModeKeys('Delete');
helpers.doKeys('<Del>');
helpers.assertCursorAt(0, 3);
eq('A124\nBCD', cm.getValue());
helpers.doInsertModeKeys('Delete');
helpers.doKeys('<Del>');
helpers.assertCursorAt(0, 2);
eq('A12\nBCD', cm.getValue());
helpers.doInsertModeKeys('Delete');
helpers.doKeys('<Del>');
helpers.assertCursorAt(0, 1);
eq('A1\nBCD', cm.getValue());
}, { value: 'A1234\nBCD'});
@ -1690,6 +1690,25 @@ testVim('J_repeat', function(cm, vim, helpers) {
eq(expectedValue, cm.getValue());
helpers.assertCursorAt(0, expectedValue.indexOf('word3') - 1);
}, { value: 'word1 \n word2\nword3\n word4' });
testVim('gJ', function(cm, vim, helpers) {
cm.setCursor(0, 4);
helpers.doKeys('g', 'J');
eq('word1word2 \n word3', cm.getValue());
helpers.assertCursorAt(0, 5);
helpers.doKeys('g', 'J');
eq('word1word2 word3', cm.getValue());
helpers.assertCursorAt(0, 11);
}, { value: 'word1\nword2 \n word3' });
testVim('gi', function(cm, vim, helpers) {
cm.setCursor(1, 5);
helpers.doKeys('g', 'I');
helpers.doKeys('a', 'a', '<Esc>', 'k');
eq('12\naa xxxx', cm.getValue());
helpers.assertCursorAt(0, 1);
helpers.doKeys('g', 'i');
helpers.assertCursorAt(1, 2);
eq('vim-insert', cm.getOption('keyMap'));
}, { value: '12\n xxxx' });
testVim('p', function(cm, vim, helpers) {
cm.setCursor(0, 1);
helpers.getRegisterController().pushText('"', 'yank', 'abc\ndef', false);