Skip to content

Commit

Permalink
Fixed some crashes and small issues
Browse files Browse the repository at this point in the history
  • Loading branch information
Nimaoth committed Jul 6, 2024
1 parent ef98b7d commit 1789f90
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 9 deletions.
13 changes: 10 additions & 3 deletions config/harpoon.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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)

Expand All @@ -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]
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.
2 changes: 1 addition & 1 deletion scripting/absytree_runtime.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
8 changes: 6 additions & 2 deletions scripting/absytree_runtime_impl.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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.} =
Expand Down
11 changes: 8 additions & 3 deletions src/app.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
3 changes: 3 additions & 0 deletions src/misc/event.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 1789f90

Please sign in to comment.