Skip to content

Commit

Permalink
Don't render md when pressing k in first cell
Browse files Browse the repository at this point in the history
  • Loading branch information
firai committed Oct 3, 2023
1 parent 320f23b commit 0e00f0f
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 10 deletions.
31 changes: 23 additions & 8 deletions src/codemirrorCommands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,21 +167,28 @@ export class VimCellManager extends VimEditorManager {
tracker: INotebookTracker,
activeCell: Cell<ICellModel> | null
): void {
this.modifyCell(activeCell).catch(console.error);
this.modifyCell(
activeCell,
tracker.currentWidget?.content.activeCellIndex
).catch(console.error);
}

updateLastActive() {
if (!this._lastActiveCell) {
if (!this._lastActiveCell || this._lastActiveCellIndex === undefined) {
return;
}
this.modifyCell(this._lastActiveCell);
this.modifyCell(this._lastActiveCell, this._lastActiveCellIndex);
}

async modifyCell(activeCell: Cell<ICellModel> | null): Promise<void> {
if (!activeCell) {
async modifyCell(
activeCell: Cell<ICellModel> | null,
activeCellIndex: number | undefined
): Promise<void> {
if (!activeCell || activeCellIndex === undefined) {
return;
}
this._lastActiveCell = activeCell;
this._lastActiveCellIndex = activeCellIndex;
await activeCell.ready;

if (activeCell.isDisposed) {
Expand All @@ -190,11 +197,14 @@ export class VimCellManager extends VimEditorManager {
}
const wasEnabled = this.modifyEditor(activeCell.editor);
if (wasEnabled) {
this._modifyEdgeNavigation(activeCell);
this._modifyEdgeNavigation(activeCell, activeCellIndex);
}
}

private _modifyEdgeNavigation(activeCell: Cell<ICellModel>) {
private _modifyEdgeNavigation(
activeCell: Cell<ICellModel>,
activeCellIndex: number
) {
// Define a function to use as Vim motion
// This replaces the codemirror moveByLines function to
// for jumping between notebook cells.
Expand Down Expand Up @@ -254,7 +264,11 @@ export class VimCellManager extends VimEditorManager {
// var key = '';
// `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') {
if (
currentCell !== null &&
currentCell.model.type === 'markdown' &&
!(!motionArgs.forward && activeCellIndex === 0)
) {
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
Expand Down Expand Up @@ -368,4 +382,5 @@ export class VimCellManager extends VimEditorManager {

private _commands: CommandRegistry;
private _lastActiveCell: Cell<ICellModel> | null = null;
private _lastActiveCellIndex: number | undefined;
}
8 changes: 6 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,8 @@ async function activateCellVim(
editorManager.modifyEditor(editorTracker.currentWidget.content.editor);
} else if (notebookTracker.currentWidget === current) {
cellManager.modifyCell(
notebookTracker.currentWidget.content.activeCell
notebookTracker.currentWidget.content.activeCell,
notebookTracker.currentWidget.content.activeCellIndex
);
} else {
console.warn('Current widget is not vim-enabled');
Expand Down Expand Up @@ -156,7 +157,10 @@ async function activateCellVim(
} else if (editorTracker.currentWidget === current) {
editorManager.modifyEditor(editorTracker.currentWidget.content.editor);
} else if (notebookTracker.currentWidget === current) {
cellManager.modifyCell(notebookTracker.currentWidget.content.activeCell);
cellManager.modifyCell(
notebookTracker.currentWidget.content.activeCell,
notebookTracker.currentWidget.content.activeCellIndex
);
} else {
// no-op
}
Expand Down

0 comments on commit 0e00f0f

Please sign in to comment.