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-04-17 16:21:47 -06:00
|
|
|
"fmt"
|
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"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (s *Server) hover(ctx context.Context, params *protocol.TextDocumentPositionParams) (*protocol.Hover, error) {
|
|
|
|
uri := span.NewURI(params.TextDocument.URI)
|
2019-05-15 10:24:49 -06:00
|
|
|
view := s.session.ViewOf(uri)
|
2019-05-03 22:04:18 -06:00
|
|
|
f, m, err := getGoFile(ctx, view, uri)
|
2019-04-17 13:37:20 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
spn, err := m.PointSpan(params.Position)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
identRange, err := spn.Range(m.Converter)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
ident, err := source.Identifier(ctx, view, f, identRange.Start)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-04-27 20:45:06 -06:00
|
|
|
hover, err := ident.Hover(ctx, nil, s.preferredContentFormat == protocol.Markdown, !s.noDocsOnHover)
|
2019-04-17 13:37:20 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
identSpan, err := ident.Range.Span()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
rng, err := m.Range(identSpan)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &protocol.Hover{
|
2019-04-19 11:09:33 -06:00
|
|
|
Contents: protocol.MarkupContent{
|
|
|
|
Kind: s.preferredContentFormat,
|
|
|
|
Value: hover,
|
|
|
|
},
|
|
|
|
Range: &rng,
|
2019-04-17 13:37:20 -06:00
|
|
|
}, nil
|
|
|
|
}
|
2019-04-17 12:47:10 -06:00
|
|
|
|
2019-04-17 16:21:47 -06:00
|
|
|
func markupContent(decl, doc string, kind protocol.MarkupKind) protocol.MarkupContent {
|
2019-04-17 12:47:10 -06:00
|
|
|
result := protocol.MarkupContent{
|
|
|
|
Kind: kind,
|
|
|
|
}
|
|
|
|
switch kind {
|
|
|
|
case protocol.PlainText:
|
2019-04-17 16:21:47 -06:00
|
|
|
result.Value = decl
|
2019-04-17 12:47:10 -06:00
|
|
|
case protocol.Markdown:
|
2019-04-17 16:21:47 -06:00
|
|
|
result.Value = "```go\n" + decl + "\n```"
|
|
|
|
}
|
|
|
|
if doc != "" {
|
|
|
|
result.Value = fmt.Sprintf("%s\n%s", doc, result.Value)
|
2019-04-17 12:47:10 -06:00
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|