From fbfd8efdf99c3cd6823e56b1b7b7e305ca346935 Mon Sep 17 00:00:00 2001 From: Alex Veden Date: Wed, 8 Feb 2023 10:17:19 +0400 Subject: [PATCH 1/4] Arrow navigation fixes --- src/codemirrorCommands.ts | 50 ++++++++++++++++++++++++++++++++++++--- 1 file changed, 47 insertions(+), 3 deletions(-) diff --git a/src/codemirrorCommands.ts b/src/codemirrorCommands.ts index 524f2c3..a96983a 100644 --- a/src/codemirrorCommands.ts +++ b/src/codemirrorCommands.ts @@ -143,6 +143,7 @@ export class VimCellManager { // JUPYTER PATCH BEGIN // here we insert the jumps to the next cells + if (line < first || line > last) { // var currentCell = ns.notebook.get_selected_cell(); // var currentCell = tracker.activeCell; @@ -150,16 +151,45 @@ export class VimCellManager { // `currentCell !== null should not be needed since `activeCell` // is already check against null (row 61). Added to avoid warning. if (currentCell !== null && currentCell.model.type === 'markdown') { - (currentCell as MarkdownCell).rendered = true; + if (!motionArgs.handleArrow) { + // markdown cells tends to improperly handle arrow keys movement, + // on the way up the cell is rendered, but down movement is ignored + // when use arrows the cell will remain unrendered (need to shift+enter) + // However, this is the same as Jupyter default behaviour + (currentCell as MarkdownCell).rendered = true; + } // currentCell.execute(); } if (motionArgs.forward) { // ns.notebook.select_next(); - this._commands.execute('notebook:move-cursor-down'); + if (!motionArgs.handleArrow) { + this._commands.execute('notebook:move-cursor-down'); + } else { + // This block preventing double cell hop when you use arrow keys for navigation + // also arrow key navigation works properly when current cursor position + // at the beginning of line for up move, and at the end for down move + let cursor = cm.getCursor(); + let last_char = cm.doc.getLine(last).length; + if (cursor.ch !== last_char) { + cm.setCursor({ line: last, ch: last_char }) + this._commands.execute('notebook:move-cursor-down'); + } + } // key = 'j'; } else { // ns.notebook.select_prev(); - this._commands.execute('notebook:move-cursor-up'); + if (!motionArgs.handleArrow) { + this._commands.execute('notebook:move-cursor-up'); + } else { + // This block preventing double cell hop when you use arrow keys for navigation + // also arrow key navigation works properly when current cursor position + // at the beginning of line for up move, and at the end for down move + let cursor = cm.getCursor() + if (cursor.ch !== 0) { + cm.setCursor({ line: 0, ch: 0 }) + this._commands.execute('notebook:move-cursor-up'); + } + } // key = 'k'; } return; @@ -188,6 +218,20 @@ export class VimCellManager { }; lvim.defineMotion('moveByLinesOrCell', moveByLinesOrCell); + lvim.mapCommand( + '', + 'motion', + 'moveByLinesOrCell', + { forward: false, linewise: true, handleArrow: true }, + { context: 'normal' } + ); + lvim.mapCommand( + '', + 'motion', + 'moveByLinesOrCell', + { forward: true, linewise: true, handleArrow: true }, + { context: 'normal' } + ); lvim.mapCommand( 'k', 'motion', From 67bb602202cbfdc603a2574eba6163465f91a639 Mon Sep 17 00:00:00 2001 From: Alex Veden Date: Wed, 8 Feb 2023 10:30:09 +0400 Subject: [PATCH 2/4] Handling repeated actions N N combinations --- src/codemirrorCommands.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/codemirrorCommands.ts b/src/codemirrorCommands.ts index a96983a..d8eeade 100644 --- a/src/codemirrorCommands.ts +++ b/src/codemirrorCommands.ts @@ -170,7 +170,7 @@ export class VimCellManager { // at the beginning of line for up move, and at the end for down move let cursor = cm.getCursor(); let last_char = cm.doc.getLine(last).length; - if (cursor.ch !== last_char) { + if (cursor.line !== last || cursor.ch !== last_char) { cm.setCursor({ line: last, ch: last_char }) this._commands.execute('notebook:move-cursor-down'); } @@ -185,7 +185,7 @@ export class VimCellManager { // also arrow key navigation works properly when current cursor position // at the beginning of line for up move, and at the end for down move let cursor = cm.getCursor() - if (cursor.ch !== 0) { + if (cursor.line !== 0 || cursor.ch !== 0) { cm.setCursor({ line: 0, ch: 0 }) this._commands.execute('notebook:move-cursor-up'); } From 112f6d7f95b4a1c8f557831f5edfea304ef8f68b Mon Sep 17 00:00:00 2001 From: Alex Veden Date: Thu, 16 Feb 2023 10:55:40 +0400 Subject: [PATCH 3/4] fixed lint errors --- src/codemirrorCommands.ts | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/codemirrorCommands.ts b/src/codemirrorCommands.ts index d8eeade..f0711cd 100644 --- a/src/codemirrorCommands.ts +++ b/src/codemirrorCommands.ts @@ -166,12 +166,12 @@ export class VimCellManager { this._commands.execute('notebook:move-cursor-down'); } else { // This block preventing double cell hop when you use arrow keys for navigation - // also arrow key navigation works properly when current cursor position + // also arrow key navigation works properly when current cursor position // at the beginning of line for up move, and at the end for down move - let cursor = cm.getCursor(); + const cursor = cm.getCursor(); let last_char = cm.doc.getLine(last).length; if (cursor.line !== last || cursor.ch !== last_char) { - cm.setCursor({ line: last, ch: last_char }) + cm.setCursor({ line: last, ch: last_char }); this._commands.execute('notebook:move-cursor-down'); } } @@ -182,11 +182,11 @@ export class VimCellManager { this._commands.execute('notebook:move-cursor-up'); } else { // This block preventing double cell hop when you use arrow keys for navigation - // also arrow key navigation works properly when current cursor position + // also arrow key navigation works properly when current cursor position // at the beginning of line for up move, and at the end for down move - let cursor = cm.getCursor() + const cursor = cm.getCursor(); if (cursor.line !== 0 || cursor.ch !== 0) { - cm.setCursor({ line: 0, ch: 0 }) + cm.setCursor({ line: 0, ch: 0 }); this._commands.execute('notebook:move-cursor-up'); } } From 8ac4689b054c8ef61d6b0a27b395b90ce35c8935 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Krassowski?= <5832902+krassowski@users.noreply.github.com> Date: Sun, 12 Mar 2023 23:00:13 +0000 Subject: [PATCH 4/4] Lint --- src/codemirrorCommands.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/codemirrorCommands.ts b/src/codemirrorCommands.ts index f0711cd..c024f8d 100644 --- a/src/codemirrorCommands.ts +++ b/src/codemirrorCommands.ts @@ -169,7 +169,7 @@ export class VimCellManager { // also arrow key navigation works properly when current cursor position // at the beginning of line for up move, and at the end for down move const cursor = cm.getCursor(); - let last_char = cm.doc.getLine(last).length; + const last_char = cm.doc.getLine(last).length; if (cursor.line !== last || cursor.ch !== last_char) { cm.setCursor({ line: last, ch: last_char }); this._commands.execute('notebook:move-cursor-down');