Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds support for workspace/willRenameFiles #33

Merged
merged 4 commits into from
Oct 6, 2023

Conversation

bugabinga
Copy link
Contributor

@bugabinga bugabinga commented Sep 30, 2023

This commit calls a callback in the nvim-lsp-file-operations plugin,
if available.
antosha417/nvim-lsp-file-operations#13
The affected file operations are "rename" and "move-rename".

This commit calls a callback in the `nvim-lsp-file-operations` plugin,
if available.
<antosha417/nvim-lsp-file-operations#13>
The affacted file operations are "rename" and "move-rename".
@bugabinga
Copy link
Contributor Author

I have tested this only with rust-analyzer!

@chrisgrieser
Copy link
Owner

great, thanks! Tested it with tsserver (works), and lua_ls (not supported).

Could you do the following to make this properly ready for merging:

  • ensure check passes (formatting)
  • use camelCase variable names
  • add to the readme that with nvim-lsp-file-operations, you can get workspace-wide renaming (for LSP servers with willRename), and also the required installation/setup for that
  • indicate somehow to the user that such an LSP-file-renaming has occurred. Right now, there is no indication for that, so that to the user, rename will look the same in lua and typescript, even though different things are happening. It should be more transparent to the user, if something different is happening

@bugabinga
Copy link
Contributor Author

indicate somehow to the user

would vim.notify be appropriate?
I wonder if this should not be part of nvim-lsp-file-operations though.

@chrisgrieser
Copy link
Owner

either that, or maybe change the prompt text of vim.ui.input. Would probably be something like checking whether an attached LSP has willRename, and if so, change the prompt?

@chrisgrieser
Copy link
Owner

I just stumbled upon this on reddit: it appears, it may be LSP-renaming even without lsp-file-operations as a dependency https://www.reddit.com/r/neovim/comments/16x109j/comment/k3057lh/?utm_source=share&utm_medium=web2x&context=3

@bugabinga
Copy link
Contributor Author

Oh I like that. That is way simpler than to depend on a plugin.

In that case I think there are two approaches:

  1. Simply bake the function into genghis: https://github.com/LazyVim/LazyVim/blob/fecc5faca25c209ed62e3658dd63731e26c0c643/lua/lazyvim/util/init.lua#L304
  2. Or, provide a callback for file operations, so that humans can implement this themselves.
--stolen from https://github.com/LazyVim/LazyVim/blob/fecc5faca25c209ed62e3658dd63731e26c0c643/lua/lazyvim/util/init.lua#L304
local function on_rename(from, to)
  local clients = vim.lsp.get_active_clients()
  for _, client in ipairs(clients) do
    if client:supports_method("workspace/willRenameFiles") then
      local resp = client.request_sync("workspace/willRenameFiles", {
        files = {
          {
            oldUri = vim.uri_from_fname(from),
            newUri = vim.uri_from_fname(to),
          },
        },
      }, 1000)
      if resp and resp.result ~= nil then
        vim.lsp.util.apply_workspace_edit(resp.result, client.offset_encoding)
      end
    end
  end
end

-- pseudo genghis config
local khan = require'genghis'
khan.setup{
  ...
  post_operation = function(file_operation, ...)
    if file_operation == 'rename' or file_operation 'move-rename' then
      on_rename(...)
    end
  end,
  ...
}

@chrisgrieser
Copy link
Owner

yeah, I think baking it into genghis makes much more sense. No need to burden the user with the need to add boilerplate to their config.

@chrisgrieser
Copy link
Owner

thx! Could you add some information to the readme on the change? And maybe also post a notification for the user when the LSP-rename is due to an attached LSP supporting it? Then I think we can merge this

@bugabinga bugabinga changed the title Adds support for nvim-lsp-file-operations Adds support for workspace/willRenameFiles Oct 6, 2023
@bugabinga
Copy link
Contributor Author

Not sure about the wording inside the prompts, but those are easy to change anytime.

@chrisgrieser
Copy link
Owner

thanks, looks good to me now! Bummer that many LSPs like lua_ls do not support willRename though

@chrisgrieser chrisgrieser merged commit ec447d0 into chrisgrieser:main Oct 6, 2023
1 check passed
@chrisgrieser
Copy link
Owner

hmm, so there were some issues with the renaming-capability-detectin. I fixed them, but just so you are aware of it:

---@nodiscard
---@return boolean
local function lspSupportsRenaming()
-- INFO `client:supports_method()` seems to always return true, whatever is
-- supplied as argument. This does not affect `onRename`, but here we need to
-- check for the server_capabilities to properly identify whether our LSP
-- supports renaming or not.
-- TODO investigate if `client:supports_method()` works in nvim 0.10 or later
local clients = vim.lsp.get_active_clients { bufnr = 0 }
for _, client in ipairs(clients) do
local workspaceCap = client.server_capabilities.workspace
local supports = workspaceCap and workspaceCap.fileOperations and workspaceCap.fileOperations.willRename
if supports then return true end
end
return false
end

(also, vim.iter is not supported on stable yet, so I changed that as well)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants