Merge pull request #2210 from alixander/lsp-get-board-pos

fix d2lsp pr
This commit is contained in:
Alexander Wang 2024-11-12 22:56:27 -07:00 committed by GitHub
commit 10b0190f9b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 6 additions and 23 deletions

View file

@ -93,12 +93,9 @@ func getBoardMap(path string, fs map[string]string, boardPath []string) (*d2ir.M
return m, nil
}
func GetBoardAtPosition(path string, fs map[string]string, pos d2ast.Position) ([]string, error) {
if _, ok := fs[path]; !ok {
return nil, fmt.Errorf(`"%s" not found`, path)
}
r := strings.NewReader(fs[path])
ast, err := d2parser.Parse(path, r, nil)
func GetBoardAtPosition(text string, pos d2ast.Position) ([]string, error) {
r := strings.NewReader(text)
ast, err := d2parser.Parse("", r, nil)
if err != nil {
return nil, err
}

View file

@ -1,6 +1,7 @@
package d2lsp_test
import (
"slices"
"testing"
"oss.terrastruct.com/d2/d2ast"
@ -294,29 +295,14 @@ layers: {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := d2lsp.GetBoardAtPosition(tt.path, tt.fs, tt.position)
got, err := d2lsp.GetBoardAtPosition(tt.fs[tt.path], tt.position)
assert.Success(t, err)
if tt.want == nil {
assert.Equal(t, true, got == nil)
} else {
assert.Equal(t, len(tt.want), len(got))
assert.Equal(t, tt.want[0], got[0]) // board type
assert.Equal(t, tt.want[1], got[1]) // board id
assert.True(t, slices.Equal(tt.want, got))
}
})
}
// Error cases
t.Run("invalid file", func(t *testing.T) {
fs := map[string]string{
"index.d2": "x ->",
}
_, err := d2lsp.GetBoardAtPosition("index.d2", fs, d2ast.Position{Line: 0, Column: 0})
assert.Error(t, err)
})
t.Run("file not found", func(t *testing.T) {
_, err := d2lsp.GetBoardAtPosition("notfound.d2", nil, d2ast.Position{Line: 0, Column: 0})
assert.Error(t, err)
})
}