Skip to content

Commit

Permalink
Don't render Markdown when attempting to navigate above first cell (#119
Browse files Browse the repository at this point in the history
)

* 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
  • Loading branch information
firai committed Oct 12, 2023
1 parent 47abfc4 commit 78509bc
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 13 deletions.
37 changes: 29 additions & 8 deletions src/codemirrorCommands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ interface IUndoOptions {
registerName: unknown;
}

export interface ICellContext {
index?: number;
cellCount?: number;
}

export class VimEditorManager {
constructor({ enabled, userKeybindings }: VimEditorManager.IOptions) {
this.enabled = enabled;
Expand Down Expand Up @@ -167,21 +172,29 @@ export class VimCellManager extends VimEditorManager {
tracker: INotebookTracker,
activeCell: Cell<ICellModel> | null
): void {
this.modifyCell(activeCell).catch(console.error);
const activeCellContext = {
index: tracker.currentWidget?.content.activeCellIndex,
cellCount: tracker.currentWidget?.content.widgets.length
} as ICellContext;
this.modifyCell(activeCell, activeCellContext).catch(console.error);
}

updateLastActive() {
if (!this._lastActiveCell) {
if (!this._lastActiveCell || !this._lastActiveCellContext) {
return;
}
this.modifyCell(this._lastActiveCell);
this.modifyCell(this._lastActiveCell, this._lastActiveCellContext);
}

async modifyCell(activeCell: Cell<ICellModel> | null): Promise<void> {
if (!activeCell) {
async modifyCell(
activeCell: Cell<ICellModel> | null,
activeCellContext: ICellContext
): Promise<void> {
if (!activeCell || !activeCellContext) {
return;
}
this._lastActiveCell = activeCell;
this._lastActiveCellContext = activeCellContext;
await activeCell.ready;

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

private _modifyEdgeNavigation(activeCell: Cell<ICellModel>) {
private _modifyEdgeNavigation(
activeCell: Cell<ICellModel>,
activeCellContext: ICellContext
) {
// 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 +270,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 && activeCellContext.index === 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 +388,5 @@ export class VimCellManager extends VimEditorManager {

private _commands: CommandRegistry;
private _lastActiveCell: Cell<ICellModel> | null = null;
private _lastActiveCellContext: ICellContext | undefined;
}
19 changes: 16 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ import { Prec } from '@codemirror/state';
import {
VimEditorManager,
VimCellManager,
IKeybinding
IKeybinding,
ICellContext
} from './codemirrorCommands';
import { addNotebookCommands } from './labCommands';
import { PartialJSONObject } from '@lumino/coreutils';
Expand Down Expand Up @@ -97,8 +98,13 @@ async function activateCellVim(
} else if (editorTracker.currentWidget === current) {
editorManager.modifyEditor(editorTracker.currentWidget.content.editor);
} else if (notebookTracker.currentWidget === current) {
const activeCellContext = {
index: notebookTracker.currentWidget.content.activeCellIndex,
cellCount: notebookTracker.currentWidget.content.widgets.length
} as ICellContext;
cellManager.modifyCell(
notebookTracker.currentWidget.content.activeCell
notebookTracker.currentWidget.content.activeCell,
activeCellContext
);
} else {
console.warn('Current widget is not vim-enabled');
Expand Down Expand Up @@ -156,7 +162,14 @@ 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);
const activeCellContext = {
index: notebookTracker.currentWidget.content.activeCellIndex,
cellCount: notebookTracker.currentWidget.content.widgets.length
} as ICellContext;
cellManager.modifyCell(
notebookTracker.currentWidget.content.activeCell,
activeCellContext
);
} else {
// no-op
}
Expand Down
5 changes: 3 additions & 2 deletions src/labCommands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,15 +210,16 @@ export function addNotebookCommands(
isEnabled
}),
commands.addCommand('vim:select-above-execute-markdown', {
label: 'Execute Markdown and Select Cell Below',
label: 'Execute Markdown and Select Cell Above',
execute: args => {
const current = getCurrent(args);

if (current) {
const { content } = current;
if (
content.activeCell !== null &&
content.activeCell.model.type === 'markdown'
content.activeCell.model.type === 'markdown' &&
content.activeCellIndex !== 0
) {
(current.content.activeCell as MarkdownCell).rendered = true;
}
Expand Down

0 comments on commit 78509bc

Please sign in to comment.