Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't render Markdown when attempting to navigate above first cell #119

Merged
merged 6 commits into from
Oct 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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