internal/lsp: fix declarations in references

Fixes golang/go#34087

Change-Id: I854c03cd124fe783f838dc53ee76cec5fffa563b
Reviewed-on: https://go-review.googlesource.com/c/tools/+/193379
Run-TryBot: Rebecca Stambler <rstambler@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Cottrell <iancottrell@google.com>
This commit is contained in:
Rebecca Stambler 2019-09-04 17:23:20 -04:00
parent 70bfb60283
commit df305b82d2
5 changed files with 39 additions and 27 deletions

View File

@ -621,7 +621,6 @@ func (r *runner) Reference(t *testing.T, data tests.References) {
if err != nil { if err != nil {
t.Fatalf("failed for %v: %v", src, err) t.Fatalf("failed for %v: %v", src, err)
} }
if len(got) != len(want) { if len(got) != len(want) {
t.Errorf("references failed: different lengths got %v want %v", len(got), len(want)) t.Errorf("references failed: different lengths got %v want %v", len(got), len(want))
} }

View File

@ -57,17 +57,22 @@ func (s *Server) references(ctx context.Context, params *protocol.ReferenceParam
// it is added to the beginning of the list if IncludeDeclaration // it is added to the beginning of the list if IncludeDeclaration
// was specified. // was specified.
if params.Context.IncludeDeclaration { if params.Context.IncludeDeclaration {
rng, err := ident.Declaration.Range() decSpan, err := ident.Declaration.Span()
if err != nil { if err != nil {
return nil, err return nil, err
} }
locations = append([]protocol.Location{ if !seen[decSpan] {
{ rng, err := ident.Declaration.Range()
URI: protocol.NewURI(ident.Declaration.URI()), if err != nil {
Range: rng, return nil, err
}, }
}, locations...) locations = append([]protocol.Location{
{
URI: protocol.NewURI(ident.Declaration.URI()),
Range: rng,
},
}, locations...)
}
} }
return locations, nil return locations, nil
} }

View File

@ -61,37 +61,37 @@ func (i *IdentifierInfo) References(ctx context.Context, view View) ([]*Referenc
if obj == nil || !sameObj(obj, i.Declaration.obj) { if obj == nil || !sameObj(obj, i.Declaration.obj) {
continue continue
} }
reference := &ReferenceInfo{ rng, err := posToRange(ctx, view, ident.Pos(), ident.End())
if err != nil {
return nil, err
}
// Add the declarations at the beginning of the references list.
references = append([]*ReferenceInfo{{
Name: ident.Name, Name: ident.Name,
ident: ident, ident: ident,
obj: obj, obj: obj,
pkg: pkg, pkg: pkg,
isDeclaration: true, isDeclaration: true,
} mappedRange: rng,
if reference.mappedRange, err = posToRange(ctx, view, ident.Pos(), ident.End()); err != nil { }}, references...)
return nil, err
}
// Add the declarations at the beginning of the references list.
references = append([]*ReferenceInfo{reference}, references...)
} }
for ident, obj := range info.Uses { for ident, obj := range info.Uses {
if obj == nil || !sameObj(obj, i.Declaration.obj) { if obj == nil || !sameObj(obj, i.Declaration.obj) {
continue continue
} }
reference := &ReferenceInfo{ rng, err := posToRange(ctx, view, ident.Pos(), ident.End())
Name: ident.Name, if err != nil {
ident: ident,
pkg: pkg,
obj: obj,
}
if reference.mappedRange, err = posToRange(ctx, view, ident.Pos(), ident.End()); err != nil {
return nil, err return nil, err
} }
references = append(references, reference) references = append(references, &ReferenceInfo{
Name: ident.Name,
ident: ident,
pkg: pkg,
obj: obj,
mappedRange: rng,
})
} }
} }
return references, nil return references, nil
} }

View File

@ -9,3 +9,11 @@ func _(_ int) []bool { //@mark(argInt, "int")
func _(_ string) int { //@mark(returnInt, "int") func _(_ string) int { //@mark(returnInt, "int")
return 0 return 0
} }
var q string //@mark(declQ, "q"),refs("q", declQ, assignQ, bobQ)
func _() {
q = "hello" //@mark(assignQ, "q")
bob := func(_ string) {}
bob(q) //@mark(bobQ, "q")
}

View File

@ -38,7 +38,7 @@ const (
ExpectedTypeDefinitionsCount = 2 ExpectedTypeDefinitionsCount = 2
ExpectedFoldingRangesCount = 1 ExpectedFoldingRangesCount = 1
ExpectedHighlightsCount = 2 ExpectedHighlightsCount = 2
ExpectedReferencesCount = 5 ExpectedReferencesCount = 6
ExpectedRenamesCount = 20 ExpectedRenamesCount = 20
ExpectedPrepareRenamesCount = 8 ExpectedPrepareRenamesCount = 8
ExpectedSymbolsCount = 1 ExpectedSymbolsCount = 1