github.com/april1989/origin-go-tools@v0.0.32/internal/lsp/definition.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/april1989/origin-go-tools/internal/lsp/protocol"
    11  	"github.com/april1989/origin-go-tools/internal/lsp/source"
    12  )
    13  
    14  func (s *Server) definition(ctx context.Context, params *protocol.DefinitionParams) ([]protocol.Location, error) {
    15  	snapshot, fh, ok, release, err := s.beginFileRequest(ctx, params.TextDocument.URI, source.Go)
    16  	defer release()
    17  	if !ok {
    18  		return nil, err
    19  	}
    20  	ident, err := source.Identifier(ctx, snapshot, fh, params.Position)
    21  	if err != nil {
    22  		return nil, err
    23  	}
    24  
    25  	var locations []protocol.Location
    26  	for _, ref := range ident.Declaration.MappedRange {
    27  		decRange, err := ref.Range()
    28  		if err != nil {
    29  			return nil, err
    30  		}
    31  
    32  		locations = append(locations, protocol.Location{
    33  			URI:   protocol.URIFromSpanURI(ref.URI()),
    34  			Range: decRange,
    35  		})
    36  	}
    37  
    38  	return locations, nil
    39  }
    40  
    41  func (s *Server) typeDefinition(ctx context.Context, params *protocol.TypeDefinitionParams) ([]protocol.Location, error) {
    42  	snapshot, fh, ok, release, err := s.beginFileRequest(ctx, params.TextDocument.URI, source.Go)
    43  	defer release()
    44  	if !ok {
    45  		return nil, err
    46  	}
    47  	ident, err := source.Identifier(ctx, snapshot, fh, params.Position)
    48  	if err != nil {
    49  		return nil, err
    50  	}
    51  	identRange, err := ident.Type.Range()
    52  	if err != nil {
    53  		return nil, err
    54  	}
    55  	return []protocol.Location{
    56  		{
    57  			URI:   protocol.URIFromSpanURI(ident.Type.URI()),
    58  			Range: identRange,
    59  		},
    60  	}, nil
    61  }