MCPcopy
hub / github.com/MadAppGang/dingo / handleReferences

Method handleReferences

pkg/lsp/navigation.go:14–78  ·  view source on GitHub ↗

handleReferences handles textDocument/references requests. It translates Dingo positions to Go, calls gopls, then translates results back. All references are returned including those in stdlib/vendor .go files.

(
	ctx context.Context,
	reply jsonrpc2.Replier,
	req jsonrpc2.Request,
)

Source from the content-addressed store, hash-verified

12// It translates Dingo positions to Go, calls gopls, then translates results back.
13// All references are returned including those in stdlib/vendor .go files.
14func (s *Server) handleReferences(
15 ctx context.Context,
16 reply jsonrpc2.Replier,
17 req jsonrpc2.Request,
18) error {
19 var params protocol.ReferenceParams
20 if err := json.Unmarshal(req.Params(), &params); err != nil {
21 return reply(ctx, nil, err)
22 }
23
24 s.config.Logger.Debugf("[References] Request for URI=%s, Line=%d, Char=%d",
25 params.TextDocument.URI.Filename(), params.Position.Line, params.Position.Character)
26
27 // If not a .dingo file, forward directly to gopls
28 if !isDingoFile(params.TextDocument.URI) {
29 result, err := s.gopls.References(ctx, params)
30 return reply(ctx, result, err)
31 }
32
33 // Translate Dingo position -> Go position
34 goURI, goPos, err := s.translator.TranslatePosition(params.TextDocument.URI, params.Position, DingoToGo)
35 if err != nil {
36 s.config.Logger.Warnf("[References] Position translation failed: %v", err)
37 // Graceful degradation: try with original position
38 result, err := s.gopls.References(ctx, params)
39 return reply(ctx, result, err)
40 }
41
42 s.config.Logger.Debugf("[References] Translated to Go: URI=%s, Line=%d, Char=%d",
43 goURI.Filename(), goPos.Line, goPos.Character)
44
45 // Update params with translated position
46 params.TextDocument.URI = goURI
47 params.Position = goPos
48
49 // Forward to gopls
50 locations, err := s.gopls.References(ctx, params)
51 if err != nil {
52 s.config.Logger.Warnf("[References] gopls error: %v", err)
53 return reply(ctx, nil, err)
54 }
55
56 s.config.Logger.Debugf("[References] gopls returned %d locations", len(locations))
57
58 // Translate all locations Go -> Dingo
59 // Decision: Include ALL references (stdlib, vendor) unchanged for completeness
60 translated := make([]protocol.Location, 0, len(locations))
61 for i, loc := range locations {
62 translatedLoc, err := s.translator.TranslateLocation(loc, GoToDingo)
63 if err != nil {
64 // Keep unchanged for locations that can't be translated (e.g., stdlib, vendor .go files)
65 s.config.Logger.Debugf("[References] [%d] Translation failed, keeping original: URI=%s, Line=%d",
66 i, loc.URI.Filename(), loc.Range.Start.Line)
67 translated = append(translated, loc)
68 } else {
69 s.config.Logger.Debugf("[References] [%d] Translated: URI=%s, Line=%d -> URI=%s, Line=%d",
70 i, loc.URI.Filename(), loc.Range.Start.Line,
71 translatedLoc.URI.Filename(), translatedLoc.Range.Start.Line)

Callers 1

handleRequestMethod · 0.95

Calls 6

isDingoFileFunction · 0.85
ReferencesMethod · 0.80
TranslatePositionMethod · 0.80
TranslateLocationMethod · 0.80
DebugfMethod · 0.65
WarnfMethod · 0.65

Tested by

no test coverage detected