Skip to content

Commit

Permalink
Cleanup some plugin apis
Browse files Browse the repository at this point in the history
  • Loading branch information
Nimaoth committed Oct 19, 2024
1 parent 3159cdd commit 893c210
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 167 deletions.
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.
85 changes: 0 additions & 85 deletions scripting/editor_api_wasm.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1129,91 +1129,6 @@ proc scriptInsertTextInto*(editorId: EditorId; text: string) {.gcsafe,
argsJsonString.cstring)


proc editor_scriptTextEditorSelection_Selection_EditorId_wasm(arg: cstring): cstring {.
importc.}
proc scriptTextEditorSelection*(editorId: EditorId): Selection {.gcsafe,
raises: [].} =
var argsJson = newJArray()
argsJson.add editorId.toJson()
let argsJsonString = $argsJson
let res {.used.} = editor_scriptTextEditorSelection_Selection_EditorId_wasm(
argsJsonString.cstring)
try:
result = parseJson($res).jsonTo(typeof(result))
except:
raiseAssert(getCurrentExceptionMsg())


proc editor_scriptSetTextEditorSelection_void_EditorId_Selection_wasm(
arg: cstring): cstring {.importc.}
proc scriptSetTextEditorSelection*(editorId: EditorId; selection: Selection) {.
gcsafe, raises: [].} =
var argsJson = newJArray()
argsJson.add editorId.toJson()
argsJson.add selection.toJson()
let argsJsonString = $argsJson
let res {.used.} = editor_scriptSetTextEditorSelection_void_EditorId_Selection_wasm(
argsJsonString.cstring)


proc editor_scriptTextEditorSelections_seq_Selection_EditorId_wasm(arg: cstring): cstring {.
importc.}
proc scriptTextEditorSelections*(editorId: EditorId): seq[Selection] {.gcsafe,
raises: [].} =
var argsJson = newJArray()
argsJson.add editorId.toJson()
let argsJsonString = $argsJson
let res {.used.} = editor_scriptTextEditorSelections_seq_Selection_EditorId_wasm(
argsJsonString.cstring)
try:
result = parseJson($res).jsonTo(typeof(result))
except:
raiseAssert(getCurrentExceptionMsg())


proc editor_scriptSetTextEditorSelections_void_EditorId_seq_Selection_wasm(
arg: cstring): cstring {.importc.}
proc scriptSetTextEditorSelections*(editorId: EditorId;
selections: seq[Selection]) {.gcsafe,
raises: [].} =
var argsJson = newJArray()
argsJson.add editorId.toJson()
argsJson.add selections.toJson()
let argsJsonString = $argsJson
let res {.used.} = editor_scriptSetTextEditorSelections_void_EditorId_seq_Selection_wasm(
argsJsonString.cstring)


proc editor_scriptGetTextEditorLine_string_EditorId_int_wasm(arg: cstring): cstring {.
importc.}
proc scriptGetTextEditorLine*(editorId: EditorId; line: int): string {.gcsafe,
raises: [].} =
var argsJson = newJArray()
argsJson.add editorId.toJson()
argsJson.add line.toJson()
let argsJsonString = $argsJson
let res {.used.} = editor_scriptGetTextEditorLine_string_EditorId_int_wasm(
argsJsonString.cstring)
try:
result = parseJson($res).jsonTo(typeof(result))
except:
raiseAssert(getCurrentExceptionMsg())


proc editor_scriptGetTextEditorLineCount_int_EditorId_wasm(arg: cstring): cstring {.
importc.}
proc scriptGetTextEditorLineCount*(editorId: EditorId): int {.gcsafe, raises: [].} =
var argsJson = newJArray()
argsJson.add editorId.toJson()
let argsJsonString = $argsJson
let res {.used.} = editor_scriptGetTextEditorLineCount_int_EditorId_wasm(
argsJsonString.cstring)
try:
result = parseJson($res).jsonTo(typeof(result))
except:
raiseAssert(getCurrentExceptionMsg())


proc editor_setSessionDataJson_void_App_string_JsonNode_bool_wasm(arg: cstring): cstring {.
importc.}
proc setSessionDataJson*(path: string; value: JsonNode; override: bool = true) {.
Expand Down
41 changes: 41 additions & 0 deletions scripting/editor_text_api_wasm.nim
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,21 @@ proc getText*(self: TextDocumentEditor; selection: Selection;
raiseAssert(getCurrentExceptionMsg())


proc editor_text_getLine_string_TextDocumentEditor_int_wasm(arg: cstring): cstring {.
importc.}
proc getLine*(self: TextDocumentEditor; line: int): string {.gcsafe, raises: [].} =
var argsJson = newJArray()
argsJson.add self.toJson()
argsJson.add line.toJson()
let argsJsonString = $argsJson
let res {.used.} = editor_text_getLine_string_TextDocumentEditor_int_wasm(
argsJsonString.cstring)
try:
result = parseJson($res).jsonTo(typeof(result))
except:
raiseAssert(getCurrentExceptionMsg())


proc editor_text_insert_seq_Selection_TextDocumentEditor_seq_Selection_string_bool_bool_wasm(
arg: cstring): cstring {.importc.}
proc insert*(self: TextDocumentEditor; selections: seq[Selection]; text: string;
Expand Down Expand Up @@ -2155,6 +2170,20 @@ proc getSelection*(self: TextDocumentEditor): Selection {.gcsafe, raises: [].} =
raiseAssert(getCurrentExceptionMsg())


proc editor_text_getSelections_Selections_TextDocumentEditor_wasm(arg: cstring): cstring {.
importc.}
proc getSelections*(self: TextDocumentEditor): Selections {.gcsafe, raises: [].} =
var argsJson = newJArray()
argsJson.add self.toJson()
let argsJsonString = $argsJson
let res {.used.} = editor_text_getSelections_Selections_TextDocumentEditor_wasm(
argsJsonString.cstring)
try:
result = parseJson($res).jsonTo(typeof(result))
except:
raiseAssert(getCurrentExceptionMsg())


proc editor_text_setSelection_void_TextDocumentEditor_Selection_wasm(
arg: cstring): cstring {.importc.}
proc setSelection*(self: TextDocumentEditor; selection: Selection) {.gcsafe,
Expand All @@ -2167,6 +2196,18 @@ proc setSelection*(self: TextDocumentEditor; selection: Selection) {.gcsafe,
argsJsonString.cstring)


proc editor_text_setSelections_void_TextDocumentEditor_Selections_wasm(
arg: cstring): cstring {.importc.}
proc setSelections*(self: TextDocumentEditor; selections: Selections) {.gcsafe,
raises: [].} =
var argsJson = newJArray()
argsJson.add self.toJson()
argsJson.add selections.toJson()
let argsJsonString = $argsJson
let res {.used.} = editor_text_setSelections_void_TextDocumentEditor_Selections_wasm(
argsJsonString.cstring)


proc editor_text_setTargetSelection_void_TextDocumentEditor_Selection_wasm(
arg: cstring): cstring {.importc.}
proc setTargetSelection*(self: TextDocumentEditor; selection: Selection) {.
Expand Down
21 changes: 6 additions & 15 deletions scripting/plugin_runtime.nim
Original file line number Diff line number Diff line change
Expand Up @@ -115,30 +115,21 @@ proc runAction*(id: EditorId, action: string, arg: string = "") =
proc runAction*(editor: TextDocumentEditor, action: string, arg: string = "") =
scriptRunActionFor(editor.id, action, arg)

proc insertText*(editor: AnyDocumentEditor, text: string) =
scriptInsertTextInto(editor.id, text)

proc selection*(editor: TextDocumentEditor): Selection =
return editor.getSelection()

proc selections*(editor: TextDocumentEditor): seq[Selection] =
return editor.getSelections()

proc `selection=`*(editor: TextDocumentEditor, selection: Selection) =
editor.setSelection(selection)

proc `selections=`*(editor: TextDocumentEditor, selections: seq[Selection]) =
editor.setSelections(selections)

proc `targetSelection=`*(editor: TextDocumentEditor, selection: Selection) =
editor.setTargetSelection(selection)

proc selections*(editor: TextDocumentEditor): seq[Selection] =
return scriptTextEditorSelections(editor.id)

proc `selections=`*(editor: TextDocumentEditor, selection: seq[Selection]) =
scriptSetTextEditorSelections(editor.id, selection)

proc getLine*(editor: TextDocumentEditor, line: int): string =
return scriptGetTextEditorLine(editor.id, line)

proc getLineCount*(editor: TextDocumentEditor): int =
return scriptGetTextEditorLineCount(editor.id)

proc getOption*[T](path: string, default: T = T.default): T =
try:
let json: JsonNode = plugin_api.getOptionJson(path, newJNull())
Expand Down
67 changes: 0 additions & 67 deletions src/app.nim
Original file line number Diff line number Diff line change
Expand Up @@ -197,8 +197,6 @@ type

sessionFile*: string

gitIgnore: Globs

currentLocationListIndex: int
finderItems: seq[FinderItem]
previewer: Option[DisposableRef[Previewer]]
Expand Down Expand Up @@ -1054,8 +1052,6 @@ proc newApp*(backend: api.Backend, platform: Platform, fs: Filesystem, services:

self.currentView = 0

self.gitIgnore = parseGlobs(self.fs.loadApplicationFile(".gitignore"))

assignEventHandler(self.eventHandler, self.getEventHandlerConfig("editor")):
onAction:
if self.handleAction(action, arg, record=true).isSome:
Expand Down Expand Up @@ -3323,69 +3319,6 @@ proc scriptInsertTextInto*(editorId: EditorId, text: string) {.expose("editor").
if gEditor.getEditorForId(editorId).getSome(editor):
discard editor.eventHandler.handleInput(text)

proc scriptTextEditorSelection*(editorId: EditorId): Selection {.expose("editor").} =
{.gcsafe.}:
if gEditor.isNil:
return ((0, 0), (0, 0))
defer:
gEditor.platform.requestRender()
if gEditor.getEditorForId(editorId).getSome(editor):
if editor of TextDocumentEditor:
let editor = TextDocumentEditor(editor)
return editor.selection
return ((0, 0), (0, 0))

proc scriptSetTextEditorSelection*(editorId: EditorId, selection: Selection) {.expose("editor").} =
{.gcsafe.}:
if gEditor.isNil:
return
defer:
gEditor.platform.requestRender()
if gEditor.getEditorForId(editorId).getSome(editor):
if editor of TextDocumentEditor:
editor.TextDocumentEditor.selection = selection

proc scriptTextEditorSelections*(editorId: EditorId): seq[Selection] {.expose("editor").} =
{.gcsafe.}:
if gEditor.isNil:
return @[((0, 0), (0, 0))]
if gEditor.getEditorForId(editorId).getSome(editor):
if editor of TextDocumentEditor:
let editor = TextDocumentEditor(editor)
return editor.selections
return @[((0, 0), (0, 0))]

proc scriptSetTextEditorSelections*(editorId: EditorId, selections: seq[Selection]) {.expose("editor").} =
{.gcsafe.}:
if gEditor.isNil:
return
defer:
gEditor.platform.requestRender()
if gEditor.getEditorForId(editorId).getSome(editor):
if editor of TextDocumentEditor:
editor.TextDocumentEditor.selections = selections

proc scriptGetTextEditorLine*(editorId: EditorId, line: int): string {.expose("editor").} =
{.gcsafe.}:
if gEditor.isNil:
return ""
if gEditor.getEditorForId(editorId).getSome(editor):
if editor of TextDocumentEditor:
let editor = TextDocumentEditor(editor)
if line >= 0 and line < editor.document.numLines:
return $editor.document.getLine(line)
return ""

proc scriptGetTextEditorLineCount*(editorId: EditorId): int {.expose("editor").} =
{.gcsafe.}:
if gEditor.isNil:
return 0
if gEditor.getEditorForId(editorId).getSome(editor):
if editor of TextDocumentEditor:
let editor = TextDocumentEditor(editor)
return editor.document.numLines
return 0

proc setSessionDataJson*(self: App, path: string, value: JsonNode, override: bool = true) {.expose("editor").} =
if self.isNil or path.len == 0:
return
Expand Down
9 changes: 9 additions & 0 deletions src/text/text_editor.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1086,6 +1086,9 @@ proc getText*(self: TextDocumentEditor, selection: Selection, inclusiveEnd: bool
string {.expose("editor.text").} =
return self.document.contentString(selection, inclusiveEnd)

proc getLine*(self: TextDocumentEditor, line: int): string {.expose("editor.text").} =
return $self.document.getLine(line)

proc insert*(self: TextDocumentEditor, selections: seq[Selection], text: string, notify: bool = true,
record: bool = true): seq[Selection] {.expose("editor.text").} =
return self.document.edit(selections, self.selections, [text], notify, record)
Expand Down Expand Up @@ -3183,9 +3186,15 @@ proc assignKeys*(self: TextDocumentEditor, cursors: openArray[Cursor]): seq[stri
proc getSelection*(self: TextDocumentEditor): Selection {.expose("editor.text").} =
self.selection

proc getSelections*(self: TextDocumentEditor): Selections {.expose("editor.text").} =
self.selections

proc setSelection*(self: TextDocumentEditor, selection: Selection) {.expose("editor.text").} =
self.selection = selection

proc setSelections*(self: TextDocumentEditor, selections: Selections) {.expose("editor.text").} =
self.selections = selections

proc setTargetSelection*(self: TextDocumentEditor, selection: Selection) {.expose("editor.text").} =
self.targetSelection = selection

Expand Down

0 comments on commit 893c210

Please sign in to comment.