clerk/internal/lsp/textdocument_definition_test.go (view raw)
| 1 | package lsp |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "os" |
| 6 | "path/filepath" |
| 7 | "strings" |
| 8 | "testing" |
| 9 | |
| 10 | "go.lsp.dev/protocol" |
| 11 | "go.lsp.dev/uri" |
| 12 | |
| 13 | "olexsmir.xyz/clerk/internal/lsp/lsputil" |
| 14 | "olexsmir.xyz/clerk/internal/testutil/golden" |
| 15 | ) |
| 16 | |
| 17 | func TestDefinition_DocumentNotFound(t *testing.T) { |
| 18 | srv := NewServer("test") |
| 19 | res, err := srv.server.Definition(t.Context(), &protocol.DefinitionParams{ |
| 20 | TextDocumentPositionParams: protocol.TextDocumentPositionParams{ |
| 21 | TextDocument: protocol.TextDocumentIdentifier{URI: uri.URI("file:///nonexistent.journal")}, |
| 22 | Position: protocol.Position{Line: 0, Character: 0}, |
| 23 | }, |
| 24 | }) |
| 25 | if err != nil { |
| 26 | t.Fatal(err) |
| 27 | } |
| 28 | if res != nil { |
| 29 | t.Errorf("got %v, want nil", res) |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | // Golden |
| 34 | |
| 35 | func TestDefinitionTxtar(t *testing.T) { |
| 36 | tests := []string{ |
| 37 | "definition-journal", |
| 38 | "definition-include", |
| 39 | } |
| 40 | for _, tt := range tests { |
| 41 | ar := golden.Read(t, tt) |
| 42 | t.Run(tt, func(t *testing.T) { |
| 43 | dir := t.TempDir() |
| 44 | for _, f := range ar.Files { |
| 45 | if f.Name != "expect" { |
| 46 | if err := os.WriteFile(filepath.Join(dir, f.Name), f.Data, 0o644); err != nil { |
| 47 | t.Fatal(err) |
| 48 | } |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | content, cursors := stripCursors(string(ar.Get("in.journal"))) |
| 53 | u := uri.File(filepath.Join(dir, "in.journal")) |
| 54 | |
| 55 | srv := NewServer("test") |
| 56 | srv.server.openDoc(u, content, 1, "journal") |
| 57 | |
| 58 | var b strings.Builder |
| 59 | for _, c := range cursors { |
| 60 | pos := lsputil.Position(content, c) |
| 61 | res, err := srv.server.Definition(t.Context(), &protocol.DefinitionParams{ |
| 62 | TextDocumentPositionParams: protocol.TextDocumentPositionParams{ |
| 63 | TextDocument: protocol.TextDocumentIdentifier{URI: u}, |
| 64 | Position: pos, |
| 65 | }, |
| 66 | }) |
| 67 | if err != nil { |
| 68 | t.Fatal(err) |
| 69 | } |
| 70 | locs, _ := res.(protocol.LocationSlice) |
| 71 | if len(locs) == 0 { |
| 72 | fmt.Fprintf(&b, "%d:%d <none>\n", pos.Line, pos.Character) |
| 73 | continue |
| 74 | } |
| 75 | r := locs[0].Range |
| 76 | fmt.Fprintf(&b, "%d:%d %s %d:%d-%d:%d\n", pos.Line, pos.Character, |
| 77 | filepath.Base(locs[0].URI.Path()), |
| 78 | r.Start.Line, r.Start.Character, r.End.Line, r.End.Character) |
| 79 | } |
| 80 | golden.Assert(t, ar, b.String()) |
| 81 | }) |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | func stripCursors(content string) (string, []int) { |
| 86 | var cursors []int |
| 87 | for { |
| 88 | i := strings.Index(content, "^") |
| 89 | if i < 0 { |
| 90 | return content, cursors |
| 91 | } |
| 92 | cursors = append(cursors, i) |
| 93 | content = content[:i] + content[i+1:] |
| 94 | } |
| 95 | } |