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-12 14:59:23 -06:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
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-08-13 13:07:39 -06:00
|
|
|
"golang.org/x/tools/internal/telemetry/log"
|
2019-04-17 13:37:20 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
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-08-16 11:49:17 -06:00
|
|
|
f, err := getGoFile(ctx, view, uri)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-08-26 22:26:45 -06:00
|
|
|
ident, err := source.Identifier(ctx, view, f, params.Position)
|
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-08-12 14:59:23 -06:00
|
|
|
hover, 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-08-12 14:59:23 -06:00
|
|
|
contents := s.toProtocolHoverContents(ctx, hover)
|
2019-04-17 13:37:20 -06:00
|
|
|
return &protocol.Hover{
|
2019-08-12 14:59:23 -06:00
|
|
|
Contents: contents,
|
|
|
|
Range: &rng,
|
2019-04-17 13:37:20 -06:00
|
|
|
}, nil
|
|
|
|
}
|
2019-08-12 14:59:23 -06:00
|
|
|
|
|
|
|
func (s *Server) toProtocolHoverContents(ctx context.Context, h *source.HoverInformation) protocol.MarkupContent {
|
2019-09-05 22:17:36 -06:00
|
|
|
options := s.session.Options()
|
2019-08-12 14:59:23 -06:00
|
|
|
content := protocol.MarkupContent{
|
2019-09-05 22:17:36 -06:00
|
|
|
Kind: options.PreferredContentFormat,
|
2019-08-12 14:59:23 -06:00
|
|
|
}
|
|
|
|
signature := h.Signature
|
|
|
|
if content.Kind == protocol.Markdown {
|
|
|
|
signature = fmt.Sprintf("```go\n%s\n```", h.Signature)
|
|
|
|
}
|
2019-09-05 22:17:36 -06:00
|
|
|
switch options.HoverKind {
|
|
|
|
case source.SingleLine:
|
2019-08-12 14:59:23 -06:00
|
|
|
content.Value = h.SingleLine
|
2019-09-05 22:17:36 -06:00
|
|
|
case source.NoDocumentation:
|
2019-08-12 14:59:23 -06:00
|
|
|
content.Value = signature
|
2019-09-05 22:17:36 -06:00
|
|
|
case source.SynopsisDocumentation:
|
2019-08-12 14:59:23 -06:00
|
|
|
if h.Synopsis != "" {
|
|
|
|
content.Value = fmt.Sprintf("%s\n%s", h.Synopsis, signature)
|
|
|
|
} else {
|
|
|
|
content.Value = signature
|
|
|
|
}
|
2019-09-05 22:17:36 -06:00
|
|
|
case source.FullDocumentation:
|
2019-08-12 14:59:23 -06:00
|
|
|
if h.FullDocumentation != "" {
|
|
|
|
content.Value = fmt.Sprintf("%s\n%s", signature, h.FullDocumentation)
|
|
|
|
} else {
|
|
|
|
content.Value = signature
|
|
|
|
}
|
2019-09-05 22:17:36 -06:00
|
|
|
case source.Structured:
|
2019-08-12 14:59:23 -06:00
|
|
|
b, err := json.Marshal(h)
|
|
|
|
if err != nil {
|
|
|
|
log.Error(ctx, "failed to marshal structured hover", err)
|
|
|
|
}
|
|
|
|
content.Value = string(b)
|
|
|
|
}
|
|
|
|
return content
|
|
|
|
}
|