Skip to content

Commit

Permalink
added modal keybindings for model editor
Browse files Browse the repository at this point in the history
  • Loading branch information
Nimaoth committed Oct 14, 2023
1 parent eed26bb commit 43e85ed
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 1 deletion.
Binary file modified config/absytree_config_wasm.wasm
Binary file not shown.
89 changes: 89 additions & 0 deletions config/keybindings_vim.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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", "<LEFT>", "move-cursor-left-line")
addCommand("editor.model", "<RIGHT>", "move-cursor-right-line")
addCommand("editor.model", "<A-LEFT>", "move-cursor-left")
addCommand("editor.model", "<A-RIGHT>", "move-cursor-right")
addCommand("editor.model", "<UP>", "move-cursor-up")
addCommand("editor.model", "<DOWN>", "move-cursor-down")
addCommand("editor.model", "<A-UP>", "select-node")
addCommand("editor.model", "<C-UP>", "select-parent-cell")
addCommand("editor.model", "<A-DOWN>", "move-cursor-down")
addCommand("editor.model", "<C-LEFT>", "move-cursor-left-cell")
addCommand("editor.model", "<C-RIGHT>", "move-cursor-right-cell")
addCommand("editor.model", "b", "move-cursor-left-cell")
addCommand("editor.model", "w", "move-cursor-right-cell")
addCommand("editor.model", "<HOME>", "move-cursor-line-start")
addCommand("editor.model", "<END>", "move-cursor-line-end")
addCommand("editor.model", "<A-HOME>", "move-cursor-line-start-inline")
addCommand("editor.model", "<A-END>", "move-cursor-line-end-inline")

addCommand("editor.model", "<S-LEFT>", "move-cursor-left-line", true)
addCommand("editor.model", "<S-RIGHT>", "move-cursor-right-line", true)
addCommand("editor.model", "<SA-LEFT>", "move-cursor-left", true)
addCommand("editor.model", "<SA-RIGHT>", "move-cursor-right", true)
addCommand("editor.model", "<S-UP>", "move-cursor-up", true)
addCommand("editor.model", "<S-DOWN>", "move-cursor-down", true)
addCommand("editor.model", "<SA-UP>", "move-cursor-up", true)
addCommand("editor.model", "<SA-DOWN>", "move-cursor-down", true)
addCommand("editor.model", "<SC-LEFT>", "move-cursor-left-cell", true)
addCommand("editor.model", "<SC-RIGHT>", "move-cursor-right-cell", true)
addCommand("editor.model", "<S-HOME>", "move-cursor-line-start", true)
addCommand("editor.model", "<S-END>", "move-cursor-line-end", true)
addCommand("editor.model", "<SA-HOME>", "move-cursor-line-start-inline", true)
addCommand("editor.model", "<SA-END>", "move-cursor-line-end-inline", true)

addCommand("editor.model", "<C-y>", "undo")
addCommand("editor.model", "<C-z>", "redo")
addCommand("editor.model", "u", "undo")
addCommand("editor.model", "U", "redo")
addCommand("editor.model", "<BACKSPACE>", "delete-left")
addCommand("editor.model", "<DELETE>", "delete-right")
addCommand("editor.model", "<ENTER>", "create-new-node")
addCommand("editor.model", "<TAB>", "select-next-placeholder")
addCommand("editor.model", "<S-TAB>", "select-prev-placeholder")

addCommand("editor.model", "<C-SPACE>", "show-completions")

addCommand("editor.model", "<CA-g>", "toggle-use-default-cell-builder")

addCommand("editor.model", "<S-SPACE>R", "run-selected-function")

addCommand("editor.model.completion", "<ENTER>", "finish-edit", true)
addCommand("editor.model.completion", "<ESCAPE>", "hide-completions")
addCommand("editor.model.completion", "<UP>", "select-prev-completion")
addCommand("editor.model.completion", "<DOWN>", "select-next-completion")
addCommand("editor.model.completion", "<C-SPACE>", "move-cursor-start")
addCommand("editor.model.completion", "<TAB>", "apply-selected-completion")

addCommand "editor.model.goto", "<END>", "end"

addModelCommandBlock "", "<C-e>":
editor.setMode("")
# editor.selection = editor.selection.last.toSelection
addModelCommandBlock "", "<ESCAPE>":
editor.setMode("")
# editor.selection = editor.selection.last.toSelection
addModelCommandBlock "", "<S-ESCAPE>":
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", "<ENTER>", "insert-text-at-cursor", "\n"
addModelCommand "insert", "<SPACE>", "insert-text-at-cursor", " "
36 changes: 35 additions & 1 deletion scripting/absytree_runtime.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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() =
Expand Down

0 comments on commit 43e85ed

Please sign in to comment.