Skip to content

Commit

Permalink
vim indent in visualBlock mode (#5684)
Browse files Browse the repository at this point in the history
  • Loading branch information
nightwing authored Nov 28, 2024
1 parent 16a75fc commit 3eaf667
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 6 deletions.
39 changes: 34 additions & 5 deletions src/keyboard/vim.js
Original file line number Diff line number Diff line change
Expand Up @@ -3392,8 +3392,40 @@ domLib.importCssString(`.normal-mode .ace_cursor{
},
indent: function(cm, args, ranges) {
var vim = cm.state.vim;
if (cm.indentMore) {
var repeat = (vim.visualMode) ? args.repeat : 1;
// In visual mode, n> shifts the selection right n times, instead of
// shifting n lines right once.
var repeat = (vim.visualMode) ? args.repeat : 1;
if (vim.visualBlock) {
var tabSize = cm.getOption('tabSize');
var indent = cm.getOption('indentWithTabs') ? '\t' : ' '.repeat(tabSize);
var cursor;
for (var i = ranges.length - 1; i >= 0; i--) {
cursor = cursorMin(ranges[i].anchor, ranges[i].head);
if (args.indentRight) {
cm.replaceRange(indent.repeat(repeat), cursor, cursor);
} else {
var text = cm.getLine(cursor.line);
var end = 0;
for (var j = 0; j < repeat; j++) {
var ch = text[cursor.ch + end];
if (ch == '\t') {
end++;
} else if (ch == ' ') {
end++;
for (var k = 1; k < indent.length; k++) {
ch = text[cursor.ch + end];
if (ch !== ' ') break;
end++;
}
} else {
break
}
}
cm.replaceRange('', cursor, offsetCursor(cursor, 0, end));
}
}
return cursor;
} else if (cm.indentMore) {
for (var j = 0; j < repeat; j++) {
if (args.indentRight) cm.indentMore();
else cm.indentLess();
Expand All @@ -3403,9 +3435,6 @@ domLib.importCssString(`.normal-mode .ace_cursor{
var endLine = vim.visualBlock ?
ranges[ranges.length - 1].anchor.line :
ranges[0].head.line;
// In visual mode, n> shifts the selection right n times, instead of
// shifting n lines right once.
var repeat = (vim.visualMode) ? args.repeat : 1;
if (args.linewise) {
// The only way to delete a newline is to delete until the start of
// the next line, so in linewise mode evalInput will include the next
Expand Down
10 changes: 9 additions & 1 deletion src/keyboard/vim_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1571,7 +1571,15 @@ testVim('=', function(cm, vim, helpers) {
var expectedValue = 'word1\nword2\nword3';
helpers.doKeys('=');
eq(expectedValue, cm.getValue());
}, { value: ' word1\n word2\n word3', indentUnit: 2 });
}, { value: ' word1\n word2\n word3', indentUnit: 2 });
testVim('><visualblock', function(cm, vim, helpers) {
cm.setCursor(0, 6);
helpers.doKeys('<C-v>', 'j', 'j');
helpers.doKeys('4', '>');
eq(' word 1\n word 2\n word 3', cm.getValue());
helpers.doKeys('g', 'v', '14', '<');
eq(' word1\n word2\n word3', cm.getValue());
}, { value: ' word1\n word2\n word3', indentUnit: 2 });


// Edit tests
Expand Down

0 comments on commit 3eaf667

Please sign in to comment.