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

Add initial support for LSP DidChangeWatchedFiles #7665

Merged
merged 9 commits into from
Jul 21, 2023

Conversation

ryanfowler
Copy link
Contributor

Context: #1125 (comment)

This PR implements initial support for LSP DidChangeWatchedFiles by forwarding a notification for all file writes or reloads to any LSP that has registered with a matching glob. This work will enable LSPs to respond accordingly to changes made to any metadata files (e.g. Cargo.toml) that they have registered for notifications on. For now it can only forward notifications for changes that occur within Helix, however this code can continue to be used when a proper file watcher is added at some point in the future.

For the implementation, a new file event Handler struct spawns a dedicated tokio task to listen for any registrations/unregistrations by LSPs as well as any file change events. Since the Editor struct owns the helix-lsp::Registry instance, I saw a few potential designs for the Handler to be able to access LSP clients. Ultimately, I chose to pass the Handler a Weak reference to the LSP clients as they register for file watching. This is done so that the Handler can have direct access to the client without preventing the client from being dropped if it is closed and isn't properly notified. Alternatives could include synchronizing references to clients between the Editor and Handler, synchronizing access to the LSP clients, or notifying the Editor of file change notifications that need to be sent to LSPs via a new channel. My opinion is that all of those alternatives seem more complex or bug-prone.

There are certainly optimizations that can be made (e.g. batching file changes) but I think those can be added in future PRs, as necessary.

I've tested these changes successfully with the rust-analyzer and gopls LSPs on both macos and Linux. Changes to the Cargo.toml of a crate are immediately recognized by rust-analyzer, instead of having to restart the LSP.

Thanks!

Copy link
Member

@pascalkuthe pascalkuthe left a comment

Choose a reason for hiding this comment

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

This looks really promising, thanks for taking a stab at this! I will do a more thorough review later. One thing that I noticed immidietly us that I would prefer the filer handlr to be moved to helix-lsp. You can store the handle in the registry (that's stored on the editor so you can still keep most of the rest the same)

@pascalkuthe
Copy link
Member

Also :reload and :reload-all should send events too, they don't seem to at the moment.

@pascalkuthe pascalkuthe added A-language-server Area: Language server client E-medium Call for participation: Experience needed to fix: Medium / intermediate S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. S-waiting-on-review Status: Awaiting review from a maintainer. labels Jul 18, 2023
@pascalkuthe pascalkuthe added this to the next milestone Jul 18, 2023
@ryanfowler
Copy link
Contributor Author

Thanks for taking a look! I've moved the file event Handler to helix-lsp, as you've suggested.

Also :reload and :reload-all should send events too, they don't seem to at the moment.

Those should already be in place for :reload here and :reload-all here.

Copy link
Member

@pascalkuthe pascalkuthe left a comment

Choose a reason for hiding this comment

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

some minor technical commints the rest looks good

helix-view/src/file_event.rs Outdated Show resolved Hide resolved
helix-view/src/file_event.rs Outdated Show resolved Hide resolved
@pascalkuthe
Copy link
Member

pascalkuthe commented Jul 18, 2023

Thanks for taking a look! I've moved the file event Handler to helix-lsp, as you've suggested.

Also :reload and :reload-all should send events too, they don't seem to at the moment.

Those should already be in place for :reload here and :reload-all here.

ah sorry about that I somehow missed that

pascalkuthe
pascalkuthe previously approved these changes Jul 18, 2023
@pascalkuthe pascalkuthe removed the S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. label Jul 18, 2023
Copy link
Member

@the-mikedavis the-mikedavis left a comment

Choose a reason for hiding this comment

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

I have a few style nits and a question but overall I think this looks good, I like the idea 👍

helix-lsp/src/lib.rs Outdated Show resolved Hide resolved
helix-lsp/src/file_event.rs Outdated Show resolved Hide resolved
helix-view/src/editor.rs Show resolved Hide resolved
helix-view/Cargo.toml Outdated Show resolved Hide resolved
@ryanfowler
Copy link
Contributor Author

@the-mikedavis thanks for the review! I think I've addressed all feedback, but let me know if there's anything else you'd like me to change 🙂

@pascalkuthe pascalkuthe merged commit 5c41f22 into helix-editor:master Jul 21, 2023
6 checks passed
@ryanfowler ryanfowler deleted the lsp-didchangewatchedfiles branch July 21, 2023 22:36
mtoohey31 pushed a commit to mtoohey31/helix that referenced this pull request Jun 2, 2024
* Add initial support for LSP DidChangeWatchedFiles

* Move file event Handler to helix-lsp

* Simplify file event handling

* Refactor file event handling

* Block on future within LSP file event handler

* Fully qualify uses of the file_event::Handler type

* Rename ops field to options

* Revert newline removal from helix-view/Cargo.toml

* Ensure file event Handler is cleaned up when lsp client is shutdown
smortime pushed a commit to smortime/helix that referenced this pull request Jul 10, 2024
* Add initial support for LSP DidChangeWatchedFiles

* Move file event Handler to helix-lsp

* Simplify file event handling

* Refactor file event handling

* Block on future within LSP file event handler

* Fully qualify uses of the file_event::Handler type

* Rename ops field to options

* Revert newline removal from helix-view/Cargo.toml

* Ensure file event Handler is cleaned up when lsp client is shutdown
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-language-server Area: Language server client E-medium Call for participation: Experience needed to fix: Medium / intermediate S-waiting-on-review Status: Awaiting review from a maintainer.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants