Skip to content

[Feature request] Update imports for the file after file is renamed #623

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

Closed
sidlatau opened this issue Nov 25, 2022 · 7 comments
Closed
Labels
enhancement New feature or request

Comments

@sidlatau
Copy link
Contributor

Problem:

I am using this plugin for file rename/move operations. I would want to integrate these operations with LSP. Some LSP servers support functionality to update imports after the file is renamed or moved. Client sends workspace/willRenameFiles notification to LSP, which returns import changes. dartls supports this notification. I want a hook to file rename flow to be able to call LSP and apply returned results.

Possible implementation:

It would be nice to be able to setup this hook in neo-tree, possible implementation:
https://github.com/nvim-neo-tree/neo-tree.nvim/compare/v2.x...sidlatau:neo-tree.nvim:update-imports?expand=1

Then user needs to setup this hook:

neo_tree.setup {
    ...
    hooks = {
      on_rename_file = function(data)
        ("dart-lsp-refactorings").on_rename_file(data)
      end,
    },
}

where data is:

{
  source= "/path_to_source",
  destination = "/path_to_destination",
  callback = function()
    -- Function called to finish rename process
  end
}

Alternatives tried:

I tried to use events infrastructure in neo-tree, added new WILL_FILE_RENAME event, but problem with them is that they does not block a flow. When request reaches LSP about renamed file, server returns empty results, I suspect file is already renamed and server does not see it. In above provided solution I can control when actual rename occurs.

I think several such hooks could be useful - for file rename, file move, maybe even for operations with folders.

Would it be feasible to add such hooks to neo-tree?
I may create PR then.

@cseickel
Copy link
Contributor

I have been using the technique linked to in this comment: #308 (comment)

It works great for me. I'm sure you can adjust that for other languages.

@cseickel cseickel reopened this Nov 25, 2022
@cseickel
Copy link
Contributor

Sorry, wrong button. I didn't mean to close it!

@sidlatau
Copy link
Contributor Author

Nice solution, but I think that only works for typescript. _typescript.applyRenameFile is not a standard LSP command, I am unable to find an exact replacement in LSP specs.
I am getting an error when trying this:

ERROR dartls: -32005: workspace/willRenameFiles is not a valid command identifier 

Here is my implementation that works with dartls: https://github.com/sidlatau/dart-lsp-refactorings.nvim/blob/main/lua/dart-lsp-refactorings/rename-file.lua

But that needs changes in neo-tree codebase. I need to call LSP, apply changes, and only then rename/move the file.

@cseickel
Copy link
Contributor

OK, I see. If you wanted to add the new event I would accept that as a PR. I think this would be the callback point:

loop.fs_rename(path, destination, function(err)

@cseickel cseickel added the enhancement New feature or request label Nov 26, 2022
@sidlatau
Copy link
Contributor Author

Not sure I understand what "callback point" means - is it an event with a callback?
Either way, I created PR #625 as a starting point.

@mehalter
Copy link
Contributor

It would also probably be a good idea to add similar events for BEFORE_FILE_ADD and BEFORE_FILE_DELETE as well since are similar LSP file operations like willRenameFiles there are willDeleteFiles and willCreateFiles that would be nice to be able to support with neo-tree events :)

Let me know if you think this should be opened as a new issue for a feature request or if here is more succinct rather than opening new issues.

Thanks for the initial BEFORE_FILE_MOVE and BEFORE_FILE_RENAME support @sidlatau !

@cseickel
Copy link
Contributor

@mehalter it definitely should be a new issue to add the new events.

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

No branches or pull requests

3 participants