1

This is a followup to this question, where I would like to load the method definition in a pop-up rather than a split screen (or the whole screen for that matter).

Attempt 1: using nui.vim

I’m using nui.nvim for this, their pop-up method seems perfect for this purpose.

I have this in my lsp server key binding:


  for _, lsp in ipairs(servers) do
      lspconfig[lsp].setup {
          ..
          on_attach = function(client, bufnr)
            // jumps to method definition
            vim.keymap.set('n', 'gd', vim.lsp.buf.definition, bufopts) 
            // opens method definition in a split screen
            vim.keymap.set('n', 'gv', function() 
                    vim.cmd([[
                        vsplit
                        ]])
                        vim.lsp.buf.definition()
            end, bufopts)
 
            // loads ‘hello world’ in a pop-up window
            vim.keymap.set('n','gn', function()
              popup:mount()
              vim.api.nvim_buf_set_lines(popup.bufnr, 0, 1, false, { "Hello World" })
            end, bufopts)

            // loads an empty pop-up window
            vim.keymap.set('n','gx', function()
              popup:mount()
              vim.api.nvim_buf_set_lines(popup.bufnr, 0, 1, false, { vim.lsp.buf.definition() })
            end, bufopts)
          end
      }
  end

result: didn’t work.

Attemp 2: using on_list and vim.lsp.util.open_floating_preview

I was going over the docs from lsp-neovim, and I found these two interesting:

vim.lsp.util.open_floating_preview()
open_floating_preview({contents}, {syntax}, {opts})
    Shows contents in a floating window.

and

on_list receives a table with:
items table[], structured like setqflist-what
title string, title for the list.
context table|nil. ctx from lsp-handler

I tried this:


  local function on_list(options)
    vim.lsp.util.open_floating_preview(options)
  end


  for _, lsp in ipairs(servers) do
      lspconfig[lsp].setup {
          capabilities = capabilities,
          on_attach = on_attach,
          flags = lsp_flags,
          on_attach = function(client, bufnr)
            vim.keymap.set('n', 'gd', vim.lsp.buf.definition{on_list=on_list}, bufopts)

          end
      }
  end

result: didn’t work

0

Your Answer

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

Browse other questions tagged or ask your own question.