1
+ let s: supports_floating = exists (' *nvim_open_win' )
2
+ let s: win = v: false
3
+
4
+ function ! lsp#ui#vim#output#closepreview () abort
5
+ if win_getid () == s: win
6
+ " Don't close if window got focus
7
+ return
8
+ endif
9
+ pclose
10
+ let s: win = v: false
11
+ autocmd ! lsp_float_preview_close CursorMoved ,CursorMovedI ,VimResized *
12
+ endfunction
13
+
14
+ function ! s: get_float_positioning (height, width)
15
+ let l: height = a: height
16
+ let l: width = a: width
17
+ " For a start show it below/above the cursor
18
+ " TODO: add option to configure it 'docked' at the bottom/top/right
19
+ let l: y = winline ()
20
+ if l: y + l: height >= &lines
21
+ " Float does not fit
22
+ if l: y - 2 > l: height
23
+ " Fits above
24
+ let l: y = winline () - l: height
25
+ elseif l: y - 2 > &lines - l: y
26
+ " Take space above cursor
27
+ let l: y = 1
28
+ let l: height = winline ()-2
29
+ else
30
+ " Take space below cursor
31
+ let l: height = &lines - l: y
32
+ endif
33
+ endif
34
+ let l: col = col (" ." )
35
+ " Positioning is not window but screen relative
36
+ let l: opts = {
37
+ \ ' relative' : ' win' ,
38
+ \ ' row' : l: y ,
39
+ \ ' col' : l: col ,
40
+ \ ' width' : l: width ,
41
+ \ ' height' : l: height ,
42
+ \ }
43
+ return l: opts
44
+ endfunction
45
+
46
+ function ! lsp#ui#vim#output#floatingpreview (data) abort
47
+ let l: buf = nvim_create_buf (v: false , v: true )
48
+ call setbufvar (l: buf , ' &signcolumn' , ' no' )
49
+
50
+ " Try to get as much pace right-bolow the cursor, but at least 10x10
51
+ let l: width = max ([float2nr (&columns - col (" ." ) - 10 ), 10 ])
52
+ let l: height = max ([&lines - winline () + 1 , 10 ])
53
+
54
+ let l: opts = s: get_float_positioning (l: height , l: width )
55
+
56
+ let s: win = nvim_open_win (buf , v: true , l: opts )
57
+ call nvim_win_set_option (s: win , ' winhl' , " Normal:Pmenu,NormalNC:Pmenu" )
58
+ call nvim_win_set_option (s: win , ' foldenable' , v: false )
59
+ call nvim_win_set_option (s: win , ' wrap' , v: true )
60
+ call nvim_win_set_option (s: win , ' statusline' , ' ' )
61
+ call nvim_win_set_option (s: win , ' number' , v: false )
62
+ call nvim_win_set_option (s: win , ' relativenumber' , v: false )
63
+ call nvim_win_set_option (s: win , ' cursorline' , v: false )
64
+ " Enable closing the preview with esc, but map only in the scratch buffer
65
+ nmap <buffer> <silent> <esc> :pclose<cr>
66
+ return s: win
67
+ endfunction
68
+
1
69
function ! lsp#ui#vim#output#preview (data) abort
2
70
" Close any previously opened preview window
3
71
pclose
4
72
5
73
let l: current_window_id = win_getid ()
6
74
7
- execute &previewheight .' new'
75
+ if s: supports_floating && g: lsp_preview_float
76
+ call lsp#ui#vim#output#floatingpreview (a: data )
77
+ else
78
+ execute &previewheight .' new'
79
+ endif
80
+ let s: win = win_getid ()
8
81
9
82
let l: ft = s: append (a: data )
10
83
" Delete first empty line
@@ -13,6 +86,9 @@ function! lsp#ui#vim#output#preview(data) abort
13
86
setlocal readonly nomodifiable
14
87
15
88
let &l: filetype = l: ft . ' .lsp-hover'
89
+ " Get size information while still having the buffer active
90
+ let l: bufferlines = line (" $" )
91
+ let l: maxwidth = max (map (getline (1 , ' $' ), ' strdisplaywidth(v:val)' ))
16
92
17
93
if g: lsp_preview_keep_focus
18
94
" restore focus to the previous window
@@ -21,6 +97,17 @@ function! lsp#ui#vim#output#preview(data) abort
21
97
22
98
echo ' '
23
99
100
+ if s: supports_floating && s: win && g: lsp_preview_float
101
+ let l: win_config = {}
102
+ let l: height = min ([winheight (s: win ), l: bufferlines ])
103
+ let l: width = min ([winwidth (s: win ), l: maxwidth ])
104
+ let l: win_config = s: get_float_positioning (l: height , l: width )
105
+ call nvim_win_set_config (s: win , l: win_config )
106
+ augroup lsp_float_preview_close
107
+ autocmd ! lsp_float_preview_close CursorMoved ,CursorMovedI ,VimResized *
108
+ autocmd CursorMoved ,CursorMovedI ,VimResized * call lsp#ui#vim#output#closepreview ()
109
+ augroup END
110
+ endif
24
111
return ' '
25
112
endfunction
26
113
0 commit comments