Skip to content

feat(event): add will rename node event. #1821

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

Merged
merged 1 commit into from
Dec 12, 2022

Conversation

antosha417
Copy link
Collaborator

Hey!
Some language servers support workspace/willRenameFiles request. It is used in order to automatically fix imports.
Currently neovim does not support it. So plugins must be used if one wants to use this feature. Here is an open issue in neovim repo.

Since I use nvim-tree.lua for all file manipulations I thought it would be cool to add WillRenameNode event and do workspace/willRenameFiles request in the handler.

Copy link
Member

@alex-courtis alex-courtis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This works.

Not quite sure what the use cases will be...

@alex-courtis alex-courtis merged commit a2c7556 into nvim-tree:master Dec 12, 2022
@antosha417
Copy link
Collaborator Author

@alex-courtis, so one of the possible use-cases is the following:

local api = require('nvim-tree.api')
local Event = api.events.Event

function _G.dump(...)
  local objects = vim.tbl_map(vim.inspect, { ... })
  print(unpack(objects))
  return ...
end

dump("Loaded will_rename_listener")

local function getWorkspaceEdit(client, data)
  local will_rename_params = {
    files = {
      {
        newUri = "file://" .. data.new_name,
        oldUri = "file://" .. data.old_name
      }
    }
  }
  local resp = client.request_sync("workspace/willRenameFiles", will_rename_params, 1000)
  return resp.result
end

api.events.subscribe(Event.WillRenameNode, function(data)
  dump("Got will_rename_event")
  dump(data)
  for _, client in pairs(vim.lsp.get_active_clients()) do
    if client.name == "metals" then
      local edit = getWorkspaceEdit(client, data)
      dump("Got workspace edit")
      dump(edit)
      vim.lsp.util.apply_workspace_edit(edit, client.offset_encoding)
    end
  end
end)

Now when I move files around in nvim-tree.lua my imports and package names are automatically fixed my metals language server for scala.

I think that rust language server also supports workspace/willRenameFiles request. Probably typescript laguage server also. I'll give them a try soon and update.

@gegoune
Copy link
Collaborator

gegoune commented Dec 12, 2022

@antosha417 Could you maybe add that to wiki? Seems like pretty significant functionality that users might want to configure. This is pretty neat!

@antosha417
Copy link
Collaborator Author

antosha417 commented Dec 12, 2022

@gegoune I'll explore this a bit more with couple of other language servers and add this to wiki. I'm also very excited about it.

@alex-courtis
Copy link
Member

Oh that is nice... I can see instances where that would be useful.

Wiki: yes please!

@antosha417
Copy link
Collaborator Author

@gegoune, @alex-courtis
I ended up wring a small extension plugin for lsp file operations.
https://github.com/antosha417/nvim-lsp-file-operations

I think there might be some corner cases which I don't know about. So I thought that wiki is not a great place to maintain that peace of code.

Will write about that extension in the wiki though.

@antosha417
Copy link
Collaborator Author

https://github.com/nvim-tree/nvim-tree.lua/wiki/Recipes#lsp-file-operations

@gegoune
Copy link
Collaborator

gegoune commented Jan 9, 2023

Perhaps it deserves its own Extensions page?

@alex-courtis
Copy link
Member

Perhaps it deserves its own Extensions page?

Done: https://github.com/nvim-tree/nvim-tree.lua/wiki/Extensions

I shall enjoy using lsp-file-operations. I hope it is the first of many...

@alex-courtis
Copy link
Member

lsp-file-operations runs fine when its setup is called before nvim-tree's. Proves that the event system is working well :)

@alex-courtis
Copy link
Member

Renamed wiki to https://github.com/nvim-tree/nvim-tree.lua/wiki/Extension-Plugins and added mini.base16

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.

3 participants