/
hover.go
43 lines (34 loc) · 992 Bytes
/
hover.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// SPDX-License-Identifier: MIT
package lsp
import (
"reflect"
"github.com/caixw/apidoc/v7/core"
"github.com/caixw/apidoc/v7/internal/lsp/protocol"
)
type usager interface {
core.Searcher
Usage() string
}
var usagerType = reflect.TypeOf((*usager)(nil)).Elem()
// textDocument/hover
//
// https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocument_hover
func (s *server) textDocumentHover(notify bool, in *protocol.HoverParams, out *protocol.Hover) error {
f := s.findFolder(in.TextDocument.URI)
if f == nil {
return nil // 非项目文件,不应该出错
}
f.parsedMux.RLock()
defer f.parsedMux.RUnlock()
if u := f.doc.Search(in.TextDocument.URI, in.TextDocumentPositionParams.Position, usagerType); u != nil {
usage := u.(usager)
if v := usage.Usage(); v != "" {
out.Range = usage.Loc().Range
out.Contents = protocol.MarkupContent{
Kind: protocol.MarkupKindMarkdown,
Value: v,
}
}
}
return nil
}