Skip to content

Commit 0ddf32a

Browse files
committed
update:support find cross package overload decl reference
1 parent 077305d commit 0ddf32a

File tree

2 files changed

+164
-15
lines changed

2 files changed

+164
-15
lines changed

gopls/internal/lsp/source/xrefs/xrefs_gox.go

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -48,26 +48,38 @@ func gopIndex(
4848
obj = typeparams.OriginMethod(fn)
4949
}
5050

51-
objects := getObjects(obj.Pkg())
52-
gobObj, ok := objects[obj]
53-
if !ok {
54-
path, err := objectpathFor(obj)
55-
if err != nil {
56-
// Capitalized but not exported
57-
// (e.g. local const/var/type).
58-
return true
51+
reportRef := func(obj types.Object) error {
52+
objects := getObjects(obj.Pkg())
53+
gobObj, ok := objects[obj]
54+
if !ok {
55+
path, err := objectpathFor(obj)
56+
if err != nil {
57+
// Capitalized but not exported
58+
// (e.g. local const/var/type).
59+
return err
60+
}
61+
gobObj = &gobObject{Path: path}
62+
objects[obj] = gobObj
5963
}
60-
gobObj = &gobObject{Path: path}
61-
objects[obj] = gobObj
64+
65+
gobObj.GopRefs = append(gobObj.GopRefs, gobRef{
66+
FileIndex: fileIndex,
67+
Range: nodeRange(n),
68+
})
69+
return nil
6270
}
6371

64-
gobObj.GopRefs = append(gobObj.GopRefs, gobRef{
65-
FileIndex: fileIndex,
66-
Range: nodeRange(n),
67-
})
72+
// goxls:overload use,refer its overload decl & overload members
73+
if err := reportRef(obj); err != nil {
74+
return true
75+
}
76+
if odObj, _ := info.OverloadOf(n); odObj != nil {
77+
if err := reportRef(odObj); err != nil {
78+
return true
79+
}
80+
}
6881
}
6982
//}
70-
7183
case *ast.ImportSpec:
7284
// Report a reference from each import path
7385
// string to the imported package.

gopls/internal/regtest/misc/references_gox_test.go

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,143 @@ func main() {
196196
runFindRefTest(t, files, testCases)
197197
}
198198

199+
func TestRefOverloadDeclCrossPackage(t *testing.T) {
200+
const files = `
201+
-- go.mod --
202+
module mod.com
203+
204+
go 1.19
205+
-- lib/lib.gop --
206+
package lib
207+
208+
func Add = (
209+
func(a, b int) int {
210+
return a + b
211+
}
212+
func(a, b string) string {
213+
return a + b
214+
}
215+
)
216+
217+
func LibAddUse() {
218+
println(Add(1, 2))
219+
}
220+
221+
func MulInt(a, b int) int {
222+
return a * b
223+
}
224+
225+
func MulFloat(a, b float64) float64 {
226+
return a * b
227+
}
228+
229+
func Mul = (
230+
MulInt
231+
func(a, b string) string {
232+
return a + b
233+
}
234+
MulFloat
235+
)
236+
-- lib/gop_autogen.go --
237+
package lib
238+
239+
import "fmt"
240+
241+
const GopPackage = true
242+
const _ = true
243+
const Gopo_Mul = "MulInt,,MulFloat"
244+
func Add__0(a int, b int) int {
245+
return a + b
246+
}
247+
func Add__1(a string, b string) string {
248+
return a + b
249+
}
250+
func MulInt(a int, b int) int {
251+
return a * b
252+
}
253+
func Mul__1(a string, b string) string {
254+
return a + b
255+
}
256+
func MulFloat(a float64, b float64) float64 {
257+
return a * b
258+
}
259+
func LibAddUse() {
260+
fmt.Println(Add__0(1, 2))
261+
}
262+
-- def.gop --
263+
func Add = (
264+
func(a, b int) int {
265+
return a + b
266+
}
267+
func(a, b string) string {
268+
return a + b
269+
}
270+
)
271+
-- test.gop --
272+
import "mod.com/lib"
273+
274+
println Add(1, 2)
275+
println Add("Hello", "World")
276+
277+
println lib.Add(1, 2)
278+
println lib.Add("Hello", "World")
279+
280+
println lib.Mul(1, 2)
281+
println lib.Mul("Hello", "World")
282+
println lib.Mul(200.5, 2.3)
283+
-- gop_autogen.go --
284+
package main
285+
286+
import (
287+
"fmt"
288+
"mod.com/lib"
289+
)
290+
291+
const _ = true
292+
func Add__0(a int, b int) int {
293+
return a + b
294+
}
295+
func Add__1(a string, b string) string {
296+
return a + b
297+
}
298+
func main() {
299+
fmt.Println(Add__0(1, 2))
300+
fmt.Println(Add__1("Hello", "World"))
301+
fmt.Println(lib.Add__0(1, 2))
302+
fmt.Println(lib.Add__1("Hello", "World"))
303+
fmt.Println(lib.MulInt(1, 2))
304+
fmt.Println(lib.Mul__1("Hello", "World"))
305+
fmt.Println(lib.MulFloat(200.5, 2.3))
306+
}
307+
`
308+
testCases := []refTest{
309+
{
310+
"def.gop", `Add`, []string{
311+
"def.gop 0:5-0:8", // overload decl
312+
"test.gop 2:8-2:11", // overload int call
313+
"test.gop 3:8-3:11", // overload string call
314+
},
315+
},
316+
{
317+
"lib/lib.gop", `Add`, []string{
318+
"lib/lib.gop 2:5-2:8", // overload decl
319+
"lib/lib.gop 12:9-12:12", // same package use
320+
"test.gop 5:12-5:15", // cross package use lib.Add
321+
"test.gop 6:12-6:15", // cross package use lib.Add
322+
},
323+
},
324+
{
325+
"lib/lib.gop", `func (Mul) = \(`, []string{
326+
"lib/lib.gop 23:5-23:8",
327+
"test.gop 8:12-8:15",
328+
"test.gop 9:12-9:15",
329+
"test.gop 10:12-10:15",
330+
},
331+
},
332+
}
333+
runFindRefTest(t, files, testCases)
334+
}
335+
199336
type refTest struct {
200337
defineFile string
201338
defineLocReg string

0 commit comments

Comments
 (0)