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 19:35:32 +00:00
|
|
|
func GetRefs(path string, fs map[string]string, key string, boardPath []string) (refs []d2ir.Reference, _ error) {
|
2024-10-12 00:42:23 +00:00
|
|
|
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
|
|
|
|
|
}
|
2024-10-12 19:35:32 +00:00
|
|
|
if mk.Key == nil && len(mk.Edges) == 0 {
|
2024-09-19 14:32:48 +00:00
|
|
|
return nil, fmt.Errorf(`"%s" is invalid`, key)
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-12 19:35:32 +00:00
|
|
|
m, _, err := d2ir.Compile(ast, &d2ir.CompileOptions{
|
2024-09-19 14:32:48 +00:00
|
|
|
FS: mfs,
|
|
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-12 19:35:32 +00:00
|
|
|
m = m.FindBoardRoot(boardPath)
|
|
|
|
|
if m == nil {
|
2024-10-12 00:42:23 +00:00
|
|
|
return nil, fmt.Errorf(`board "%v" not found`, boardPath)
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-19 14:32:48 +00:00
|
|
|
var f *d2ir.Field
|
2024-10-12 19:35:32 +00:00
|
|
|
if mk.Key != nil {
|
|
|
|
|
for _, p := range mk.Key.Path {
|
|
|
|
|
f = m.GetField(p.Unbox().ScalarString())
|
|
|
|
|
if f == nil {
|
|
|
|
|
return nil, nil
|
|
|
|
|
}
|
|
|
|
|
m = f.Map()
|
2024-09-19 14:32:48 +00:00
|
|
|
}
|
|
|
|
|
}
|
2024-10-12 19:35:32 +00:00
|
|
|
|
|
|
|
|
if len(mk.Edges) > 0 {
|
|
|
|
|
eids := d2ir.NewEdgeIDs(mk)
|
|
|
|
|
var edges []*d2ir.Edge
|
|
|
|
|
for _, eid := range eids {
|
|
|
|
|
edges = append(edges, m.GetEdges(eid, nil, nil)...)
|
|
|
|
|
}
|
|
|
|
|
if len(edges) == 0 {
|
|
|
|
|
return nil, nil
|
|
|
|
|
}
|
|
|
|
|
for _, edge := range edges {
|
|
|
|
|
for _, ref := range edge.References {
|
|
|
|
|
refs = append(refs, ref)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return refs, nil
|
|
|
|
|
} else {
|
|
|
|
|
for _, ref := range f.References {
|
|
|
|
|
refs = append(refs, ref)
|
|
|
|
|
}
|
2024-09-19 14:32:48 +00:00
|
|
|
}
|
|
|
|
|
return refs, nil
|
|
|
|
|
}
|