github.com/jhump/golang-x-tools@v0.0.0-20220218190644-4958d6d39439/internal/lsp/references.go (about)

     1  // Copyright 2019 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package lsp
     6  
     7  import (
     8  	"context"
     9  
    10  	"github.com/jhump/golang-x-tools/internal/lsp/protocol"
    11  	"github.com/jhump/golang-x-tools/internal/lsp/source"
    12  	"github.com/jhump/golang-x-tools/internal/lsp/template"
    13  )
    14  
    15  func (s *Server) references(ctx context.Context, params *protocol.ReferenceParams) ([]protocol.Location, error) {
    16  	snapshot, fh, ok, release, err := s.beginFileRequest(ctx, params.TextDocument.URI, source.UnknownKind)
    17  	defer release()
    18  	if !ok {
    19  		return nil, err
    20  	}
    21  	if snapshot.View().FileKind(fh) == source.Tmpl {
    22  		return template.References(ctx, snapshot, fh, params)
    23  	}
    24  	references, err := source.References(ctx, snapshot, fh, params.Position, params.Context.IncludeDeclaration)
    25  	if err != nil {
    26  		return nil, err
    27  	}
    28  	var locations []protocol.Location
    29  	for _, ref := range references {
    30  		refRange, err := ref.Range()
    31  		if err != nil {
    32  			return nil, err
    33  		}
    34  		locations = append(locations, protocol.Location{
    35  			URI:   protocol.URIFromSpanURI(ref.URI()),
    36  			Range: refRange,
    37  		})
    38  	}
    39  	return locations, nil
    40  }