Skip to content

Commit

Permalink
fixup
Browse files Browse the repository at this point in the history
  • Loading branch information
alixander committed Nov 13, 2024
1 parent 269f4f5 commit 8868d3a
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 23 deletions.
9 changes: 3 additions & 6 deletions d2lsp/d2lsp.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
20 changes: 3 additions & 17 deletions d2lsp/d2lsp_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package d2lsp_test

import (
"slices"
"testing"

"oss.terrastruct.com/d2/d2ast"
Expand Down Expand Up @@ -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)
})
}

0 comments on commit 8868d3a

Please sign in to comment.