Skip to content

Commit 411416f

Browse files
authored
Feature/move lines up down (#1232)
* Move lines up/down
1 parent 6f4b022 commit 411416f

File tree

3 files changed

+46
-3
lines changed

3 files changed

+46
-3
lines changed

plug-api/syscalls/editor.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,14 @@ export function deleteLine(): Promise<void> {
406406
return syscall("editor.deleteLine");
407407
}
408408

409+
export function moveLineUp(): Promise<void> {
410+
return syscall("editor.moveLineUp");
411+
}
412+
413+
export function moveLineDown(): Promise<void> {
414+
return syscall("editor.moveLineDown");
415+
}
416+
409417
// Vim-mode specific syscalls
410418

411419
/**

plugs/editor/outline.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,15 @@ export async function moveItemUp() {
55
const text = await editor.getText();
66

77
try {
8-
const currentItemBounds = determineItemBounds(text, cursorPos);
8+
let currentItemBounds: ReturnType<typeof determineItemBounds> | undefined;
9+
try {
10+
currentItemBounds = determineItemBounds(text, cursorPos);
11+
} catch {
12+
// If `determineItemBounds()` throws, that likely means the cursor is NOT in a bullet list,
13+
// so we fall back to `moveLineUp()`
14+
editor.moveLineUp();
15+
return;
16+
}
917
let previousItemBounds: ReturnType<typeof determineItemBounds> | undefined;
1018

1119
try {
@@ -69,7 +77,15 @@ export async function moveItemDown() {
6977
const text = await editor.getText();
7078

7179
try {
72-
const currentItemBounds = determineItemBounds(text, cursorPos);
80+
let currentItemBounds: ReturnType<typeof determineItemBounds> | undefined;
81+
try {
82+
currentItemBounds = determineItemBounds(text, cursorPos);
83+
} catch {
84+
// If `determineItemBounds()` throws, that likely means the cursor is NOT in a bullet list,
85+
// so we fall back to `moveLineDown()`
86+
editor.moveLineDown();
87+
return;
88+
}
7389
let nextItemBounds: ReturnType<typeof determineItemBounds> | undefined;
7490
try {
7591
nextItemBounds = determineItemBounds(

web/syscalls/editor.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,14 @@ import {
66
unfoldAll,
77
unfoldCode,
88
} from "@codemirror/language";
9-
import { deleteLine, isolateHistory, redo, undo } from "@codemirror/commands";
9+
import {
10+
deleteLine,
11+
isolateHistory,
12+
moveLineDown,
13+
moveLineUp,
14+
redo,
15+
undo,
16+
} from "@codemirror/commands";
1017
import type { Transaction } from "@codemirror/state";
1118
import { EditorView } from "@codemirror/view";
1219
import { getCM as vimGetCm, Vim } from "@replit/codemirror-vim";
@@ -309,6 +316,18 @@ export function editorSyscalls(client: Client): SysCallMapping {
309316
"editor.deleteLine": () => {
310317
deleteLine(client.editorView);
311318
},
319+
"editor.moveLineUp": () => {
320+
return moveLineUp({
321+
state: client.editorView.state,
322+
dispatch: client.editorView.dispatch,
323+
});
324+
},
325+
"editor.moveLineDown": () => {
326+
return moveLineDown({
327+
state: client.editorView.state,
328+
dispatch: client.editorView.dispatch,
329+
});
330+
},
312331
// Folding
313332
"editor.fold": () => {
314333
foldCode(client.editorView);

0 commit comments

Comments
 (0)