SmiteshP / nvim-navbuddy

A simple popup display that provides breadcrumbs feature using LSP server
Apache License 2.0
770 stars 30 forks source link

Attaching navbuddy to LSP #45

Closed akmalsoliev closed 1 year ago

akmalsoliev commented 1 year ago

Hello,

First of all thank you for the plugin, this is something I was looking for as it provides an excellent class and method file exploration.

I am struggling on attaching navbuddy to the LSP as whenever the file is sourced or :lua require("nvim-navbuddy").open() the following error is given:

E5108: Error executing lua ...ck/packer/start/nvim-navbuddy/lua/nvim-navbuddy/init.lua:203: attempt to get length of a nil value
stack traceback:
        ...ck/packer/start/nvim-navbuddy/lua/nvim-navbuddy/init.lua:203: in function 'request'
        ...ck/packer/start/nvim-navbuddy/lua/nvim-navbuddy/init.lua:267: in function 'open'
        [string ":lua"]:1: in main chunk

After some digging I came to realisation that this indicates that navbuddy is not attached to the LSP.

Attempts (no success):

  1. Created navbuddy.lua file with code provided in README.md
    
    local navbuddy = require("nvim-navbuddy")

require("lspconfig").clangd.setup { on_attach = function(client, bufnr) navbuddy.attach(client, bufnr) end }


2. Attempted to insert navbuddy into `lsp.lua` config file

Following file: https://github.com/akmalsoliev/nvim/blob/main/after/plugin/lsp.lua

Line 38:
```lua
lsp.on_attach(function(client, bufnr)
  local navbuddy = require("nvim-navbuddy")
  navbuddy.attach(client, bufnr)

  local opts = {buffer = bufnr, remap = false}

  vim.keymap.set("n", "gd", function() vim.lsp.buf.definition() end, opts)
  vim.keymap.set("n", "K", function() vim.lsp.buf.hover() end, opts)
  vim.keymap.set("n", "<leader>vws", function() vim.lsp.buf.workspace_symbol() end, opts)
  vim.keymap.set("n", "<leader>vd", function() vim.diagnostic.open_float() end, opts)
  vim.keymap.set("n", "[d", function() vim.diagnostic.goto_next() end, opts)
  vim.keymap.set("n", "]d", function() vim.diagnostic.goto_prev() end, opts)
  vim.keymap.set("n", "<leader>vca", function() vim.lsp.buf.code_action() end, opts)
  vim.keymap.set("n", "<leader>vrr", function() vim.lsp.buf.references() end, opts)
  vim.keymap.set("n", "<leader>vrn", function() vim.lsp.buf.rename() end, opts)
  vim.keymap.set("i", "<C-h>", function() vim.lsp.buf.signature_help() end, opts)
end)

Neither of these changes had any effects, so I would like to ask for your guidance on what would be the way to attach navbuddy to LSP? @SmiteshP

Thanks

akmalsoliev commented 1 year ago

SOLVED:

If you're using lsp-zero as I am, then you would need to add the following code to either your init.lua or after/plugin/navbuddy.lua:

local lsp = require('lsp-zero')
local navbuddy = require("nvim-navbuddy")

lsp.preset().on_attach(
    function(client, bufnr)
        navbuddy.attach(client, bufnr)
    end
)
lsp.setup()