mirror of
https://github.com/golang/go
synced 2024-11-19 03:04:42 -07:00
80313e1ba7
Rather than panicking when we have not created any views for the packages, we should show a reasonable error to the user. This change propagates the errors to the user. Updates golang/go#35599 Change-Id: I49789d8ce18e154f111bc3584488f468a129e30c Reviewed-on: https://go-review.googlesource.com/c/tools/+/207344 Run-TryBot: Rebecca Stambler <rstambler@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Michael Matloob <matloob@golang.org>
66 lines
1.5 KiB
Go
66 lines
1.5 KiB
Go
// 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"
|
|
"golang.org/x/tools/internal/span"
|
|
)
|
|
|
|
func (s *Server) definition(ctx context.Context, params *protocol.DefinitionParams) ([]protocol.Location, error) {
|
|
uri := span.NewURI(params.TextDocument.URI)
|
|
view, err := s.session.ViewOf(uri)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
f, err := view.GetFile(ctx, uri)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
ident, err := source.Identifier(ctx, view, f, params.Position)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
decRange, err := ident.Declaration.Range()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return []protocol.Location{
|
|
{
|
|
URI: protocol.NewURI(ident.Declaration.URI()),
|
|
Range: decRange,
|
|
},
|
|
}, nil
|
|
}
|
|
|
|
func (s *Server) typeDefinition(ctx context.Context, params *protocol.TypeDefinitionParams) ([]protocol.Location, error) {
|
|
uri := span.NewURI(params.TextDocument.URI)
|
|
view, err := s.session.ViewOf(uri)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
f, err := view.GetFile(ctx, uri)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
ident, err := source.Identifier(ctx, view, f, params.Position)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
identRange, err := ident.Type.Range()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return []protocol.Location{
|
|
{
|
|
URI: protocol.NewURI(ident.Type.URI()),
|
|
Range: identRange,
|
|
},
|
|
}, nil
|
|
}
|