SmiteshP / nvim-navbuddy

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

Feature Request - Support better lazy loading #66

Closed weilbith closed 1 year ago

weilbith commented 1 year ago

Hey 👋🏾

Thanks for this pretty plugin. I'm just setting it up for me and start to customize it. One thing I stumbled over was an issue with properly lazy loading the plugin. I use the command Navbuddy to load to this plugin and its configuration. So it only loads when I need it. This also means I do not use a manual autocommand to attach navbuddy to the LSP clients, but use the configuration for auto-attachment. BUT, this only works for newly started clients, as is uses the LspAttach event. That then leads to issues because Navbuddy is not aware of the already existing clients. So I wrote myself a little utility function which runs after the setup. It does the job and everything works nicely. It looks like the following:

--- Fix the issue that NavBuddy can attach automatically to new clients, but
--- does not for already started ones. To have better lazy loading support, this
--- retroactively attaches NavBuddy to all already existing clients and their
--- buffers.
local function attach_to_existing_lsp_clients()
  local all_clients = vim.lsp.get_active_clients()

  local supported_clients = vim.tbl_filter(function(client)
    return client.server_capabilities.documentSymbolProvider
  end, all_clients)

  for _, client in ipairs(supported_clients) do
    local buffers_of_client = vim.lsp.get_buffers_by_client_id(client.id)

    for _, buffer_number in ipairs(buffers_of_client) do
      navbuddy.attach(client, buffer_number)
    end
  end
end

So I thought maybe it would be nice to add this to the plugin itself? It would make the plugin more robust in terms of the load order, lazyness etc. So if the user has enable the option to automatically attach, the setup function will not only register the auto-command, but also does something similar to my function above. What do you think? Should I open a PR?

SmiteshP commented 1 year ago

Yep! This sounds good. You can open a PR