Skip to content

Commit 0970e79

Browse files
committed
Integrate vim8.1 popup for hover/preview
1 parent 4a5a0da commit 0970e79

File tree

1 file changed

+54
-26
lines changed

1 file changed

+54
-26
lines changed

autoload/lsp/ui/vim/output.vim

Lines changed: 54 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
let s:supports_floating = exists('*nvim_open_win')
1+
let s:supports_floating = exists('*nvim_open_win') || has('patch-8.1.1517')
22
let s:win = v:false
33

44
function! lsp#ui#vim#output#closepreview() abort
@@ -63,6 +63,7 @@ function! s:get_float_positioning(height, width) abort
6363
endfunction
6464

6565
function! lsp#ui#vim#output#floatingpreview(data) abort
66+
if has("nvim")
6667
let l:buf = nvim_create_buf(v:false, v:true)
6768
call setbufvar(l:buf, '&signcolumn', 'no')
6869

@@ -83,6 +84,43 @@ function! lsp#ui#vim#output#floatingpreview(data) abort
8384
" Enable closing the preview with esc, but map only in the scratch buffer
8485
nmap <buffer><silent> <esc> :pclose<cr>
8586
return s:win
87+
else
88+
return popup_atcursor('...', {
89+
\ 'moved': 'any',
90+
\ 'border': [1, 1, 1, 1],
91+
\})
92+
endif
93+
endfunction
94+
95+
function! s:setcontent(lines, ft) abort
96+
if s:supports_floating && g:lsp_preview_float && !has("nvim")
97+
" vim popup
98+
echom "Vimp Popup SetContent"
99+
call setbufline(winbufnr(s:win), 1, a:lines)
100+
call win_execute(s:win, 'setlocal filetype=' . a:ft . '.lsp-hover')
101+
else
102+
" nvim floating
103+
call setline(1, a:lines)
104+
setlocal readonly nomodifiable
105+
let &l:filetype = a:ft . '.lsp-hover'
106+
endif
107+
endfunction
108+
109+
function! s:adjust_float_placement(bufferlines, maxwidth) abort
110+
if has("nvim")
111+
let l:win_config = {}
112+
let l:height = min([winheight(s:win), a:bufferlines])
113+
let l:width = min([winwidth(s:win), a:maxwidth])
114+
let l:win_config = s:get_float_positioning(l:height, l:width)
115+
call nvim_win_set_config(s:win, l:win_config )
116+
endif
117+
endfunction
118+
119+
function! s:add_float_closing_hooks() abort
120+
augroup lsp_float_preview_close
121+
autocmd! lsp_float_preview_close CursorMoved,CursorMovedI,VimResized *
122+
autocmd CursorMoved,CursorMovedI,VimResized * call lsp#ui#vim#output#closepreview()
123+
augroup END
86124
endfunction
87125

88126
function! lsp#ui#vim#output#preview(data) abort
@@ -92,19 +130,16 @@ function! lsp#ui#vim#output#preview(data) abort
92130
let l:current_window_id = win_getid()
93131

94132
if s:supports_floating && g:lsp_preview_float
95-
call lsp#ui#vim#output#floatingpreview(a:data)
133+
let s:win = lsp#ui#vim#output#floatingpreview(a:data)
96134
else
97135
execute &previewheight.'new'
136+
let s:win = win_getid()
98137
endif
99-
let s:win = win_getid()
100-
101-
let l:ft = s:append(a:data)
102-
" Delete first empty line
103-
0delete _
104138

105-
setlocal readonly nomodifiable
139+
let l:lines = []
140+
let l:ft = s:append(a:data, l:lines)
141+
call s:setcontent(l:lines, l:ft)
106142

107-
let &l:filetype = l:ft . '.lsp-hover'
108143
" Get size information while still having the buffer active
109144
let l:bufferlines = line('$')
110145
let l:maxwidth = max(map(getline(1, '$'), 'strdisplaywidth(v:val)'))
@@ -116,39 +151,32 @@ function! lsp#ui#vim#output#preview(data) abort
116151

117152
echo ''
118153

119-
if s:supports_floating && s:win && g:lsp_preview_float
120-
let l:win_config = {}
121-
let l:height = min([winheight(s:win), l:bufferlines])
122-
let l:width = min([winwidth(s:win), l:maxwidth])
123-
let l:win_config = s:get_float_positioning(l:height, l:width)
124-
call nvim_win_set_config(s:win, l:win_config )
125-
augroup lsp_float_preview_close
126-
autocmd! lsp_float_preview_close CursorMoved,CursorMovedI,VimResized *
127-
autocmd CursorMoved,CursorMovedI,VimResized * call lsp#ui#vim#output#closepreview()
128-
augroup END
154+
if s:supports_floating && s:win && g:lsp_preview_float && has("nvim")
155+
call s:adjust_float_placement(l:bufferlines, l:maxwidth)
156+
call s:add_float_closing_hooks()
129157
endif
130158
return ''
131159
endfunction
132160

133-
function! s:append(data) abort
161+
function! s:append(data, lines) abort
134162
if type(a:data) == type([])
135163
for l:entry in a:data
136-
call s:append(entry)
164+
call s:append(entry, a:lines)
137165
endfor
138166

139167
return 'markdown'
140168
elseif type(a:data) == type('')
141-
silent put =a:data
169+
call extend(a:lines, split(a:data, "\n"))
142170

143171
return 'markdown'
144172
elseif type(a:data) == type({}) && has_key(a:data, 'language')
145-
silent put ='```'.a:data.language
146-
silent put =a:data.value
147-
silent put ='```'
173+
call add(a:lines, '```'.a:data.language)
174+
call extend(a:lines, split(a:data.value, '\n'))
175+
call add(a:lines, '```')
148176

149177
return 'markdown'
150178
elseif type(a:data) == type({}) && has_key(a:data, 'kind')
151-
silent put =a:data.value
179+
call add(a:lines, a:data.value)
152180

153181
return a:data.kind ==? 'plaintext' ? 'text' : a:data.kind
154182
endif

0 commit comments

Comments
 (0)