2

I'm using a LSP plugin to call a command from the server, and currently it is logging the data using echom.

Response

My current code is like this:

function! PtypeCallback(result)
    echom json_encode(a:result)
endfunction

It was working, but for functions with larger types, The JSON string is truncated. Also I want to keep it for some time, preferably launching it in a preview window.

How can I open the preview-window using a custom created buffer which contains the typ parameter from the JSON I'm receiving in a:result parameter?

5
  • 1
    Take a look at scratch.vim plugin github.com/mtth/scratch.vim Or check :he special-buffers
    – Alex Kroll
    Mar 1, 2019 at 10:30
  • 2
    Have you had a look at the documentation, check :h pedit to open the preview window with some custom file name. Mar 1, 2019 at 10:42
  • @AlexKroll I want the preview window to be non-editable. This command is only useful to see what is the type of a function, and I get the contents from a parameter in a callback function. I've seen the help file for special-buffers, which is listing quickfix but not preview. This is supposed to contain documentation also if it is available, so I don't think a location list is useful. Mar 1, 2019 at 10:57
  • @ChristianBrabandt Unfortunately I don't have a file. The response comes by means of a JSON RPC from the LSP plugin for me. I don't want to write it to a file only to load it into a buffer. Mar 1, 2019 at 10:58
  • 3
    well, I don't see a way than to create a tempfile and display it in the preview window. (Note something like this might even work: :exe 'pedit +call\ setline(1,text)' tempname()) Mar 1, 2019 at 12:32

1 Answer 1

2

After seven hours of fiddling with the documentation and searching, I was able to write a function that takes a filetype, an array of strings and show a dummy buffer based on that.

function! showInPreview(name, fileType, lines)
    let l:command = "silent! pedit! +setlocal\\ " .
                  \ "buftype=nofile\\ nobuflisted\\ " .
                  \ "noswapfile\\ nonumber\\ " .
                  \ "filetype=" . a:fileType . " " . a:name

    exe l:command

    if has('nvim')
        let l:bufNr = bufnr(a:name)
        call nvim_buf_set_lines(l:bufNr, 0, -1, 0, a:lines)
    else
        call setbufline(a:name, 1, a:lines)
    endif
endfunction

I wrote this function based on the rust plugin code I found in the LanguageClient_neovim plugin. Thanks @D._Ben_Knoble for suggesting the vim/nvim check in the function.

Hope this helps anyone looking for this answer. I can now call this function from my callback function with the correct lines and I'm ensured that it works for me.

2
  • 1
    I believe you can check with call to make with if has('nvim')
    – D. Ben Knoble
    Mar 1, 2019 at 22:23
  • 1
    @D.BenKnoble Thanks, tried and it was working under VIM too. Mar 2, 2019 at 4:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.