Skip to content

Commit f40a1c3

Browse files
committed
Add LanguageClient#openHoverInSeparateWindow()
to reopen floating hover in separate preview window
1 parent 372939d commit f40a1c3

File tree

3 files changed

+74
-10
lines changed

3 files changed

+74
-10
lines changed

autoload/LanguageClient.vim

+41-10
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ function! s:CloseFloatingHoverOnCursorMove(win_id, opened) abort
273273
" was really moved
274274
return
275275
endif
276-
autocmd! plugin-LC-neovim-close-hover
276+
silent! autocmd! plugin-LC-neovim-close-hover
277277
let winnr = win_id2win(a:win_id)
278278
if winnr == 0
279279
return
@@ -285,7 +285,7 @@ function! s:CloseFloatingHoverOnBufEnter(win_id, bufnr) abort
285285
let winnr = win_id2win(a:win_id)
286286
if winnr == 0
287287
" Float window was already closed
288-
autocmd! plugin-LC-neovim-close-hover
288+
silent! autocmd! plugin-LC-neovim-close-hover
289289
return
290290
endif
291291
if winnr == winnr()
@@ -296,7 +296,7 @@ function! s:CloseFloatingHoverOnBufEnter(win_id, bufnr) abort
296296
" When current buffer opened hover window, it's not another buffer. Skipped
297297
return
298298
endif
299-
autocmd! plugin-LC-neovim-close-hover
299+
silent! autocmd! plugin-LC-neovim-close-hover
300300
execute winnr . 'wincmd c'
301301
endfunction
302302

@@ -388,17 +388,22 @@ function! s:OpenHoverPreview(bufname, lines, filetype) abort
388388
endif
389389
endfunction
390390

391-
function! s:MoveIntoHoverPreview() abort
391+
function! s:GetHoverPreviewBufnr() abort
392392
for bufnr in range(1, bufnr('$'))
393393
if bufname(bufnr) ==# '__LanguageClient__'
394-
let winnr = bufwinnr(bufnr)
395-
if winnr != -1
396-
execute winnr . 'wincmd w'
397-
endif
398-
return v:true
394+
return bufnr
399395
endif
400396
endfor
401-
return v:false
397+
return -1
398+
endfunction
399+
400+
function! s:MoveIntoHoverPreview() abort
401+
let winnr = bufwinnr(s:GetHoverPreviewBufnr())
402+
if winnr == -1
403+
return v:false
404+
endif
405+
execute winnr . 'wincmd w'
406+
return v:true
402407
endfunction
403408

404409
let s:id = 1
@@ -1294,4 +1299,30 @@ function! LanguageClient#debugInfo(...) abort
12941299
return LanguageClient#Call('languageClient/debugInfo', l:params, l:Callback)
12951300
endfunction
12961301

1302+
function! LanguageClient#openHoverInSeparateWindow() abort
1303+
let bufnr = s:GetHoverPreviewBufnr()
1304+
if bufnr == -1
1305+
echo 'No hover found'
1306+
return
1307+
endif
1308+
1309+
let lines = nvim_buf_get_lines(bufnr, 1, -1, v:false)
1310+
let filetype = nvim_buf_get_option(bufnr, 'filetype')
1311+
let name = bufname(bufnr)
1312+
1313+
silent! autocmd! plugin-LC-neovim-close-hover
1314+
let winnr = bufwinnr(bufnr)
1315+
if winnr != -1
1316+
execute winnr . 'wincmd c'
1317+
endif
1318+
1319+
execute 'silent! noswapfile pedit!' name
1320+
wincmd P
1321+
setlocal buftype=nofile nobuflisted bufhidden=wipe nonumber norelativenumber signcolumn=no
1322+
let &filetype = filetype
1323+
call setline(1, lines)
1324+
setlocal nomodified nomodifiable
1325+
wincmd p
1326+
endfunction
1327+
12971328
let g:LanguageClient_loaded = s:Launch()

doc/LanguageClient.txt

+7
Original file line numberDiff line numberDiff line change
@@ -633,6 +633,13 @@ Signature: LanguageClient#debugInfo(...)
633633

634634
Print out debug info.
635635

636+
*LanguageClient#openHoverInSeparateWindow*
637+
Signature: LanguageClient#openHoverInSeparateWindow()
638+
639+
When a hover is open in floating window, calling this function reopens the
640+
content in a separate preview window. This function is useful when you want to
641+
keep it open.
642+
636643
==============================================================================
637644
5. Events *LanguageClientEvents*
638645

tests/LanguageClient_test.py

+26
Original file line numberDiff line numberDiff line change
@@ -333,3 +333,29 @@ def test_textDocument_hover_float_window_move_cursor_into_window(nvim):
333333
# Check float window buffer was closed by :close in the window
334334
assert all(
335335
b for b in nvim.buffers if not b.name.endswith("__LanguageClient__"))
336+
337+
338+
def test_textDocument_hover_float_window_reopen_in_preview_window(nvim):
339+
if not nvim.funcs.exists("*nvim_open_win"):
340+
pytest.skip("Neovim 0.3.0 or earlier does not support floating window")
341+
342+
nvim.command("edit! {}".format(PATH_INDEXJS))
343+
time.sleep(1)
344+
345+
_open_float_window(nvim)
346+
347+
try:
348+
nvim.call("LanguageClient#openHoverInSeparateWindow")
349+
preview_buf = next(
350+
b for b in nvim.buffers if b.name.endswith("__LanguageClient__"))
351+
352+
winnr = nvim.funcs.bufwinnr(preview_buf.number)
353+
assert winnr > 0
354+
355+
win_id = nvim.funcs.win_getid(winnr)
356+
assert win_id > 0
357+
358+
assert nvim.api.win_get_option(win_id, 'previewwindow') == 1
359+
assert nvim.api.buf_get_option(preview_buf, 'filetype') == 'markdown'
360+
finally:
361+
nvim.api.win_close(win_id, True)

0 commit comments

Comments
 (0)