1
0
mirror of https://github.com/golang/go synced 2024-11-18 14:14:46 -07:00
go/internal/lsp/hover.go
pjw 5091d647ee internal/lsp: reorganize the generated Go code for the lsp protocol
Code generation has been unified, so that tsprotocol.go and tsserver.go
are produced by the same program. tsprotocol.go is about 900 lines shorter,
partly from removing boilerplate comments that golint no longer requires.
(And partly by generating fewer unneeded types.)

The choice made for a union type is commented with the set of types. There
is no Go equivalent for union types, but making themn all interface{}
would replace type checking at unmarshalling with checking runtime
conversions.

Intersection types (A&B) are sometimes embedded (struct{A;B;}, and
sometimes expanded, as they have to be if A and B have fields with the
same names.

There are fewer embedded structs, which had been verbose and confusing to
initialize. They have been replaced by types whose names end in Gn.

Essentially all the generated *structs have been removed. This makes
no difference in what the client sends, and the server may send a {}
where it previously might have sent nothing. The benefit is that some
nil tests can be removed. Thus 'omitempty' in json tags is just
documentation that the element is optional in the protocol.

The files that generate this code will be submitted later, but soon.

Change-Id: I52b997d9c58de3d733fc8c6ce061e47ce2bdb100
Reviewed-on: https://go-review.googlesource.com/c/tools/+/207598
Run-TryBot: Peter Weinberger <pjw@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Cottrell <iancottrell@google.com>
2019-11-18 19:51:19 +00:00

94 lines
2.3 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"
"encoding/json"
"fmt"
"golang.org/x/tools/internal/lsp/protocol"
"golang.org/x/tools/internal/lsp/source"
"golang.org/x/tools/internal/span"
"golang.org/x/tools/internal/telemetry/log"
)
func (s *Server) hover(ctx context.Context, params *protocol.HoverParams) (*protocol.Hover, 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, nil
}
hover, err := ident.Hover(ctx)
if err != nil {
return nil, err
}
rng, err := ident.Range()
if err != nil {
return nil, err
}
contents := s.toProtocolHoverContents(ctx, hover, view.Options())
return &protocol.Hover{
Contents: contents,
Range: rng,
}, nil
}
func (s *Server) toProtocolHoverContents(ctx context.Context, h *source.HoverInformation, options source.Options) protocol.MarkupContent {
content := protocol.MarkupContent{
Kind: options.PreferredContentFormat,
}
signature := h.Signature
if content.Kind == protocol.Markdown {
signature = fmt.Sprintf("```go\n%s\n```", h.Signature)
}
switch options.HoverKind {
case source.SingleLine:
doc := h.SingleLine
if content.Kind == protocol.Markdown {
doc = source.CommentToMarkdown(doc)
}
content.Value = doc
case source.NoDocumentation:
content.Value = signature
case source.SynopsisDocumentation:
if h.Synopsis != "" {
doc := h.Synopsis
if content.Kind == protocol.Markdown {
doc = source.CommentToMarkdown(h.Synopsis)
}
content.Value = fmt.Sprintf("%s\n%s", doc, signature)
} else {
content.Value = signature
}
case source.FullDocumentation:
if h.FullDocumentation != "" {
doc := h.FullDocumentation
if content.Kind == protocol.Markdown {
doc = source.CommentToMarkdown(h.FullDocumentation)
}
content.Value = fmt.Sprintf("%s\n%s", signature, doc)
} else {
content.Value = signature
}
case source.Structured:
b, err := json.Marshal(h)
if err != nil {
log.Error(ctx, "failed to marshal structured hover", err)
}
content.Value = string(b)
}
return content
}