get board at position

This commit is contained in:
Alexander Wang 2024-11-13 12:17:49 -07:00
parent 2b8c047b5b
commit 784a961885
No known key found for this signature in database
GPG key ID: BE3937D0D52D8927

View file

@ -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))
@ -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)
}