2024-09-19 14:32:48 +00:00
|
|
|
// d2lsp contains functions useful for IDE clients
|
|
|
|
|
package d2lsp
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
|
|
"oss.terrastruct.com/d2/d2ir"
|
|
|
|
|
"oss.terrastruct.com/d2/d2parser"
|
|
|
|
|
"oss.terrastruct.com/d2/lib/memfs"
|
|
|
|
|
)
|
|
|
|
|
|
2024-10-12 00:42:23 +00:00
|
|
|
func GetFieldRefs(path string, fs map[string]string, key string, boardPath []string) (refs []d2ir.Reference, _ error) {
|
|
|
|
|
if _, ok := fs[path]; !ok {
|
|
|
|
|
return nil, fmt.Errorf(`"%s" not found`, path)
|
2024-09-19 14:32:48 +00:00
|
|
|
}
|
2024-10-12 00:42:23 +00:00
|
|
|
r := strings.NewReader(fs[path])
|
2024-09-19 14:32:48 +00:00
|
|
|
ast, err := d2parser.Parse(path, r, nil)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
mfs, err := memfs.New(fs)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
mk, err := d2parser.ParseMapKey(key)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
if mk.Key == nil {
|
|
|
|
|
return nil, fmt.Errorf(`"%s" is invalid`, key)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ir, _, err := d2ir.Compile(ast, &d2ir.CompileOptions{
|
|
|
|
|
FS: mfs,
|
|
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-12 00:42:23 +00:00
|
|
|
ir = ir.FindBoardRoot(boardPath)
|
|
|
|
|
if ir == nil {
|
|
|
|
|
return nil, fmt.Errorf(`board "%v" not found`, boardPath)
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-19 14:32:48 +00:00
|
|
|
var f *d2ir.Field
|
|
|
|
|
curr := ir
|
|
|
|
|
for _, p := range mk.Key.Path {
|
|
|
|
|
f = curr.GetField(p.Unbox().ScalarString())
|
|
|
|
|
if f == nil {
|
2024-10-11 20:09:51 +00:00
|
|
|
return nil, nil
|
2024-09-19 14:32:48 +00:00
|
|
|
}
|
|
|
|
|
curr = f.Map()
|
|
|
|
|
}
|
|
|
|
|
for _, ref := range f.References {
|
|
|
|
|
refs = append(refs, ref)
|
|
|
|
|
}
|
|
|
|
|
return refs, nil
|
|
|
|
|
}
|