Skip to content

Commit

Permalink
Add virtual file system and use it to preview source code of keybindi…
Browse files Browse the repository at this point in the history
…ngs (#25)
  • Loading branch information
Nimaoth authored Sep 8, 2024
1 parent e9a789b commit dc48bfa
Show file tree
Hide file tree
Showing 22 changed files with 631 additions and 128 deletions.
2 changes: 2 additions & 0 deletions config/default_config.nim
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import plugin_runtime
import std/[json]
import misc/[timer]

embedSource()

proc loadDefaultKeybindings*(clearExisting: bool = false) {.expose("load-default-keybindings").} =
let t = startTimer()
defer:
Expand Down
23 changes: 13 additions & 10 deletions config/keybindings_vim.nim
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import plugin_runtime, keybindings_normal
import misc/[timer, util, myjsonutils, custom_unicode]
import input_api

embedSource()

infof"import vim keybindings"

var yankedLines: bool = false ## Whether the last thing we yanked was in a line mode
Expand Down Expand Up @@ -38,16 +40,17 @@ macro addSubCommandWithCount*(mode: string, sub: string, keys: string, move: str
let (stmts, str) = bindArgs(args)
return genAst(stmts, mode, keys, move, str, sub):
stmts
addCommandScript(getContextWithMode("editor.text", mode) & "#" & sub, "", keysPrefix & "<?-count>" & keys, move, str & " <#" & sub & ".count>")
addCommandScript(getContextWithMode("editor.text", mode) & "#" & sub, "", keysPrefix & "<?-count>" & keys, move, str & " <#" & sub & ".count>", source = currentSourceLocation(-2))

proc addSubCommandWithCount*(mode: string, sub: string, keys: string, action: proc(editor: TextDocumentEditor, count: int): void) =
addCommand getContextWithMode("editor.text", mode) & "#" & sub, "<?-count>" & keys, "<#" & sub & ".count>", action
proc addSubCommandWithCount*(mode: string, sub: string, keys: string, action: proc(editor: TextDocumentEditor, count: int): void, source = currentSourceLocation()) =
addCommand getContextWithMode("editor.text", mode) & "#" & sub, "<?-count>" & keys, "<#" & sub & ".count>", action, source

template addSubCommandWithCountBlock*(mode: string, sub: string, keys: string, body: untyped): untyped =
addSubCommandWithCount mode, sub, keys, proc(editor: TextDocumentEditor, count: int): void =
let p = proc(editor: TextDocumentEditor, count: int): void =
let editor {.inject.} = editor
let count {.inject.} = count
body
addSubCommandWithCount mode, sub, keys, p, currentSourceLocation(-2)

template addSubCommand*(mode: string, sub: string, keys: string, move: string, args: varargs[untyped]) =
addTextCommand mode & "#" & sub, keys, move, args
Expand Down Expand Up @@ -915,15 +918,15 @@ proc loadVimKeybindings*() {.expose("load-vim-keybindings").} =
editor.setMode("normal")

addTextCommandBlockDesc "", ".", "replay commands": replayCommands(".")
addCommand "editor.text.normal", "@<CHAR>", "<CHAR>", proc(editor: TextDocumentEditor, c: string) =
addCommand "editor.text.normal", "@<CHAR>", "<CHAR>", source = currentSourceLocation(), action = proc(editor: TextDocumentEditor, c: string) =
let register = if c == "@":
getOption("editor.current-macro-register", "")
else:
c

replayCommands(register)

addCommand "editor.text.normal", "q<CHAR>", "<CHAR>", proc(editor: TextDocumentEditor, c: string) =
addCommand "editor.text.normal", "q<CHAR>", "<CHAR>", source = currentSourceLocation(), action = proc(editor: TextDocumentEditor, c: string) =
if isReplayingCommands() or isRecordingCommands(getCurrentMacroRegister()):
return
setOption("editor.current-macro-register", c)
Expand Down Expand Up @@ -1263,9 +1266,9 @@ proc loadVimKeybindings*() {.expose("load-vim-keybindings").} =
addTextCommand "replace", "<ESCAPE>", "set-mode", "normal"

# Deleting text
addTextCommand "", "x", vimDeleteRight
addTextCommand "", "<DELETE>", vimDeleteRight
addTextCommand "", "X", vimDeleteLeft
addTextCommand "", "x", vimDeleteRight, source = currentSourceLocation()
addTextCommand "", "<DELETE>", vimDeleteRight, source = currentSourceLocation()
addTextCommand "", "X", vimDeleteLeft, source = currentSourceLocation()

addTextCommand "", "u", "vim-undo", enterNormalModeBefore=true
addTextCommand "", "<C-r>", "vim-redo", enterNormalModeBefore=true
Expand Down Expand Up @@ -1455,7 +1458,7 @@ proc loadVimKeybindings*() {.expose("load-vim-keybindings").} =
editor.scrollToCursor Last
editor.updateTargetColumn()

addCommand "editor.text.visual", "S<CHAR>", "<CHAR>", proc(editor: TextDocumentEditor, c: string) =
addCommand "editor.text.visual", "S<CHAR>", "<CHAR>", source = currentSourceLocation(), action = proc(editor: TextDocumentEditor, c: string) =
let (left, right) = case c
of "(", ")": ("(", ")")
of "{", "}": ("{", "}")
Expand Down
Binary file modified config/wasm/harpoon.wasm
Binary file not shown.
Binary file modified config/wasm/keybindings_plugin.wasm
Binary file not shown.
Binary file modified config/wasm/vscode_config_plugin.wasm
Binary file not shown.
18 changes: 13 additions & 5 deletions scripting/editor_api.nim
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,13 @@ proc chooseTheme*() =
editor_chooseTheme_void_App_impl()
proc createFile*(path: string) =
editor_createFile_void_App_string_impl(path)
proc browseKeybinds*() =
editor_browseKeybinds_void_App_impl()
proc mountVfs*(parentPath: string; prefix: string; config: JsonNode) =
editor_mountVfs_void_App_string_string_JsonNode_impl(parentPath, prefix,
config)
proc browseKeybinds*(preview: bool = true; scaleX: float = 0.9;
scaleY: float = 0.8; previewScale: float = 0.4) =
editor_browseKeybinds_void_App_bool_float_float_float_impl(preview, scaleX,
scaleY, previewScale)
proc chooseFile*(preview: bool = true; scaleX: float = 0.8; scaleY: float = 0.8;
previewScale: float = 0.5) =
## Opens a file dialog which shows all files in the currently open workspaces
Expand Down Expand Up @@ -274,11 +279,14 @@ proc setLeaders*(leaders: seq[string]) =
editor_setLeaders_void_App_seq_string_impl(leaders)
proc addLeader*(leader: string) =
editor_addLeader_void_App_string_impl(leader)
proc registerPluginSourceCode*(path: string; content: string) =
editor_registerPluginSourceCode_void_App_string_string_impl(path, content)
proc addCommandScript*(context: string; subContext: string; keys: string;
action: string; arg: string = "";
description: string = "") =
editor_addCommandScript_void_App_string_string_string_string_string_string_impl(
context, subContext, keys, action, arg, description)
description: string = ""; source: tuple[filename: string,
line: int, column: int] = ("", 0, 0)) =
editor_addCommandScript_void_App_string_string_string_string_string_string_tuple_filename_string_line_int_column_int_impl(
context, subContext, keys, action, arg, description, source)
proc removeCommand*(context: string; keys: string) =
editor_removeCommand_void_App_string_string_impl(context, keys)
proc getActivePopup*(): EditorId =
Expand Down
84 changes: 78 additions & 6 deletions scripting/editor_api_wasm.nim
Original file line number Diff line number Diff line change
Expand Up @@ -933,11 +933,58 @@ proc createFile*(path: string) =
argsJsonString.cstring)


proc editor_browseKeybinds_void_App_wasm(arg: cstring): cstring {.importc.}
proc browseKeybinds*() =
proc editor_mountVfs_void_App_string_string_JsonNode_wasm(arg: cstring): cstring {.
importc.}
proc mountVfs*(parentPath: string; prefix: string; config: JsonNode) =
var argsJson = newJArray()
argsJson.add block:
when string is JsonNode:
parentPath
else:
parentPath.toJson()
argsJson.add block:
when string is JsonNode:
prefix
else:
prefix.toJson()
argsJson.add block:
when JsonNode is JsonNode:
config
else:
config.toJson()
let argsJsonString = $argsJson
let res {.used.} = editor_browseKeybinds_void_App_wasm(argsJsonString.cstring)
let res {.used.} = editor_mountVfs_void_App_string_string_JsonNode_wasm(
argsJsonString.cstring)


proc editor_browseKeybinds_void_App_bool_float_float_float_wasm(arg: cstring): cstring {.
importc.}
proc browseKeybinds*(preview: bool = true; scaleX: float = 0.9;
scaleY: float = 0.8; previewScale: float = 0.4) =
var argsJson = newJArray()
argsJson.add block:
when bool is JsonNode:
preview
else:
preview.toJson()
argsJson.add block:
when float is JsonNode:
scaleX
else:
scaleX.toJson()
argsJson.add block:
when float is JsonNode:
scaleY
else:
scaleY.toJson()
argsJson.add block:
when float is JsonNode:
previewScale
else:
previewScale.toJson()
let argsJsonString = $argsJson
let res {.used.} = editor_browseKeybinds_void_App_bool_float_float_float_wasm(
argsJsonString.cstring)


proc editor_chooseFile_void_App_bool_float_float_float_wasm(arg: cstring): cstring {.
Expand Down Expand Up @@ -1371,11 +1418,31 @@ proc addLeader*(leader: string) =
argsJsonString.cstring)


proc editor_addCommandScript_void_App_string_string_string_string_string_string_wasm(
proc editor_registerPluginSourceCode_void_App_string_string_wasm(arg: cstring): cstring {.
importc.}
proc registerPluginSourceCode*(path: string; content: string) =
var argsJson = newJArray()
argsJson.add block:
when string is JsonNode:
path
else:
path.toJson()
argsJson.add block:
when string is JsonNode:
content
else:
content.toJson()
let argsJsonString = $argsJson
let res {.used.} = editor_registerPluginSourceCode_void_App_string_string_wasm(
argsJsonString.cstring)


proc editor_addCommandScript_void_App_string_string_string_string_string_string_tuple_filename_string_line_int_column_int_wasm(
arg: cstring): cstring {.importc.}
proc addCommandScript*(context: string; subContext: string; keys: string;
action: string; arg: string = "";
description: string = "") =
description: string = ""; source: tuple[filename: string,
line: int, column: int] = ("", 0, 0)) =
var argsJson = newJArray()
argsJson.add block:
when string is JsonNode:
Expand Down Expand Up @@ -1407,8 +1474,13 @@ proc addCommandScript*(context: string; subContext: string; keys: string;
description
else:
description.toJson()
argsJson.add block:
when tuple[filename: string, line: int, column: int] is JsonNode:
source
else:
source.toJson()
let argsJsonString = $argsJson
let res {.used.} = editor_addCommandScript_void_App_string_string_string_string_string_string_wasm(
let res {.used.} = editor_addCommandScript_void_App_string_string_string_string_string_string_tuple_filename_string_line_int_column_int_wasm(
argsJsonString.cstring)


Expand Down
15 changes: 12 additions & 3 deletions scripting/plugin_api_internal.nim
Original file line number Diff line number Diff line change
Expand Up @@ -799,7 +799,12 @@ proc editor_chooseTheme_void_App_impl*() =
discard
proc editor_createFile_void_App_string_impl*(path: string) =
discard
proc editor_browseKeybinds_void_App_impl*() =
proc editor_mountVfs_void_App_string_string_JsonNode_impl*(parentPath: string;
prefix: string; config: JsonNode) =
discard
proc editor_browseKeybinds_void_App_bool_float_float_float_impl*(
preview: bool = true; scaleX: float = 0.9; scaleY: float = 0.8;
previewScale: float = 0.4) =
discard
proc editor_chooseFile_void_App_bool_float_float_float_impl*(
preview: bool = true; scaleX: float = 0.8; scaleY: float = 0.8;
Expand Down Expand Up @@ -878,9 +883,13 @@ proc editor_setLeaders_void_App_seq_string_impl*(leaders: seq[string]) =
discard
proc editor_addLeader_void_App_string_impl*(leader: string) =
discard
proc editor_addCommandScript_void_App_string_string_string_string_string_string_impl*(
proc editor_registerPluginSourceCode_void_App_string_string_impl*(path: string;
content: string) =
discard
proc editor_addCommandScript_void_App_string_string_string_string_string_string_tuple_filename_string_line_int_column_int_impl*(
context: string; subContext: string; keys: string; action: string;
arg: string = ""; description: string = "") =
arg: string = ""; description: string = "";
source: tuple[filename: string, line: int, column: int] = ("", 0, 0)) =
discard
proc editor_removeCommand_void_App_string_string_impl*(context: string;
keys: string) =
Expand Down
Loading

0 comments on commit dc48bfa

Please sign in to comment.