Rust and TOML setup #1214
ia0
started this conversation in
Show and tell
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I'm sharing my experience setting up eglot with rust-analyzer (for Rust) and taplo (for TOML) in case it might help anyone trying to do the same (I wish I had this back then).
Minimal setup
This is straightforward from eglot documentation:
You'll need to install rust-analyzer and taplo if you don't already have them:
Subjective setup
Rust-analyzer may consume a non-negligible amount of RAM depending on the project. If you work on multiple projects, I suggest customizing
eglot-autoshutdown
tot
. This way, when closing the last buffer of a project, eglot will shutdown rust-analyzer, thus saving memory for other open projects where rust-analyzer is running.If you use company:
If you use flycheck, let me know if you find out how to use it. I had to fallback using flymake. I'm essentially using those keybindings:
I want
eglot-format
(in a TOML buffer) to match whattaplo format
does in the command line. I had to set the following up:I don't know how eglot would behave without the following project root definitions. So if you get issues because your project root is wrong, you may need this:
And here's my eglot keybindings:
Once you have a working setup, you might want to disable eglot logging (improving performance):
Experimental setup
Rust-analyzer doesn't provide an easy way to change your view of the code. By view I mean things like target, features, etc. To solve this issue, I'm computing the initialization options dynamically when eglot invokes rust-analyzer. Eglot provides a very convenient way to do that in comparison to
lsp-mode
:The initialization options are computed based on the project root:
The
:diagnostics
line is a bit extreme. You might not want it. I want it because I've had some false positives from rust-analyzer and don't trust it to produce correct diagnostics. So I'm only using diagnostics from the compiler. Note that even in this case, rust-analyzer introduces its own corruption to the compiler cache (see rust-lang/rust-analyzer#14722).You may also not want the
:invocationStrategy
line. I'm also not sure I need it, but prefer that setting overper_workspace
. I'm not sure what rust-analyzer documentation means by workspace, but I'm not using cargo workspaces (they create more problems than they solve) and have a one-to-one mapping between crates and rust-analyzer instance.The initialization options values come from a hash map indexed by project roots. This mimics the directory local idea from emacs, but making it work dynamically instead of statically in files.
And finally there must be a way to conveniently edit those settings. This is done with
ia0-eglot-edit-cargo
which I map toC-c c
. This shows you the current settings in the form of a cargo invocation command in the minibuffer. You can edit it and confirm, which would restart the rust-analyzer instance with the updated settings. This is experimental and will not return errors. This only supports:--features=foo
or--features=bar,baz
--no-default-features
--target=thumbv7em-none-eabi
or--target=wasm32-unknown-unknown
--all-targets
Beta Was this translation helpful? Give feedback.
All reactions