clerk/internal/lsp/textdocument_definition_test.go (view raw)
| 1 | package lsp |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "path/filepath" |
| 6 | "strings" |
| 7 | "testing" |
| 8 | |
| 9 | "go.lsp.dev/protocol" |
| 10 | "go.lsp.dev/uri" |
| 11 | |
| 12 | "olexsmir.xyz/clerk/internal/lsp/lsputil" |
| 13 | "olexsmir.xyz/clerk/internal/testutil/golden" |
| 14 | "olexsmir.xyz/clerk/journal" |
| 15 | ) |
| 16 | |
| 17 | func TestServer_Definition_DocumentNotFound(t *testing.T) { |
| 18 | srv := newServer(t) |
| 19 | res, err := srv.server.Definition(t.Context(), &protocol.DefinitionParams{ |
| 20 | TextDocument: protocol.TextDocumentIdentifier{URI: uri.URI("file:///nonexistent.journal")}, |
| 21 | Position: protocol.Position{Line: 0, Character: 0}, |
| 22 | }) |
| 23 | if err != nil { |
| 24 | t.Fatal(err) |
| 25 | } |
| 26 | if res != nil { |
| 27 | t.Errorf("got %v, want nil", res) |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | func TestGolden_Definition(t *testing.T) { |
| 32 | for _, tt := range []string{"definition-journal", "definition-include"} { |
| 33 | t.Run(tt, func(t *testing.T) { |
| 34 | ar := golden.Read(t, tt) |
| 35 | h := newTxtarHarness(t, ar) |
| 36 | |
| 37 | var b strings.Builder |
| 38 | for i := range h.cursors { |
| 39 | pos := h.textDocumentPosition(i).Position |
| 40 | res, err := h.srv.Definition(t.Context(), &protocol.DefinitionParams{ |
| 41 | TextDocumentPositionParams: h.textDocumentPosition(i), |
| 42 | }) |
| 43 | if err != nil { |
| 44 | t.Fatal(err) |
| 45 | } |
| 46 | locs, _ := res.(protocol.LocationSlice) |
| 47 | if len(locs) == 0 { |
| 48 | fmt.Fprintf(&b, "%d:%d <none>\n", pos.Line, pos.Character) |
| 49 | continue |
| 50 | } |
| 51 | r := locs[0].Range |
| 52 | fmt.Fprintf(&b, "%d:%d %s %d:%d-%d:%d\n", pos.Line, pos.Character, |
| 53 | filepath.Base(locs[0].URI.Path()), |
| 54 | r.Start.Line, r.Start.Character, r.End.Line, r.End.Character) |
| 55 | } |
| 56 | golden.Assert(t, ar, b.String()) |
| 57 | }) |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | func BenchmarkDefinition(b *testing.B) { |
| 62 | path := "../../journal/testdata/journals/actual-1ktxns-100accts.journal" |
| 63 | abs, err := filepath.Abs(path) |
| 64 | if err != nil { |
| 65 | b.Fatal(err) |
| 66 | } |
| 67 | rj, err := journal.NewLoader().Resolve(abs) |
| 68 | if err != nil { |
| 69 | b.Fatal(err) |
| 70 | } |
| 71 | content := string(rj.Occurrences[0].Src) |
| 72 | |
| 73 | srv := newServer(b) |
| 74 | u := uri.File(abs) |
| 75 | srv.server.openDoc(u, content, 1, "journal") |
| 76 | srv.server.analysisFor(u) // warm the per-doc cache |
| 77 | |
| 78 | for tname, tt := range map[string]int{ |
| 79 | "1k txns, account": strings.Index(content, "\n 1:2:3 ") + len("\n ") + 2, |
| 80 | "1k txns, payee": strings.Index(content, "transaction 1") + len("transaction "), |
| 81 | "1k txns, commodity": strings.Index(content, "2 B @@") + len("2 B"), |
| 82 | } { |
| 83 | b.Run(tname, func(b *testing.B) { |
| 84 | line, col := lsputil.LineCol(content, tt) |
| 85 | params := &protocol.DefinitionParams{ |
| 86 | TextDocument: protocol.TextDocumentIdentifier{URI: u}, |
| 87 | Position: protocol.Position{Line: uint32(line), Character: uint32(col)}, |
| 88 | } |
| 89 | // warm up: first request resolves the symbol lazily; assert it found one |
| 90 | res, err := srv.server.Definition(b.Context(), params) |
| 91 | if err != nil { |
| 92 | b.Fatal(err) |
| 93 | } |
| 94 | locs, ok := res.(protocol.LocationSlice) |
| 95 | if !ok || len(locs) == 0 { |
| 96 | b.Fatalf("%s: no definition", tname) |
| 97 | } |
| 98 | b.ReportAllocs() |
| 99 | b.ResetTimer() |
| 100 | for b.Loop() { |
| 101 | if _, err := srv.server.Definition(b.Context(), params); err != nil { |
| 102 | b.Fatal(err) |
| 103 | } |
| 104 | } |
| 105 | }) |
| 106 | } |
| 107 | } |