Feature/move lines up down (#1232)

* Move lines up/down
pull/1263/head
Simon Cambier 2025-02-23 20:49:52 +01:00 committed by GitHub
parent 6f4b02298c
commit 411416fe65
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 46 additions and 3 deletions

View File

@ -406,6 +406,14 @@ export function deleteLine(): Promise<void> {
return syscall("editor.deleteLine"); return syscall("editor.deleteLine");
} }
export function moveLineUp(): Promise<void> {
return syscall("editor.moveLineUp");
}
export function moveLineDown(): Promise<void> {
return syscall("editor.moveLineDown");
}
// Vim-mode specific syscalls // Vim-mode specific syscalls
/** /**

View File

@ -5,7 +5,15 @@ export async function moveItemUp() {
const text = await editor.getText(); const text = await editor.getText();
try { try {
const currentItemBounds = determineItemBounds(text, cursorPos); let currentItemBounds: ReturnType<typeof determineItemBounds> | 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<typeof determineItemBounds> | undefined; let previousItemBounds: ReturnType<typeof determineItemBounds> | undefined;
try { try {
@ -69,7 +77,15 @@ export async function moveItemDown() {
const text = await editor.getText(); const text = await editor.getText();
try { try {
const currentItemBounds = determineItemBounds(text, cursorPos); let currentItemBounds: ReturnType<typeof determineItemBounds> | 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<typeof determineItemBounds> | undefined; let nextItemBounds: ReturnType<typeof determineItemBounds> | undefined;
try { try {
nextItemBounds = determineItemBounds( nextItemBounds = determineItemBounds(

View File

@ -6,7 +6,14 @@ import {
unfoldAll, unfoldAll,
unfoldCode, unfoldCode,
} from "@codemirror/language"; } 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 type { Transaction } from "@codemirror/state";
import { EditorView } from "@codemirror/view"; import { EditorView } from "@codemirror/view";
import { getCM as vimGetCm, Vim } from "@replit/codemirror-vim"; import { getCM as vimGetCm, Vim } from "@replit/codemirror-vim";
@ -309,6 +316,18 @@ export function editorSyscalls(client: Client): SysCallMapping {
"editor.deleteLine": () => { "editor.deleteLine": () => {
deleteLine(client.editorView); 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 // Folding
"editor.fold": () => { "editor.fold": () => {
foldCode(client.editorView); foldCode(client.editorView);