Skip to content

Commit

Permalink
get board at position
Browse files Browse the repository at this point in the history
  • Loading branch information
alixander committed Dec 12, 2024
1 parent c135b51 commit df70e0a
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions d2js/js.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ func main() {
js.Global().Set("d2GetObjOrder", js.FuncOf(jsGetObjOrder))
js.Global().Set("d2GetRefRanges", js.FuncOf(jsGetRefRanges))
js.Global().Set("d2Compile", js.FuncOf(jsCompile))
js.Global().Set("d2GetBoardAtPosition", js.FuncOf(jsGetBoardAtPosition))
js.Global().Set("d2Parse", js.FuncOf(jsParse))
js.Global().Set("d2Encode", js.FuncOf(jsEncode))
js.Global().Set("d2Decode", js.FuncOf(jsDecode))
Expand Down Expand Up @@ -302,3 +303,31 @@ func jsDecode(this js.Value, args []js.Value) interface{} {
func jsVersion(this js.Value, args []js.Value) interface{} {
return version.Version
}

type jsBoardAtPosition struct {
BoardPath []string `json:"boardPath"`
Error string `json:"error"`
}

func jsGetBoardAtPosition(this js.Value, args []js.Value) interface{} {
dsl := args[0].String()
line := args[1].Int()
column := args[2].Int()

boardPath, err := d2lsp.GetBoardAtPosition(dsl, d2ast.Position{
Line: line,
Column: column,
})

if err != nil {
ret := jsBoardAtPosition{Error: err.Error()}
str, _ := json.Marshal(ret)
return string(str)
}

resp := jsBoardAtPosition{
BoardPath: boardPath,
}
str, _ := json.Marshal(resp)
return string(str)
}

0 comments on commit df70e0a

Please sign in to comment.