diff --git a/config/absytree_config_wasm.wasm b/config/absytree_config_wasm.wasm index 118cedd6..992f2578 100755 Binary files a/config/absytree_config_wasm.wasm and b/config/absytree_config_wasm.wasm differ diff --git a/config/keybindings_vim.nim b/config/keybindings_vim.nim index 34f25cfd..6351a06c 100644 --- a/config/keybindings_vim.nim +++ b/config/keybindings_vim.nim @@ -262,3 +262,92 @@ proc loadVimKeybindings*() {.scriptActionWasmNims("load-vim-keybindings").} = editor.setMode("insert") editor.scrollToCursor(Last) editor.updateTargetColumn(Last) + + + block: # model + setHandleInputs "editor.model", false + setOption "editor.model.cursor.wide.", true + addCommand("editor.model", "", "move-cursor-left-line") + addCommand("editor.model", "", "move-cursor-right-line") + addCommand("editor.model", "", "move-cursor-left") + addCommand("editor.model", "", "move-cursor-right") + addCommand("editor.model", "", "move-cursor-up") + addCommand("editor.model", "", "move-cursor-down") + addCommand("editor.model", "", "select-node") + addCommand("editor.model", "", "select-parent-cell") + addCommand("editor.model", "", "move-cursor-down") + addCommand("editor.model", "", "move-cursor-left-cell") + addCommand("editor.model", "", "move-cursor-right-cell") + addCommand("editor.model", "b", "move-cursor-left-cell") + addCommand("editor.model", "w", "move-cursor-right-cell") + addCommand("editor.model", "", "move-cursor-line-start") + addCommand("editor.model", "", "move-cursor-line-end") + addCommand("editor.model", "", "move-cursor-line-start-inline") + addCommand("editor.model", "", "move-cursor-line-end-inline") + + addCommand("editor.model", "", "move-cursor-left-line", true) + addCommand("editor.model", "", "move-cursor-right-line", true) + addCommand("editor.model", "", "move-cursor-left", true) + addCommand("editor.model", "", "move-cursor-right", true) + addCommand("editor.model", "", "move-cursor-up", true) + addCommand("editor.model", "", "move-cursor-down", true) + addCommand("editor.model", "", "move-cursor-up", true) + addCommand("editor.model", "", "move-cursor-down", true) + addCommand("editor.model", "", "move-cursor-left-cell", true) + addCommand("editor.model", "", "move-cursor-right-cell", true) + addCommand("editor.model", "", "move-cursor-line-start", true) + addCommand("editor.model", "", "move-cursor-line-end", true) + addCommand("editor.model", "", "move-cursor-line-start-inline", true) + addCommand("editor.model", "", "move-cursor-line-end-inline", true) + + addCommand("editor.model", "", "undo") + addCommand("editor.model", "", "redo") + addCommand("editor.model", "u", "undo") + addCommand("editor.model", "U", "redo") + addCommand("editor.model", "", "delete-left") + addCommand("editor.model", "", "delete-right") + addCommand("editor.model", "", "create-new-node") + addCommand("editor.model", "", "select-next-placeholder") + addCommand("editor.model", "", "select-prev-placeholder") + + addCommand("editor.model", "", "show-completions") + + addCommand("editor.model", "", "toggle-use-default-cell-builder") + + addCommand("editor.model", "R", "run-selected-function") + + addCommand("editor.model.completion", "", "finish-edit", true) + addCommand("editor.model.completion", "", "hide-completions") + addCommand("editor.model.completion", "", "select-prev-completion") + addCommand("editor.model.completion", "", "select-next-completion") + addCommand("editor.model.completion", "", "move-cursor-start") + addCommand("editor.model.completion", "", "apply-selected-completion") + + addCommand "editor.model.goto", "", "end" + + addModelCommandBlock "", "": + editor.setMode("") + # editor.selection = editor.selection.last.toSelection + addModelCommandBlock "", "": + editor.setMode("") + # editor.selection = editor.selection.last.toSelection + addModelCommandBlock "", "": + editor.setMode("") + # editor.selection = editor.selection.last.toSelection + + addModelCommand "", "i", "set-mode", "insert" + addModelCommandBlock "", "I": + editor.moveCursorLineStart(false) + editor.setMode("insert") + addModelCommandBlock "", "a": + # editor.selections = editor.selections.mapIt(editor.doMoveCursorColumn(it.last, 1).toSelection) + editor.setMode("insert") + addModelCommandBlock "", "A": + editor.moveCursorLineEnd(false) + editor.setMode("insert") + + # Insert mode + setHandleInputs "editor.model.insert", true + setOption "editor.model.cursor.wide.insert", false + addModelCommand "insert", "", "insert-text-at-cursor", "\n" + addModelCommand "insert", "", "insert-text-at-cursor", " " diff --git a/scripting/absytree_runtime.nim b/scripting/absytree_runtime.nim index b784369b..761f7da8 100644 --- a/scripting/absytree_runtime.nim +++ b/scripting/absytree_runtime.nim @@ -239,7 +239,41 @@ proc setTextInputHandler*(context: string, action: proc(editor: TextDocumentEdit scriptSetCallback("editor.text.input-handler." & context, id) setHandleInputs("editor.text." & context, true) -# Text commands +# Model commands +template addModelCommandBlock*(mode: static[string], keys: string, body: untyped): untyped = + let context = if mode.len == 0: "editor.model" else: "editor.model." & mode + addCommand context, keys, proc() = + let editor {.inject.} = ModelDocumentEditor(id: getActiveEditor()) + body + +proc addModelCommand*(mode: string, keys: string, action: proc(editor: ModelDocumentEditor): void) = + let context = if mode.len == 0: "editor.model" else: "editor.model." & mode + addCommand context, keys, proc() = + action(ModelDocumentEditor(id: getActiveEditor())) + +macro addModelCommand*(mode: static[string], keys: string, action: string, args: varargs[untyped]): untyped = + let context = if mode.len == 0: "editor.model" else: "editor.model." & mode + var stmts = nnkStmtList.newTree() + let str = nskVar.genSym "str" + stmts.add quote do: + var `str` = "" + for arg in args: + stmts.add quote do: + `str`.add " " + `str`.add `arg`.toJsonString + + return genAst(stmts, context, keys, action, str): + stmts + addCommandScript(context, keysPrefix & keys, action, str) + +proc setModelInputHandler*(context: string, action: proc(editor: ModelDocumentEditor, input: string): bool) = + let id = addCallback proc(args: JsonNode): bool = + let input = args.str + action(ModelDocumentEditor(id: getActiveEditor()), input) + scriptSetCallback("editor.model.input-handler." & context, id) + setHandleInputs("editor.model." & context, true) + +# Ast commands template addAstCommandBlock*(mode: static[string], keys: string, body: untyped): untyped = let context = if mode.len == 0: "editor.ast" else: "editor.ast." & mode addCommand context, keys, proc() =