Execute rustfmt on file save in IntelliJ or CLion
Asked Answered
D

2

9

How do I run rustfmt automatically when I save a file in IntelliJ or CLion?

Draggle answered 28/12, 2019 at 15:27 Comment(0)
F
22

It should be noted that while bit2pixel's answer will work, (and indeed is how I did it for a good long while), there's a newer (better?) way. The rust-plugin has an option to handle this automatically.

enter image description here

Frill answered 17/3, 2020 at 18:37 Comment(0)
D
4
  1. Make sure you have File Watchers plugin via Preferences -> Plugins -> Installed. If it's not installed, go to the Marketplace tab and search for File Watchers, install it, and restart your IDE.

  2. Go to Preferences -> Tools -> File Watchers.

    File Watchers

  3. Add a new watcher and name it rustfmt.

    File Watchers

  4. Configure the watcher by following the screenshot below. Enter the path to rustfmt into the Program field; it can be found in your Cargo bin directory.

If you don't know where Cargo bin directory directory is, type which rustfmt in your terminal and copy the path. If you still can't find it, make sure you followed the instructions in Installing Rust.

enter image description here

Draggle answered 28/12, 2019 at 15:27 Comment(1)
You can add $FILE_PATH$:$LINE$:$COLUMN$ to "Output filters" to get line highlighting too. Can optionally set "show console" to never if you only want to see line highlighting on errors and not the console output. Note that you can also run rustfmt using a setting in the rust plugin now, but it's been buggy lately so I've been using this instead.Pycnometer

© 2022 - 2024 — McMap. All rights reserved.