-
Notifications
You must be signed in to change notification settings - Fork 6
/
reference.go
43 lines (36 loc) · 1.17 KB
/
reference.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package langserver
import (
"context"
"encoding/json"
"github.com/kitagry/regols/langserver/internal/lsp"
"github.com/sourcegraph/jsonrpc2"
)
func (h *handler) handleTextDocumentReferences(ctx context.Context, _ *jsonrpc2.Conn, req *jsonrpc2.Request) (result any, err error) {
if req.Params == nil {
return nil, &jsonrpc2.Error{Code: jsonrpc2.CodeInvalidParams}
}
var params lsp.TextDocumentPositionParams
if err := json.Unmarshal(*req.Params, ¶ms); err != nil {
return nil, err
}
return h.lookupReferences(ctx, params.TextDocument.URI, params.Position)
}
func (h *handler) lookupReferences(_ context.Context, uri lsp.DocumentURI, position lsp.Position) ([]lsp.Location, error) {
loc := h.toOPALocation(position, uri)
locations, err := h.project.LookupReferences(loc)
if err != nil {
h.logger.Printf("failed to get references: %v", err)
return nil, nil
}
result := make([]lsp.Location, 0, len(locations))
for _, r := range locations {
rawFile, err := h.project.GetRawText(r.File)
if err != nil {
continue
}
location := toLspLocation(r, rawFile)
location.URI = uriToDocumentURI(r.File)
result = append(result, location)
}
return result, nil
}