From 0812ba828719cd1db0032f9faac76550d67cdf18 Mon Sep 17 00:00:00 2001 From: Zef Hemel Date: Sun, 16 Mar 2025 15:53:00 +0100 Subject: [PATCH] Added some convenience editor syscalls --- common/space_lua/stdlib/string.ts | 2 +- web/syscalls/editor.ts | 63 ++++++++++++++++++++++++++++++- 2 files changed, 62 insertions(+), 3 deletions(-) diff --git a/common/space_lua/stdlib/string.ts b/common/space_lua/stdlib/string.ts index 16710002..7827aa32 100644 --- a/common/space_lua/stdlib/string.ts +++ b/common/space_lua/stdlib/string.ts @@ -209,7 +209,7 @@ export const stringApi = new LuaTable({ if (!matches) { return null; - } else if (!matches[1]) { + } else if (matches[1] === undefined) { // No captures return matches[0]; } diff --git a/web/syscalls/editor.ts b/web/syscalls/editor.ts index f86ce367..917a0bd3 100644 --- a/web/syscalls/editor.ts +++ b/web/syscalls/editor.ts @@ -39,6 +39,20 @@ export function editorSyscalls(client: Client): SysCallMapping { "editor.getText": () => { return client.editorView.state.sliceDoc(); }, + "editor.getCurrentLine": (): { + from: number; + to: number; + text: string; + textWithCursor: string; + } => { + const pos = client.editorView.state.selection.main.from; + const line = client.editorView.state.doc.lineAt(pos); + return { + ...line, + textWithCursor: line.text.slice(0, pos - line.from) + "|^|" + + line.text.slice(pos - line.from), + }; + }, "editor.setText": (_ctx, newText: string, shouldIsolateHistory = false) => { client.setEditorText(newText, shouldIsolateHistory); }, @@ -193,15 +207,49 @@ export function editorSyscalls(client: Client): SysCallMapping { client.editorView.dispatch({}); }); }, - "editor.insertAtPos": (_ctx, text: string, pos: number) => { + "editor.insertAtPos": ( + _ctx, + text: string, + pos: number, + cursorPlaceHolder = false, + ) => { + let cursorPlaceholderPos = -1; + if (cursorPlaceHolder) { + cursorPlaceholderPos = text.indexOf("|^|"); + text = text.slice(0, cursorPlaceholderPos) + + text.slice(cursorPlaceholderPos + 3); + } client.editorView.dispatch({ changes: { insert: text, from: pos, }, }); + if (cursorPlaceHolder) { + const cursorPos = pos + cursorPlaceholderPos; + client.editorView.dispatch({ + selection: { + anchor: cursorPos, + }, + effects: [ + EditorView.scrollIntoView(cursorPos), + ], + }); + } }, - "editor.replaceRange": (_ctx, from: number, to: number, text: string) => { + "editor.replaceRange": ( + _ctx, + from: number, + to: number, + text: string, + cursorPlaceHolder = false, + ) => { + let cursorPlaceholderPos = -1; + if (cursorPlaceHolder) { + cursorPlaceholderPos = text.indexOf("|^|"); + text = text.slice(0, cursorPlaceholderPos) + + text.slice(cursorPlaceholderPos + 3); + } client.editorView.dispatch({ changes: { insert: text, @@ -209,6 +257,17 @@ export function editorSyscalls(client: Client): SysCallMapping { to: to, }, }); + if (cursorPlaceHolder) { + const cursorPos = from + cursorPlaceholderPos; + client.editorView.dispatch({ + selection: { + anchor: cursorPos, + }, + effects: [ + EditorView.scrollIntoView(cursorPos), + ], + }); + } }, "editor.moveCursor": (_ctx, pos: number, center = false) => { client.editorView.dispatch({