Skip to content

Commit

Permalink
fix visual mode cursor shifted on empty lines (#174)
Browse files Browse the repository at this point in the history
  • Loading branch information
nightwing authored May 23, 2024
1 parent 893666e commit 5a4bd87
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/block-cursor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,11 @@ function measureCursor(cm: CodeMirror, view: EditorView, cursor: SelectionRange,
fatCursor = true;
if (vim.visualBlock && !primary)
return null;
if (cursor.anchor < cursor.head) head--;
if (cursor.anchor < cursor.head) {
let letter = head < view.state.doc.length && view.state.sliceDoc(head, head + 1);
if (letter != "\n")
head--;
}
if (cm.state.overwrite) hCoeff = 0.2;
else if (vim.status) hCoeff = 0.5;
}
Expand Down
25 changes: 25 additions & 0 deletions test/vim_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ function testVim(name, run, opts, expectedFail) {
var cm = CodeMirror(place, vimOpts);
var vim = CodeMirror.Vim.maybeInitVimState_(cm);
CodeMirror.Vim.mapclear();
CodeMirror.Vim.langmap('');

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

testVim('rendered_cursor_position_cm6', function(cm, vim, helpers) {
if (!cm.cm6) return;
cm.setCursor(0, 1);
helpers.doKeys('V');
function testCursorPosition(line, ch) {
cm.refresh();
var coords = cm.charCoords({line, ch});
var cursorRect = cm.getWrapperElement().querySelector(".cm-fat-cursor").getBoundingClientRect();
var contentRect = cm.getInputField().getBoundingClientRect();

is(Math.abs(coords.top - (cursorRect.top - contentRect.top)) < 2);
is(Math.abs(coords.left - (cursorRect.left - contentRect.left)) < 2);
}
testCursorPosition(0, 4);
helpers.doKeys('j');
testCursorPosition(1, 0);
helpers.doKeys('j');
testCursorPosition(2, 0);
helpers.doKeys('j');
testCursorPosition(3, 4);

}, {value: '1234\n\n\n5678\nabcdefg'});



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

0 comments on commit 5a4bd87

Please sign in to comment.