Skip to content

Commit 9a30a9a

Browse files
muirdmstamblerre
authored andcommitted
internal/lsp: trim down implementations code
Remove the unused code that was tracking concrete-type => interface-type mappings. It isn't clear if there is a good spot for this in LSP. I also made it skip interface types when looking for implementations. It doesn't seem useful to be shown other interface types/methods when you are looking for implementations of a given interface type/method. Change-Id: Ib59fb717e5c1a181cc713581a22e60ed654b918c Reviewed-on: https://go-review.googlesource.com/c/tools/+/210279 Run-TryBot: Muir Manders <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Rebecca Stambler <[email protected]>
1 parent 259af5f commit 9a30a9a

File tree

3 files changed

+17
-49
lines changed

3 files changed

+17
-49
lines changed

internal/lsp/source/implementation.go

Lines changed: 11 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -120,68 +120,38 @@ func (i *IdentifierInfo) implementations(ctx context.Context) (implementsResult,
120120
T = i.Type.Object.Type()
121121
}
122122

123-
// Find all named types, even local types (which can have
124-
// methods due to promotion) and the built-in "error".
125-
// We ignore aliases 'type M = N' to avoid duplicate
126-
// reporting of the Named type N.
123+
// Find all named types, even local types (which can have methods
124+
// due to promotion). We ignore aliases 'type M = N' to avoid
125+
// duplicate reporting of the Named type N.
127126
var allNamed []*types.Named
128127
pkgs := map[*types.Named]Package{}
129128
for _, pkg := range i.Snapshot.KnownPackages(ctx) {
130129
info := pkg.GetTypesInfo()
131130
for _, obj := range info.Defs {
132131
if obj, ok := obj.(*types.TypeName); ok && !obj.IsAlias() {
133-
if named, ok := obj.Type().(*types.Named); ok {
132+
if named, ok := obj.Type().(*types.Named); ok && !isInterface(named) {
134133
allNamed = append(allNamed, named)
135134
pkgs[named] = pkg
136135
}
137136
}
138137
}
139138
}
140-
allNamed = append(allNamed, types.Universe.Lookup("error").Type().(*types.Named))
141139

142140
var msets typeutil.MethodSetCache
143141

144-
// TODO(matloob): We only use the to and toMethod result for now. Figure out if we want to
145-
// surface the from and fromPtr results to users.
146142
// Test each named type.
147-
var to, from, fromPtr []types.Type
143+
var to []types.Type
148144
for _, U := range allNamed {
149145
if isInterface(T) {
150146
if msets.MethodSet(T).Len() == 0 {
151147
continue // empty interface
152148
}
153-
if isInterface(U) {
154-
if msets.MethodSet(U).Len() == 0 {
155-
continue // empty interface
156-
}
157-
158-
// T interface, U interface
159-
if !types.Identical(T, U) {
160-
if types.AssignableTo(U, T) {
161-
to = append(to, U)
162-
}
163-
if types.AssignableTo(T, U) {
164-
from = append(from, U)
165-
}
166-
}
167-
} else {
168-
// T interface, U concrete
169-
if types.AssignableTo(U, T) {
170-
to = append(to, U)
171-
} else if pU := types.NewPointer(U); types.AssignableTo(pU, T) {
172-
to = append(to, pU)
173-
}
174-
}
175-
} else if isInterface(U) {
176-
if msets.MethodSet(U).Len() == 0 {
177-
continue // empty interface
178-
}
179149

180-
// T concrete, U interface
181-
if types.AssignableTo(T, U) {
182-
from = append(from, U)
183-
} else if pT := types.NewPointer(T); types.AssignableTo(pT, U) {
184-
fromPtr = append(fromPtr, U)
150+
// T interface, U concrete
151+
if types.AssignableTo(U, T) {
152+
to = append(to, U)
153+
} else if pU := types.NewPointer(U); types.AssignableTo(pU, T) {
154+
to = append(to, pU)
185155
}
186156
}
187157
}
@@ -192,14 +162,12 @@ func (i *IdentifierInfo) implementations(ctx context.Context) (implementsResult,
192162
types.NewMethodSet(t).Lookup(method.Pkg(), method.Name()))
193163
}
194164
}
195-
return implementsResult{pkgs, to, from, fromPtr, toMethod}, nil
165+
return implementsResult{pkgs, to, toMethod}, nil
196166
}
197167

198168
// implementsResult contains the results of an implements query.
199169
type implementsResult struct {
200170
pkgs map[*types.Named]Package
201171
to []types.Type // named or ptr-to-named types assignable to interface T
202-
from []types.Type // named interfaces assignable from T
203-
fromPtr []types.Type // named interfaces assignable only from *T
204172
toMethod []*types.Selection
205173
}

internal/lsp/testdata/implementation/implementation.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@ type ImpS struct{} //@ImpS
1212
func (ImpS) Laugh() { //@mark(LaughS, "Laugh")
1313
}
1414

15-
type ImpI interface { //@ImpI
16-
Laugh() //@mark(LaughI, "Laugh"),implementations("Laugh", LaughP, OtherLaughP, LaughS, LaughL, OtherLaughI, OtherLaughS)
15+
type ImpI interface {
16+
Laugh() //@implementations("Laugh", LaughP, OtherLaughP, LaughS, OtherLaughS)
1717
}
1818

19-
type Laugher interface { //@implementations("Laugher", ImpP, OtherImpP, ImpI, ImpS, OtherImpI, OtherImpS)
20-
Laugh() //@mark(LaughL, "Laugh"),implementations("Laugh", LaughP, OtherLaughP, LaughI, LaughS, OtherLaughI, OtherLaughS)
19+
type Laugher interface { //@implementations("Laugher", ImpP, OtherImpP, ImpS, OtherImpS)
20+
Laugh() //@implementations("Laugh", LaughP, OtherLaughP, LaughS, OtherLaughS)
2121
}
2222

2323
type Foo struct {

internal/lsp/testdata/implementation/other/other.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ type ImpS struct{} //@mark(OtherImpS, "ImpS")
1010
func (ImpS) Laugh() { //@mark(OtherLaughS, "Laugh")
1111
}
1212

13-
type ImpI interface { //@mark(OtherImpI, "ImpI")
14-
Laugh() //@mark(OtherLaughI, "Laugh")
13+
type ImpI interface {
14+
Laugh()
1515
}
1616

1717
type Foo struct {

0 commit comments

Comments
 (0)