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"
|
2019-08-07 14:03:45 -06:00
|
|
|
|
2019-04-17 13:37:20 -06:00
|
|
|
"golang.org/x/tools/internal/lsp/protocol"
|
|
|
|
"golang.org/x/tools/internal/lsp/source"
|
|
|
|
"golang.org/x/tools/internal/span"
|
|
|
|
)
|
|
|
|
|
2019-09-07 15:01:26 -06:00
|
|
|
func (s *Server) hover(ctx context.Context, params *protocol.HoverParams) (*protocol.Hover, error) {
|
2019-04-17 13:37:20 -06:00
|
|
|
uri := span.NewURI(params.TextDocument.URI)
|
2019-11-15 10:43:45 -07:00
|
|
|
view, err := s.session.ViewOf(uri)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-11-20 14:38:43 -07:00
|
|
|
snapshot := view.Snapshot()
|
2020-01-10 15:37:29 -07:00
|
|
|
fh, err := snapshot.GetFile(uri)
|
2019-08-16 11:49:17 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-12-17 16:57:54 -07:00
|
|
|
if fh.Identity().Kind != source.Go {
|
2019-12-10 09:51:34 -07:00
|
|
|
return nil, nil
|
|
|
|
}
|
2020-01-14 16:29:21 -07:00
|
|
|
ident, err := source.Identifier(ctx, snapshot, fh, params.Position, source.WidestPackageHandle)
|
2019-04-17 13:37:20 -06:00
|
|
|
if err != nil {
|
2019-07-24 10:13:56 -06:00
|
|
|
return nil, nil
|
2019-04-17 13:37:20 -06:00
|
|
|
}
|
2019-12-06 00:44:16 -07:00
|
|
|
h, err := ident.Hover(ctx)
|
2019-04-17 13:37:20 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-08-26 22:26:45 -06:00
|
|
|
rng, err := ident.Range()
|
2019-04-17 13:37:20 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-12-06 00:44:16 -07:00
|
|
|
hover, err := source.FormatHover(h, view.Options())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-04-17 13:37:20 -06:00
|
|
|
return &protocol.Hover{
|
2019-12-06 00:44:16 -07:00
|
|
|
Contents: protocol.MarkupContent{
|
|
|
|
Kind: view.Options().PreferredContentFormat,
|
|
|
|
Value: hover,
|
|
|
|
},
|
|
|
|
Range: rng,
|
2019-04-17 13:37:20 -06:00
|
|
|
}, nil
|
|
|
|
}
|