1

Is it possible to run rust analyzer to not just display but outright inject variable types on variable declarations?

Say in this case

enter image description here

The linter is displaying the type, I would like it to inject the characters instead of just showing them.

Makogan
  • 8,208
  • 7
  • 44
  • 112
  • I don't think there's something you can do. Rust is designed with inference in mind. – Chayim Friedman Aug 23 '22 at 04:25
  • Rust analyzer is displaying the characters right there, why can;t I run it from the terminalk and get it to inject those characters instead of them just showing as phantoms? – Makogan Aug 23 '22 at 04:30
  • Because nobody has implemented that. rust-analyzer has an (unstable) API, so I think you can implement that yourself. Look at how it implements the `analysis-stats` command. – Chayim Friedman Aug 23 '22 at 04:32

1 Answers1

0

It can't, because it's out of scope. Adding the type would mean changing the file, and that's not rust-analyzer's job. rust-analyzer extracts the necessary type information, and whatever editor/IDE you use has to provide the feature of actually adding it.

Here's an example of me using Helix's source-actions menu (Space+a) to add the type:
enter image description here

(Conceptually, rather than rust-analyzer, it'd be cargo clippy --fix that could be used for changing files, but I haven't found a lint like that and suspect it's not implemented.)

Caesar
  • 6,733
  • 4
  • 38
  • 44
  • Hmm... Not exactly. rust-analyzer is also supposed to be a general-purpose library to analyzing Rust code. – Chayim Friedman Aug 23 '22 at 05:31
  • Doesn't helix's 'source-actions' rely on rust-analyzer via LSP? – Holloway Aug 23 '22 at 10:10
  • @Holloway Exactly. The point I wanted to make is that rust-analyzer is providing the type information, but to actually take that information and write it to the file, you'd usually have to go through LSP and an editor that allows to do the actual modification. – Caesar Aug 23 '22 at 10:41