m-demare / hlargs.nvim

Highlight arguments' definitions and usages, using Treesitter
GNU General Public License v3.0
472 stars 10 forks source link

Allow disabling on specific buffers #69

Closed perrin4869 closed 1 year ago

perrin4869 commented 1 year ago

Well, this is not a bug, but more of a feature request. I'd like to setup neovim to use lsp semantic tokens by default, fallback to hlargs when unavailable. Using the disable option is not a complete solution, because if the lsp client is not yet attached to the buffer being disabled, we cannot check the buffer's capabilities for semanticTokenProvider when first opening that buffer up until the point when the language server finishes initializing.

hlargs.setup {
  disable = function(_, bufnr)
    -- when opening a buffer that hasn't attached an lsp client to it, this check will fail
    local clients = vim.lsp.get_active_clients { bufnr = bufnr }
    for _, c in pairs(clients) do
      local caps = c.server_capabilities
      if caps.semanticTokensProvider and caps.semanticTokensProvider.full then
        return true
      end
    end
  end
}

A more reliable solution would be something like:

vim.api.nvim_create_augroup("LspAttach_hlargs", {})
vim.api.nvim_create_autocmd("LspAttach", {
  group = "LspAttach_hlargs",
  callback = function(args)
    if not (args.data and args.data.client_id) then
      return
    end

    local bufnr = args.buf
    local client = vim.lsp.get_client_by_id(args.data.client_id)
    local caps = client.server_capabilities
    if caps.semanticTokensProvider and caps.semanticTokensProvider.full then
      -- TODO: disable only on the buffer that was attached
      require("hlargs").disable()
    end
  end,
})

However, without the ability to specify which buffer to disable, require("hlargs").disable() will disable hlargs across the board. Would it be possible to add an option to the disable function so it can be called as require("hlargs").disable { bufnr = bufnr }?

m-demare commented 1 year ago

I've added disable_buf and enable_buf (see :h hlargs-disable-buf) Let me know if you find any issues

Also, may I add your code snippets for LSP integration to the docs?

perrin4869 commented 1 year ago

thank you so much!! updated my config here https://github.com/perrin4869/dotfiles/commit/8e460fcffffe35fb4ff56cfb45fcdcbab21a7566 😄 Please feel free to use the code-snippets!