question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Show documentation on (neovim) floating window?

See original GitHub issue

Hello, Do you have a plan to implement show doc on (nvim) floating window? Like vim-go has a setting called g:go_doc_popup_window. Show doc on Floating window or Popup window may be more comfortable than preview~

Thanks.

Issue Analytics

  • State:open
  • Created 4 years ago
  • Comments:12 (2 by maintainers)

github_iconTop GitHub Comments

3reactions
blayz3rcommented, Feb 21, 2020

In the mean time I am using this hack:

image


autocmd FileType python nnoremap <buffer> K :call PyDocVim()<CR>

function! PyDocVim()
python3 << EOF
import jedi

curfile = vim.current.buffer.name
row = vim.current.window.cursor[0]
col= vim.current.window.cursor[1]

script = jedi.Script(
    source=None,
    path=curfile,
    line=row,
    column=col)

try:
    definitions = script.goto_definitions()
except Exception:
    # print to stdout, will be in :messages
    definitions = []
    print("Exception, this shouldn't happen.")
    print(traceback.format_exc())

    if not definitions:
        echo_highlight("No documentation found for that.")
        vim.command("return")

docs = []
for d in definitions:
    doc = d.docstring()
    if doc:
        title = "Docstring for %s" % d.desc_with_module
        underline = "=" * len(title)
        docs.append("%s\n%s\n%s" % (title, underline, doc))
    else:
        docs.append("|No Docstring for %s|" % d)
    text = ("\n" + "-" * 79 + "\n").join(docs)
vim.command("let docWidth = %s" % len(title))
vim.command("let doc_lines = %s" % len(text.split("\n")))
EOF
    "Scroll
    function! s:popup_filter(winid, key)
        if a:key ==# "\<c-k>"
            call win_execute(a:winid, "normal! \<c-y>")
            return v:true
        elseif a:key ==# "\<c-j>"
            call win_execute(a:winid, "normal! \<c-e>")
            return v:true
        elseif a:key ==# 'q' || a:key ==# 'x'
            return popup_filter_menu(a:winid, 'x')
        endif
        return v:false
    endfunction

    let lines = py3eval('text')
    let winid = popup_create(lines->split('\n'), #{
            \ filter: function('s:popup_filter'),
            \ pos: 'botleft',
            \ line: 'cursor-1',
            \ col: 'cursor',
            \ moved: 'any',
            \ border: [1,1,1,1,1,1,1,1],
            \ borderchars: ['─', '│', '─', '│', '┌', '┐', '┘', '└'],
            \ borderhighlight: ['Todo'],    
            \ padding: [0,1,0,1],
            \ firstline: 1,
            \ scrollbar: 1,
            \ minwidth: docWidth,
            \ maxwidth: 74,
            \ minheight: doc_lines,
            \ maxheight: 20,
            \ mapping: 0,
            \ })

    call setbufvar(winbufnr(winid), '&syntax','rst')
    call setwinvar(winid, '&wincolor', 'Normal')
endfunction

1reaction
BrunoGomesCoelhocommented, Jan 2, 2021

Thank you very much, that was indeed the problem!

I also had to change title = "Docstring for %s" % d.desc_with_module to title = "Docstring for %s" % d.full_name to make it work;

I added a line for color support in neovim since it didn’t have any color syntaxing without: call setbufvar(winbufnr(winid), '&syntax','rst')

Here's my current full code for nvim if useful


"----- show documentation in floaterm window from https://github.com/neovim/neovim/issues/1004
if has('nvim')
	autocmd FileType python nnoremap <buffer> K :call PyDocVim()<CR>

   function! PyDocVim()
python3 << EOF
import jedi

curfile = vim.current.buffer.name
row = vim.current.window.cursor[0]
col= vim.current.window.cursor[1]

script = jedi.Script(
    code=None,
    path=curfile)

try:
   definitions = script.help(line=row, column=col)
   # definitions = script.goto_definitions()
except Exception:
   # print to stdout, will be in :messages
   definitions = []
   print("Exception, this shouldn't happen.")
   print(traceback.format_exc())

   if not definitions:
	   echo_highlight("No documentation found for that.")
	   vim.command("return")

docs = []
for d in definitions:
   doc = d.docstring()
   if doc:
	   title = "Docstring for %s" % d.full_name
	   underline = "=" * len(title)
	   docs.append("%s\n%s\n%s" % (title, underline, doc))
   else:
	   docs.append("|No Docstring for %s|" % d)
   text = ("\n" + "-" * 79 + "\n").join(docs)
vim.command("let docWidth = %s" % len(title))
vim.command("let doc_lines = %s" % len(text.split("\n")))
EOF
	   "Scroll
	   function! s:popup_filter(winid, key)
		   if a:key ==# "\<c-k>"
			   call win_execute(a:winid, "normal! \<c-y>")
			   return v:true
		   elseif a:key ==# "\<c-j>"
			   call win_execute(a:winid, "normal! \<c-e>")
			   return v:true
		   elseif a:key ==# 'q' || a:key ==# 'x'
			   return popup_filter_menu(a:winid, 'x')
		   endif
		   return v:false
	   endfunction

	   let $FZF_DEFAULT_OPTS .= ' --border --margin=0,2'
	   let width = float2nr(&columns * 0.9)
	   let height = float2nr(&lines * 0.6)
	   let opts = { 'relative': 'editor',
				  \ 'row': (&lines - height) / 2,
				  \ 'col': (&columns - width) / 2,
				  \ 'width': width,
				  \ 'height': height }


	   let buf = nvim_create_buf(v:false, v:true)
	   let lines = py3eval('text')
	   call nvim_buf_set_lines(buf, 0, -1, v:true, split(lines, '\n'))
	   let winid = nvim_open_win(buf, v:true, opts)
	   " call setwinvar(winid, '&wincolor', 'Normal')
	    call setbufvar(winbufnr(winid), '&syntax','rst')
	   call setwinvar(winid, '&winhighlight', 'NormalFloat:Normal')
	endfunction
endif

Read more comments on GitHub >

github_iconTop Results From Across the Web

LSP: how to open documentation in a help buffer instead of ...
A feature I like in coc.nvim is the option to open documentation in a help buffer (or a preview window) instead of in...
Read more >
Lsp - Neovim docs
Read vim.diagnostic.config() to learn how to customize the display. ... Calling the function twice will jump into the floating window.
Read more >
Using Neovim floating window to break bad habits - The stuff I ...
My second requirement is to have a box drawn in the buffer to show the borders of the window. There are a lot...
Read more >
Setup nvim-lspconfig + nvim-cmp | Devlog
In nvim-lspconfig documentation you'll find instructions to install the language ... Show diagnostics in a floating window bufmap('n', 'gl', ...
Read more >
neoclide/coc.nvim - Gitter
@Annis_Monadjem_twitter Possible floating window hidden by other plugin. ... By disabling it in init.vim the original neovim K show the document.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found