0

I have an auto command highlight words on cursor hold and this autocmd gets attached to buffer when the LSP server has the capability of documentFormattingProvider.

After updating neovim to 0.81 and all the plugins and language servers. The behaviour of this autocmd is buggy.

  • on svelte files, it does not work and actually outputs error: method textDocument/documentHighlight is not supported by any of the servers registered for the current buffer (lsp:svelte)
  • on python, no highlighting, no errors, but running lua vim.lsp.buf.document_highlight() on cmd line while cursor on repetitive word, it does highlight (lsp:pyright,black)
  • on go, it is working! (lsp:gopls)
  • on typescript files, it is working (lsp:tsserver)
  • on dart it is working (lsp:dartls)

Here is my dotfiles


local function lsp_highlight_document(client)
    -- Set autocommands conditional on server_capabilities
    if client.server_capabilities.documentFormattingProvider then
        vim.api.nvim_exec(
            [[
            augroup lsp_document_highlight
              autocmd! * <buffer>
              autocmd CursorHold <buffer> lua vim.lsp.buf.document_highlight()
              autocmd CursorMoved <buffer> lua vim.lsp.buf.clear_references()
            augroup END
          ]],
            false
        )
        end
end

I also tried (also not working):

local function lsp_highlight_document(client)
    -- Set autocommands conditional on server_capabilities
    if client.server_capabilities.documentFormattingProvider then
        vim.api.nvim_create_augroup("lsp_document_highlight", { clear = true })
        vim.api.nvim_clear_autocmds({ buffer = bufnr, group = "lsp_document_highlight" })
        vim.api.nvim_create_autocmd("CursorHold", {
            callback = vim.lsp.buf.document_highlight,
            buffer = bufnr,
            group = "lsp_document_highlight",
            desc = "Document Highlight",
        })
        vim.api.nvim_create_autocmd("CursorMoved", {
            callback = vim.lsp.buf.clear_references,
            buffer = bufnr,
            group = "lsp_document_highlight",
            desc = "Clear All the References",
        })
    end
end

Both of these functions gets called here

options.on_attach = function(client, bufnr)
    if client.name == "tsserver" then
        client.server_capabilities.documentFormattingProvider = true
    end

    if client.name == "sumneko_lua" then
        client.server_capabilities.documentFormattingProvider = false
    end

    lsp_highlight_document(client)
    lsp_keymaps(bufnr)
end

local capabilities = vim.lsp.protocol.make_client_capabilities()

local status_ok, cmp_nvim_lsp = pcall(require, "cmp_nvim_lsp")
if not status_ok then
    return
end

options.capabilities = cmp_nvim_lsp.default_capabilities(capabilities)

return options

Prior to this update, it was working fine in every language. Not sure what is missing.

Sami Al-Subhi
  • 4,406
  • 9
  • 39
  • 66

1 Answers1

0

Here is my config, which works for pyright, pylsp, lua-language-server etc.:

local api = vim.api
local lsp = vim.lsp

if client.server_capabilities.documentHighlightProvider then
  vim.cmd([[
    hi! link LspReferenceRead Visual
    hi! link LspReferenceText Visual
    hi! link LspReferenceWrite Visual
  ]])

  local gid = api.nvim_create_augroup("lsp_document_highlight", { clear = true })
  api.nvim_create_autocmd("CursorHold" , {
    group = gid,
    buffer = bufnr,
    callback = function ()
      lsp.buf.document_highlight()
    end
  })

  api.nvim_create_autocmd("CursorMoved" , {
    group = gid,
    buffer = bufnr,
    callback = function ()
      lsp.buf.clear_references()
    end
  })
end

Works well on nvim 0.8.1. Try it to see if it works. If it does not work, I think there is something wrong with your config, you need to dig deeper to find why.

jdhao
  • 24,001
  • 18
  • 134
  • 273