diff --git a/plug-api/syscalls/editor.ts b/plug-api/syscalls/editor.ts index 0f35531a..c477aaa6 100644 --- a/plug-api/syscalls/editor.ts +++ b/plug-api/syscalls/editor.ts @@ -406,6 +406,14 @@ export function deleteLine(): Promise { return syscall("editor.deleteLine"); } +export function moveLineUp(): Promise { + return syscall("editor.moveLineUp"); +} + +export function moveLineDown(): Promise { + return syscall("editor.moveLineDown"); +} + // Vim-mode specific syscalls /** diff --git a/plugs/editor/outline.ts b/plugs/editor/outline.ts index 87a51707..0d902de6 100644 --- a/plugs/editor/outline.ts +++ b/plugs/editor/outline.ts @@ -5,7 +5,15 @@ export async function moveItemUp() { const text = await editor.getText(); try { - const currentItemBounds = determineItemBounds(text, cursorPos); + let currentItemBounds: ReturnType | undefined; + try { + currentItemBounds = determineItemBounds(text, cursorPos); + } catch { + // If `determineItemBounds()` throws, that likely means the cursor is NOT in a bullet list, + // so we fall back to `moveLineUp()` + editor.moveLineUp(); + return; + } let previousItemBounds: ReturnType | undefined; try { @@ -69,7 +77,15 @@ export async function moveItemDown() { const text = await editor.getText(); try { - const currentItemBounds = determineItemBounds(text, cursorPos); + let currentItemBounds: ReturnType | undefined; + try { + currentItemBounds = determineItemBounds(text, cursorPos); + } catch { + // If `determineItemBounds()` throws, that likely means the cursor is NOT in a bullet list, + // so we fall back to `moveLineDown()` + editor.moveLineDown(); + return; + } let nextItemBounds: ReturnType | undefined; try { nextItemBounds = determineItemBounds( diff --git a/web/syscalls/editor.ts b/web/syscalls/editor.ts index 32fe4875..36e819a6 100644 --- a/web/syscalls/editor.ts +++ b/web/syscalls/editor.ts @@ -6,7 +6,14 @@ import { unfoldAll, unfoldCode, } from "@codemirror/language"; -import { deleteLine, isolateHistory, redo, undo } from "@codemirror/commands"; +import { + deleteLine, + isolateHistory, + moveLineDown, + moveLineUp, + redo, + undo, +} from "@codemirror/commands"; import type { Transaction } from "@codemirror/state"; import { EditorView } from "@codemirror/view"; import { getCM as vimGetCm, Vim } from "@replit/codemirror-vim"; @@ -309,6 +316,18 @@ export function editorSyscalls(client: Client): SysCallMapping { "editor.deleteLine": () => { deleteLine(client.editorView); }, + "editor.moveLineUp": () => { + return moveLineUp({ + state: client.editorView.state, + dispatch: client.editorView.dispatch, + }); + }, + "editor.moveLineDown": () => { + return moveLineDown({ + state: client.editorView.state, + dispatch: client.editorView.dispatch, + }); + }, // Folding "editor.fold": () => { foldCode(client.editorView);