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
|
|
|
)
|
|
|
|
|
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()
|
2019-09-27 11:17:59 -06:00
|
|
|
f, err := view.GetFile(ctx, uri)
|
2019-08-16 11:49:17 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-11-20 14:38:43 -07:00
|
|
|
ident, err := source.Identifier(ctx, snapshot, 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-09-11 16:21:01 -06:00
|
|
|
contents := s.toProtocolHoverContents(ctx, hover, view.Options())
|
2019-04-17 13:37:20 -06:00
|
|
|
return &protocol.Hover{
|
2019-08-12 14:59:23 -06:00
|
|
|
Contents: contents,
|
2019-11-17 12:29:15 -07:00
|
|
|
Range: rng,
|
2019-04-17 13:37:20 -06:00
|
|
|
}, nil
|
|
|
|
}
|
2019-08-12 14:59:23 -06:00
|
|
|
|
2019-09-11 16:21:01 -06:00
|
|
|
func (s *Server) toProtocolHoverContents(ctx context.Context, h *source.HoverInformation, options source.Options) protocol.MarkupContent {
|
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-10-04 11:06:35 -06:00
|
|
|
|
2019-09-05 22:17:36 -06:00
|
|
|
switch options.HoverKind {
|
|
|
|
case source.SingleLine:
|
2019-10-04 11:06:35 -06:00
|
|
|
doc := h.SingleLine
|
|
|
|
if content.Kind == protocol.Markdown {
|
|
|
|
doc = source.CommentToMarkdown(doc)
|
|
|
|
}
|
|
|
|
content.Value = doc
|
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 != "" {
|
2019-10-04 11:06:35 -06:00
|
|
|
doc := h.Synopsis
|
|
|
|
if content.Kind == protocol.Markdown {
|
|
|
|
doc = source.CommentToMarkdown(h.Synopsis)
|
|
|
|
}
|
|
|
|
content.Value = fmt.Sprintf("%s\n%s", doc, signature)
|
2019-08-12 14:59:23 -06:00
|
|
|
} 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 != "" {
|
2019-10-04 11:06:35 -06:00
|
|
|
doc := h.FullDocumentation
|
|
|
|
if content.Kind == protocol.Markdown {
|
|
|
|
doc = source.CommentToMarkdown(h.FullDocumentation)
|
|
|
|
}
|
|
|
|
content.Value = fmt.Sprintf("%s\n%s", signature, doc)
|
2019-08-12 14:59:23 -06:00
|
|
|
} 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
|
|
|
|
}
|