Support for workspace/didChangeWatchedFiles?

It looks like Neovim does not support the workspace/didChangeWatchedFiles notifications, and I’m curious why not. I often work in a Java project that contains a lot of resource SQL files, and without support for didChangeWatchedFiles, the language server does not pick up changes to resource files, so the whole language server has to be restarted instead.

It looks like Neovim does not support the workspace/didChangeWatchedFiles notifications, and I’m curious why not.

Because no one has filed a PR yet :slight_smile:

1 Like

Fair enough. :smile:

This is actually a bit challenging. CoC uses watchman, implementing an interface from watchman from scratch would probably entail a lot of code. I’d probably want to wait until [RDY] fswatch autoread by BK1603 · Pull Request #12593 · neovim/neovim · GitHub is merged and use that.

1 Like

I have been experimenting with setting up an autocmd (like at BufNew) to send the DidChangeWatchedFiles message when I create a new file. But I still need to work out the right order (I think I need to wait until the first save before sending the message).

When it works this is only part of the solution, because it will not detect changes to files that are not actively open.

I actually did a similar workaround with vim-lsp, and for my use-case at least is all I really need. Does sending a notification to the server work if the capability isn’t registered?

Turns out I also need a didOpen notification: after the autocmd has run I need to do :edit to make the server send diagnostics.

I primarily needed to handle the case of resource files being updated, which I got working with this workaround in my vimrc:

if has('nvim-0.5')
  lua <<EOF
  function notify_java_resource_update(buffer, change)
    local relpath = vim.fn.expand('#'..buffer)
    if not string.find(relpath, "/resources/") then
      return
    end
    local filepath = vim.fn.expand('#'..buffer..':p')

    for _,client in pairs(vim.lsp.get_active_clients()) do
      if client.name == "jdtls" then
        local result = client.notify('workspace/didChangeWatchedFiles', {
          changes = {{ uri = 'file://'..filepath, type = change }},
        })
        if not result then
          error('Failed to notify jdtls of resource update')
        end
      end
    end
  end
EOF
endif

function! s:notify_java_resource_update(buffer, change)
  if has('nvim-0.5')
    call luaeval('notify_java_resource_update(_A[1], _A[2])', [a:buffer, a:change])
  else
    " Currently not supported
  endif
endfunction

augroup java_resource_updates
  au!
  au BufWritePost,FileWritePost * call s:notify_java_resource_update(expand('<abuf>'), 2)
  " TODO: add new/deleted files
augroup END