Skip to content

Commit 5a4bd87

Browse files
authored
fix visual mode cursor shifted on empty lines (#174)
1 parent 893666e commit 5a4bd87

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

src/block-cursor.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,11 @@ function measureCursor(cm: CodeMirror, view: EditorView, cursor: SelectionRange,
158158
fatCursor = true;
159159
if (vim.visualBlock && !primary)
160160
return null;
161-
if (cursor.anchor < cursor.head) head--;
161+
if (cursor.anchor < cursor.head) {
162+
let letter = head < view.state.doc.length && view.state.sliceDoc(head, head + 1);
163+
if (letter != "\n")
164+
head--;
165+
}
162166
if (cm.state.overwrite) hCoeff = 0.2;
163167
else if (vim.status) hCoeff = 0.5;
164168
}

test/vim_test.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ function testVim(name, run, opts, expectedFail) {
168168
var cm = CodeMirror(place, vimOpts);
169169
var vim = CodeMirror.Vim.maybeInitVimState_(cm);
170170
CodeMirror.Vim.mapclear();
171+
CodeMirror.Vim.langmap('');
171172

172173
cm.focus();
173174
// workaround for cm5 slow polling in blurred window
@@ -5676,6 +5677,30 @@ isOldCodeMirror || testVim('langmap_visual_block_no_ctrl_remap', function(cm, vi
56765677
eq('1hworld\n5hworld\nahworld', cm.getValue());
56775678
}, {value: '1234\n5678\nabcdefg'});
56785679

5680+
testVim('rendered_cursor_position_cm6', function(cm, vim, helpers) {
5681+
if (!cm.cm6) return;
5682+
cm.setCursor(0, 1);
5683+
helpers.doKeys('V');
5684+
function testCursorPosition(line, ch) {
5685+
cm.refresh();
5686+
var coords = cm.charCoords({line, ch});
5687+
var cursorRect = cm.getWrapperElement().querySelector(".cm-fat-cursor").getBoundingClientRect();
5688+
var contentRect = cm.getInputField().getBoundingClientRect();
5689+
5690+
is(Math.abs(coords.top - (cursorRect.top - contentRect.top)) < 2);
5691+
is(Math.abs(coords.left - (cursorRect.left - contentRect.left)) < 2);
5692+
}
5693+
testCursorPosition(0, 4);
5694+
helpers.doKeys('j');
5695+
testCursorPosition(1, 0);
5696+
helpers.doKeys('j');
5697+
testCursorPosition(2, 0);
5698+
helpers.doKeys('j');
5699+
testCursorPosition(3, 4);
5700+
5701+
}, {value: '1234\n\n\n5678\nabcdefg'});
5702+
5703+
56795704

56805705
async function delay(t) {
56815706
return await new Promise(resolve => setTimeout(resolve, t));

0 commit comments

Comments
 (0)