]> Sergey Matveev's repositories - vim-lsp.git/blob - autoload/lsp/hover.vim
Markdown is not applied in the preview window. Fixes #359
[vim-lsp.git] / autoload / lsp / hover.vim
1 vim9script
2
3 # Functions related to displaying hover symbol information.
4
5 import './util.vim'
6 import './options.vim' as opt
7
8 # Util used to compute the hoverText from textDocument/hover reply
9 def GetHoverText(lspserver: dict<any>, hoverResult: any): list<any>
10   if hoverResult->empty()
11     return ['', '']
12   endif
13
14   # MarkupContent
15   if hoverResult.contents->type() == v:t_dict
16       && hoverResult.contents->has_key('kind')
17     if hoverResult.contents.kind == 'plaintext'
18       return [hoverResult.contents.value->split("\n"), 'text']
19     endif
20
21     if hoverResult.contents.kind == 'markdown'
22       return [hoverResult.contents.value->split("\n"), 'lspgfm']
23     endif
24
25     lspserver.errorLog(
26       $'{strftime("%m/%d/%y %T")}: Unsupported hover contents kind ({hoverResult.contents.kind})'
27     )
28     return ['', '']
29   endif
30
31   # MarkedString
32   if hoverResult.contents->type() == v:t_dict
33       && hoverResult.contents->has_key('value')
34     return [
35       [$'``` {hoverResult.contents.language}']
36         + hoverResult.contents.value->split("\n")
37         + ['```'],
38       'lspgfm'
39     ]
40   endif
41
42   # MarkedString
43   if hoverResult.contents->type() == v:t_string
44     return [hoverResult.contents->split("\n"), 'lspgfm']
45   endif
46
47   # interface MarkedString[]
48   if hoverResult.contents->type() == v:t_list
49     var hoverText: list<string> = []
50     for e in hoverResult.contents
51       if !hoverText->empty()
52         hoverText->extend(['- - -'])
53       endif
54
55       if e->type() == v:t_string
56         hoverText->extend(e->split("\n"))
57       else
58         hoverText->extend([$'``` {e.language}'])
59         hoverText->extend(e.value->split("\n"))
60         hoverText->extend(['```'])
61       endif
62     endfor
63
64     return [hoverText, 'lspgfm']
65   endif
66
67   lspserver.errorLog(
68     $'{strftime("%m/%d/%y %T")}: Unsupported hover reply ({hoverResult})'
69   )
70   return ['', '']
71 enddef
72
73 # process the 'textDocument/hover' reply from the LSP server
74 # Result: Hover | null
75 export def HoverReply(lspserver: dict<any>, hoverResult: any, cmdmods: string): void
76   var [hoverText, hoverKind] = GetHoverText(lspserver, hoverResult)
77
78   # Nothing to show
79   if hoverText->empty()
80     if cmdmods !~ 'silent'
81       util.WarnMsg($'No documentation found for current keyword')
82     endif
83     return
84   endif
85
86   if opt.lspOptions.hoverInPreview
87     execute $':silent! {cmdmods} pedit LspHover'
88     :wincmd P
89     :setlocal buftype=nofile
90     :setlocal bufhidden=delete
91     bufnr()->deletebufline(1, '$')
92     hoverText->append(0)
93     [1, 1]->cursor()
94     exe $'setlocal ft={hoverKind}'
95     :wincmd p
96   else
97     popup_clear()
98     var winid = hoverText->popup_atcursor({moved: 'any',
99                                            close: 'click',
100                                            fixed: true,
101                                            maxwidth: 80,
102                                            minwidth: 80,
103                                            border: [0, 1, 0, 1],
104                                            borderchars: [' ']})
105     win_execute(winid, $'setlocal ft={hoverKind}')
106   endif
107 enddef
108
109 # vim: tabstop=8 shiftwidth=2 softtabstop=2