2019-04-17 13:37:20 -06:00
|
|
|
// Copyright 2019 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package lsp
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"golang.org/x/tools/internal/lsp/protocol"
|
|
|
|
"golang.org/x/tools/internal/lsp/source"
|
|
|
|
)
|
|
|
|
|
2019-09-07 15:01:26 -06:00
|
|
|
func (s *Server) definition(ctx context.Context, params *protocol.DefinitionParams) ([]protocol.Location, error) {
|
2020-07-02 16:34:10 -06:00
|
|
|
snapshot, fh, ok, release, err := s.beginFileRequest(ctx, params.TextDocument.URI, source.Go)
|
|
|
|
defer release()
|
2020-02-13 11:46:49 -07:00
|
|
|
if !ok {
|
2019-11-15 10:43:45 -07:00
|
|
|
return nil, err
|
|
|
|
}
|
2020-01-30 17:24:33 -07:00
|
|
|
ident, err := source.Identifier(ctx, snapshot, fh, params.Position)
|
2019-04-17 13:37:20 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-12-08 21:19:55 -07:00
|
|
|
|
|
|
|
var locations []protocol.Location
|
|
|
|
for _, ref := range ident.Declaration.MappedRange {
|
|
|
|
decRange, err := ref.Range()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
locations = append(locations, protocol.Location{
|
|
|
|
URI: protocol.URIFromSpanURI(ref.URI()),
|
2019-08-26 22:26:45 -06:00
|
|
|
Range: decRange,
|
2019-12-08 21:19:55 -07:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return locations, nil
|
2019-04-17 13:37:20 -06:00
|
|
|
}
|
|
|
|
|
2019-09-07 15:01:26 -06:00
|
|
|
func (s *Server) typeDefinition(ctx context.Context, params *protocol.TypeDefinitionParams) ([]protocol.Location, error) {
|
2020-07-02 16:34:10 -06:00
|
|
|
snapshot, fh, ok, release, err := s.beginFileRequest(ctx, params.TextDocument.URI, source.Go)
|
|
|
|
defer release()
|
2020-02-13 11:46:49 -07:00
|
|
|
if !ok {
|
2019-11-15 10:43:45 -07:00
|
|
|
return nil, err
|
|
|
|
}
|
2020-01-30 17:24:33 -07:00
|
|
|
ident, err := source.Identifier(ctx, snapshot, fh, params.Position)
|
2019-04-17 13:37:20 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-08-26 22:26:45 -06:00
|
|
|
identRange, err := ident.Type.Range()
|
2019-04-17 13:37:20 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-08-26 22:26:45 -06:00
|
|
|
return []protocol.Location{
|
|
|
|
{
|
2020-02-12 14:36:46 -07:00
|
|
|
URI: protocol.URIFromSpanURI(ident.Type.URI()),
|
2019-08-26 22:26:45 -06:00
|
|
|
Range: identRange,
|
|
|
|
},
|
|
|
|
}, nil
|
2019-04-17 13:37:20 -06:00
|
|
|
}
|