Skip to content

Commit

Permalink
internal/lsp: include declaration for references
Browse files Browse the repository at this point in the history
A client can specify "IncludeDeclaration" in its ReferenceParams.
When they do so, we want to include the declaration, even if it was not
in the scope we searched for references.

Additionally, we also return the location of the declaration first in
the result array when it is included in the results.

Updates golang/go#32572

Change-Id: I12837cd98102ee8d531f0f4bac2fb7bded2564c0
Reviewed-on: https://go-review.googlesource.com/c/tools/+/184723
Run-TryBot: Suzy Mueller <suzmue@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
  • Loading branch information
suzmue committed Jul 3, 2019
1 parent 063514c commit abb7e64
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
12 changes: 12 additions & 0 deletions internal/lsp/references.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,18 @@ func (s *Server) references(ctx context.Context, params *protocol.ReferenceParam
if err != nil {
view.Session().Logger().Errorf(ctx, "no references for %s: %v", ident.Name, err)
}
if params.Context.IncludeDeclaration {
// The declaration of this identifier may not be in the
// scope that we search for references, so make sure
// it is added to the beginning of the list if IncludeDeclaration
// was specified.
references = append([]*source.ReferenceInfo{
&source.ReferenceInfo{
Range: ident.DeclarationRange(),
},
}, references...)
}

// Get the location of each reference to return as the result.
locations := make([]protocol.Location, 0, len(references))
seen := make(map[span.Span]bool)
Expand Down
7 changes: 4 additions & 3 deletions internal/lsp/source/references.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type ReferenceInfo struct {
}

// References returns a list of references for a given identifier within the packages
// containing i.File.
// containing i.File. Declarations appear first in the result.
func (i *IdentifierInfo) References(ctx context.Context) ([]*ReferenceInfo, error) {
var references []*ReferenceInfo

Expand Down Expand Up @@ -57,13 +57,14 @@ func (i *IdentifierInfo) References(ctx context.Context) ([]*ReferenceInfo, erro
if obj == nil || obj.Pos() != i.decl.obj.Pos() {
continue
}
references = append(references, &ReferenceInfo{
// Add the declarations at the beginning of the references list.
references = append([]*ReferenceInfo{&ReferenceInfo{
Name: ident.Name,
Range: span.NewRange(i.File.FileSet(), ident.Pos(), ident.End()),
ident: ident,
obj: obj,
isDeclaration: true,
})
}}, references...)
}
for ident, obj := range info.Uses {
if obj == nil || obj.Pos() != i.decl.obj.Pos() {
Expand Down

0 comments on commit abb7e64

Please sign in to comment.