@@ -143,23 +143,53 @@ export class VimCellManager {
143143
144144 // JUPYTER PATCH BEGIN
145145 // here we insert the jumps to the next cells
146+
146147 if ( line < first || line > last ) {
147148 // var currentCell = ns.notebook.get_selected_cell();
148149 // var currentCell = tracker.activeCell;
149150 // var key = '';
150151 // `currentCell !== null should not be needed since `activeCell`
151152 // is already check against null (row 61). Added to avoid warning.
152153 if ( currentCell !== null && currentCell . model . type === 'markdown' ) {
153- ( currentCell as MarkdownCell ) . rendered = true ;
154+ if ( ! motionArgs . handleArrow ) {
155+ // markdown cells tends to improperly handle arrow keys movement,
156+ // on the way up the cell is rendered, but down movement is ignored
157+ // when use arrows the cell will remain unrendered (need to shift+enter)
158+ // However, this is the same as Jupyter default behaviour
159+ ( currentCell as MarkdownCell ) . rendered = true ;
160+ }
154161 // currentCell.execute();
155162 }
156163 if ( motionArgs . forward ) {
157164 // ns.notebook.select_next();
158- this . _commands . execute ( 'notebook:move-cursor-down' ) ;
165+ if ( ! motionArgs . handleArrow ) {
166+ this . _commands . execute ( 'notebook:move-cursor-down' ) ;
167+ } else {
168+ // This block preventing double cell hop when you use arrow keys for navigation
169+ // also arrow key navigation works properly when current cursor position
170+ // at the beginning of line for up move, and at the end for down move
171+ const cursor = cm . getCursor ( ) ;
172+ const last_char = cm . doc . getLine ( last ) . length ;
173+ if ( cursor . line !== last || cursor . ch !== last_char ) {
174+ cm . setCursor ( { line : last , ch : last_char } ) ;
175+ this . _commands . execute ( 'notebook:move-cursor-down' ) ;
176+ }
177+ }
159178 // key = 'j';
160179 } else {
161180 // ns.notebook.select_prev();
162- this . _commands . execute ( 'notebook:move-cursor-up' ) ;
181+ if ( ! motionArgs . handleArrow ) {
182+ this . _commands . execute ( 'notebook:move-cursor-up' ) ;
183+ } else {
184+ // This block preventing double cell hop when you use arrow keys for navigation
185+ // also arrow key navigation works properly when current cursor position
186+ // at the beginning of line for up move, and at the end for down move
187+ const cursor = cm . getCursor ( ) ;
188+ if ( cursor . line !== 0 || cursor . ch !== 0 ) {
189+ cm . setCursor ( { line : 0 , ch : 0 } ) ;
190+ this . _commands . execute ( 'notebook:move-cursor-up' ) ;
191+ }
192+ }
163193 // key = 'k';
164194 }
165195 return ;
@@ -188,6 +218,20 @@ export class VimCellManager {
188218 } ;
189219 lvim . defineMotion ( 'moveByLinesOrCell' , moveByLinesOrCell ) ;
190220
221+ lvim . mapCommand (
222+ '<Up>' ,
223+ 'motion' ,
224+ 'moveByLinesOrCell' ,
225+ { forward : false , linewise : true , handleArrow : true } ,
226+ { context : 'normal' }
227+ ) ;
228+ lvim . mapCommand (
229+ '<Down>' ,
230+ 'motion' ,
231+ 'moveByLinesOrCell' ,
232+ { forward : true , linewise : true , handleArrow : true } ,
233+ { context : 'normal' }
234+ ) ;
191235 lvim . mapCommand (
192236 'k' ,
193237 'motion' ,
0 commit comments