diff --git a/config/harpoon.nim b/config/harpoon.nim index d223c85d..fc56e570 100644 --- a/config/harpoon.nim +++ b/config/harpoon.nim @@ -172,7 +172,10 @@ proc defaultEncode(item: HarpoonItem): string = return $item.toJson proc defaultDecode(value: string): HarpoonItem = - return value.parseJson.jsonTo(HarpoonItem) + try: + return value.parseJson.jsonTo(HarpoonItem) + except: + return HarpoonItem() proc defaultCreateListItem(config: HarpoonPartialConfigItem, name: Option[string]): HarpoonItem = let activeEditor = if getActiveEditor().isTextEditor(ed): @@ -226,7 +229,11 @@ proc encode(list: HarpoonList): Option[seq[string]] = for item in list.items: var encoded = "" if encodeCallback != "": - encodeCallback.invoke(encoded, item) + try: + encodeCallback.invoke(encoded, item) + except: + infof"Failed to encode item {item}" + continue else: encoded = defaultEncode(item) @@ -253,7 +260,7 @@ proc getList(self: Harpoon, name: Option[string] = string.none): var HarpoonList else: key = "" - let lists = self.lists.mgetOrPut(key).addr + let lists = self.lists.mgetOrPut(key, initTable[string, HarpoonList]()).addr if lists[].contains(name): return self.lists[key][name] diff --git a/config/wasm/harpoon.wasm b/config/wasm/harpoon.wasm index b0dbd7f1..dacdc621 100644 Binary files a/config/wasm/harpoon.wasm and b/config/wasm/harpoon.wasm differ diff --git a/config/wasm/keybindings_plugin.wasm b/config/wasm/keybindings_plugin.wasm index ca914319..e21380d0 100644 Binary files a/config/wasm/keybindings_plugin.wasm and b/config/wasm/keybindings_plugin.wasm differ diff --git a/config/wasm/vscode_config_plugin.wasm b/config/wasm/vscode_config_plugin.wasm index 9cf92cc3..0e629537 100644 Binary files a/config/wasm/vscode_config_plugin.wasm and b/config/wasm/vscode_config_plugin.wasm differ diff --git a/scripting/absytree_runtime.nim b/scripting/absytree_runtime.nim index e48bf419..65a1a9b8 100644 --- a/scripting/absytree_runtime.nim +++ b/scripting/absytree_runtime.nim @@ -20,7 +20,7 @@ type AnyDocumentEditor = TextDocumentEditor | ModelDocumentEditor var voidCallbacks = initTable[int, proc(args: JsonNode): void]() var boolCallbacks = initTable[int, proc(args: JsonNode): bool]() var anyCallbacks = initTable[int, proc(args: JsonNode): JsonNode]() -var onEditorModeChanged*: Event[tuple[editor: EditorId, oldMode: string, newMode: string]] +var onEditorModeChanged* = initEvent[tuple[editor: EditorId, oldMode: string, newMode: string]]() var callbackId = 0 when defined(wasm): diff --git a/scripting/absytree_runtime_impl.nim b/scripting/absytree_runtime_impl.nim index abebddca..6dbfe052 100644 --- a/scripting/absytree_runtime_impl.nim +++ b/scripting/absytree_runtime_impl.nim @@ -6,8 +6,12 @@ proc handleAnyCallback*(id: int, args: JsonNode): JsonNode = handleAnyCallbackIm proc handleScriptAction*(name: string, args: JsonNode): JsonNode = handleScriptActionImpl(name, args) proc handleEditorModeChanged*(editor: EditorId, oldMode: string, newMode: string) = - # infof"handleEditorModeChanged {editor} {oldMode} {newMode}" - onEditorModeChanged.invoke (editor, oldMode, newMode) + try: + # todo: in nimscript this crashes? + when not defined(nimscript): + onEditorModeChanged.invoke (editor, oldMode, newMode) + except: + info &"handleEditorModeChanged failed: {getCurrentExceptionMsg()}\n{getCurrentException().getStackTrace()}" when defined(wasm): proc postInitializeWasm(): bool {.wasmexport.} = diff --git a/src/app.nim b/src/app.nim index 438ef3e3..76935632 100644 --- a/src/app.nim +++ b/src/app.nim @@ -739,10 +739,15 @@ proc initScripting(self: App, options: AppOptions) {.async.} = if self.homeDir != "": let scriptPath = self.homeDir / ".absytree/custom.nims" - if createScriptContext(scriptPath, searchPaths).await.getSome(scriptContext): - self.nimsScriptContext = scriptContext + if fileExists(scriptPath): + log lvlInfo, &"Found '{scriptPath}'" + if createScriptContext(scriptPath, searchPaths).await.getSome(scriptContext): + self.nimsScriptContext = scriptContext + else: + log lvlError, "Failed to create nim script context" + else: - log lvlError, "Failed to create nim script context" + log lvlInfo, &"No custom.nims found in home ~/.absytree" withScriptContext self, self.nimsScriptContext: log(lvlInfo, fmt"init nim script config") diff --git a/src/misc/event.nim b/src/misc/event.nim index 33bdfd9b..8b5dca04 100644 --- a/src/misc/event.nim +++ b/src/misc/event.nim @@ -4,6 +4,9 @@ import id, util type Event*[T] = object handlers: seq[tuple[id: Id, callback: (T) -> void]] +proc initEvent*[T](): Event[T] = + result = Event[T](handlers: @[]) + proc subscribe*[T: void](event: var Event[T], callback: () -> void): Id = assert callback != nil result = newId()