Skip to content

Commit 78509bc

Browse files
authored
Don't render Markdown when attempting to navigate above first cell (#119)
* Don't render md in first cell in vim:select-above * Don't render md when pressing k in first cell * Revise with cell loc context per review * Lint
1 parent 47abfc4 commit 78509bc

File tree

3 files changed

+48
-13
lines changed

3 files changed

+48
-13
lines changed

src/codemirrorCommands.ts

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@ interface IUndoOptions {
4343
registerName: unknown;
4444
}
4545

46+
export interface ICellContext {
47+
index?: number;
48+
cellCount?: number;
49+
}
50+
4651
export class VimEditorManager {
4752
constructor({ enabled, userKeybindings }: VimEditorManager.IOptions) {
4853
this.enabled = enabled;
@@ -167,21 +172,29 @@ export class VimCellManager extends VimEditorManager {
167172
tracker: INotebookTracker,
168173
activeCell: Cell<ICellModel> | null
169174
): void {
170-
this.modifyCell(activeCell).catch(console.error);
175+
const activeCellContext = {
176+
index: tracker.currentWidget?.content.activeCellIndex,
177+
cellCount: tracker.currentWidget?.content.widgets.length
178+
} as ICellContext;
179+
this.modifyCell(activeCell, activeCellContext).catch(console.error);
171180
}
172181

173182
updateLastActive() {
174-
if (!this._lastActiveCell) {
183+
if (!this._lastActiveCell || !this._lastActiveCellContext) {
175184
return;
176185
}
177-
this.modifyCell(this._lastActiveCell);
186+
this.modifyCell(this._lastActiveCell, this._lastActiveCellContext);
178187
}
179188

180-
async modifyCell(activeCell: Cell<ICellModel> | null): Promise<void> {
181-
if (!activeCell) {
189+
async modifyCell(
190+
activeCell: Cell<ICellModel> | null,
191+
activeCellContext: ICellContext
192+
): Promise<void> {
193+
if (!activeCell || !activeCellContext) {
182194
return;
183195
}
184196
this._lastActiveCell = activeCell;
197+
this._lastActiveCellContext = activeCellContext;
185198
await activeCell.ready;
186199

187200
if (activeCell.isDisposed) {
@@ -190,11 +203,14 @@ export class VimCellManager extends VimEditorManager {
190203
}
191204
const wasEnabled = this.modifyEditor(activeCell.editor);
192205
if (wasEnabled) {
193-
this._modifyEdgeNavigation(activeCell);
206+
this._modifyEdgeNavigation(activeCell, activeCellContext);
194207
}
195208
}
196209

197-
private _modifyEdgeNavigation(activeCell: Cell<ICellModel>) {
210+
private _modifyEdgeNavigation(
211+
activeCell: Cell<ICellModel>,
212+
activeCellContext: ICellContext
213+
) {
198214
// Define a function to use as Vim motion
199215
// This replaces the codemirror moveByLines function to
200216
// for jumping between notebook cells.
@@ -254,7 +270,11 @@ export class VimCellManager extends VimEditorManager {
254270
// var key = '';
255271
// `currentCell !== null should not be needed since `activeCell`
256272
// is already check against null (row 61). Added to avoid warning.
257-
if (currentCell !== null && currentCell.model.type === 'markdown') {
273+
if (
274+
currentCell !== null &&
275+
currentCell.model.type === 'markdown' &&
276+
!(!motionArgs.forward && activeCellContext.index === 0)
277+
) {
258278
if (!motionArgs.handleArrow) {
259279
// markdown cells tends to improperly handle arrow keys movement,
260280
// on the way up the cell is rendered, but down movement is ignored
@@ -368,4 +388,5 @@ export class VimCellManager extends VimEditorManager {
368388

369389
private _commands: CommandRegistry;
370390
private _lastActiveCell: Cell<ICellModel> | null = null;
391+
private _lastActiveCellContext: ICellContext | undefined;
371392
}

src/index.ts

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ import { Prec } from '@codemirror/state';
2020
import {
2121
VimEditorManager,
2222
VimCellManager,
23-
IKeybinding
23+
IKeybinding,
24+
ICellContext
2425
} from './codemirrorCommands';
2526
import { addNotebookCommands } from './labCommands';
2627
import { PartialJSONObject } from '@lumino/coreutils';
@@ -97,8 +98,13 @@ async function activateCellVim(
9798
} else if (editorTracker.currentWidget === current) {
9899
editorManager.modifyEditor(editorTracker.currentWidget.content.editor);
99100
} else if (notebookTracker.currentWidget === current) {
101+
const activeCellContext = {
102+
index: notebookTracker.currentWidget.content.activeCellIndex,
103+
cellCount: notebookTracker.currentWidget.content.widgets.length
104+
} as ICellContext;
100105
cellManager.modifyCell(
101-
notebookTracker.currentWidget.content.activeCell
106+
notebookTracker.currentWidget.content.activeCell,
107+
activeCellContext
102108
);
103109
} else {
104110
console.warn('Current widget is not vim-enabled');
@@ -156,7 +162,14 @@ async function activateCellVim(
156162
} else if (editorTracker.currentWidget === current) {
157163
editorManager.modifyEditor(editorTracker.currentWidget.content.editor);
158164
} else if (notebookTracker.currentWidget === current) {
159-
cellManager.modifyCell(notebookTracker.currentWidget.content.activeCell);
165+
const activeCellContext = {
166+
index: notebookTracker.currentWidget.content.activeCellIndex,
167+
cellCount: notebookTracker.currentWidget.content.widgets.length
168+
} as ICellContext;
169+
cellManager.modifyCell(
170+
notebookTracker.currentWidget.content.activeCell,
171+
activeCellContext
172+
);
160173
} else {
161174
// no-op
162175
}

src/labCommands.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,15 +210,16 @@ export function addNotebookCommands(
210210
isEnabled
211211
}),
212212
commands.addCommand('vim:select-above-execute-markdown', {
213-
label: 'Execute Markdown and Select Cell Below',
213+
label: 'Execute Markdown and Select Cell Above',
214214
execute: args => {
215215
const current = getCurrent(args);
216216

217217
if (current) {
218218
const { content } = current;
219219
if (
220220
content.activeCell !== null &&
221-
content.activeCell.model.type === 'markdown'
221+
content.activeCell.model.type === 'markdown' &&
222+
content.activeCellIndex !== 0
222223
) {
223224
(current.content.activeCell as MarkdownCell).rendered = true;
224225
}

0 commit comments

Comments
 (0)