Skip to content

Commit

Permalink
d2lsp: autocomplete
Browse files Browse the repository at this point in the history
  • Loading branch information
alixander committed Dec 27, 2024
1 parent d8b2c3b commit 0a87142
Show file tree
Hide file tree
Showing 5 changed files with 925 additions and 1 deletion.
1 change: 0 additions & 1 deletion d2ast/keywords.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ var ReservedKeywords map[string]struct{}
// Non Style/Holder keywords.
var SimpleReservedKeywords = map[string]struct{}{
"label": {},
"desc": {},
"shape": {},
"icon": {},
"constraint": {},
Expand Down
34 changes: 34 additions & 0 deletions d2js/d2wasm/functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,3 +159,37 @@ func Decode(args []js.Value) (interface{}, error) {
func GetVersion(args []js.Value) (interface{}, error) {
return version.Version, nil
}

func GetCompletions(args []js.Value) (interface{}, error) {
if len(args) < 3 {
return nil, &WASMError{Message: "missing required arguments", Code: 400}
}

text := args[0].String()
line := args[1].Int()
column := args[2].Int()

completions, err := d2lsp.GetCompletionItems(text, line, column)
if err != nil {
return nil, &WASMError{Message: err.Error(), Code: 500}
}

// Convert to map for JSON serialization
items := make([]map[string]interface{}, len(completions))
for i, completion := range completions {
items[i] = map[string]interface{}{
"label": completion.Label,
"kind": int(completion.Kind),
"detail": completion.Detail,
"insertText": completion.InsertText,
}
}

return CompletionResponse{
Items: items,
}, nil
}

type CompletionResponse struct {
Items []map[string]interface{} `json:"items"`
}
1 change: 1 addition & 0 deletions d2js/js.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
func main() {
api := d2wasm.NewD2API()

api.Register("getCompletions", d2wasm.GetCompletions)
api.Register("getParentID", d2wasm.GetParentID)
api.Register("getObjOrder", d2wasm.GetObjOrder)
api.Register("getRefRanges", d2wasm.GetRefRanges)
Expand Down
Loading

0 comments on commit 0a87142

Please sign in to comment.