43

Is it possible to format code automatically on save? I've seen a solution in which a macro assigned to a shortcut is proposed but I need to be able to make this a project setting. This way, anyone joining our project doesn't have to configure a macro for himself.

In Eclipse this was easily achieved using save actions and putting the config files under version control. Is this possible with Android Studio?

Community
  • 1
  • 1
Pin
  • 3,746
  • 4
  • 26
  • 33

6 Answers6

64

Using Flutter to auto format code when saving: tick - File|Settings|Language & Frameworks|Flutter - 'Format code on save'.

Sctajc
  • 947
  • 1
  • 8
  • 18
13

I don't know if this question is answered elsewhere or not, in Android Studio, I can find auto-format on save for Flutter in Preferences > Languages and Framework > Flutter as image here. Autoformat on save option for Flutter

Hung Nguyen
  • 131
  • 2
  • 4
12

Another option would be download the save actions plugin from jetbrains officials (as the search inside the plugin section in IDE doesn't find it)

Next, import the downloaded JAR file through IDE settings:

File > Settings > Plugins > Install plugins from disk

Restart the IDE and the Save Actions configuration will be shown on the root of IDE Settings

manasouza
  • 1,189
  • 1
  • 14
  • 26
8

It is not possible other than via a macro. There is an outstanding feature request -- IDEABKL-5806 Automatically trigger code formatter on file save -- for such an action. There a bit of discussion on the ticket you may want to read. I recommend you vote for it. It is however a 4 year old request. And based on comments by JetBrains, it does not appear like it is a high priority item. Additional votes may change that.

In the meantime, using the auto format on commit option would be an option for you.

Finally, if you want an easier way to set up the macros on other developer's systems, the macros are stored in the file .IntelliJIDEA/config/options/macros.xml (see Directories used by the IDE to store settings, caches, plugins and logs for more information on the location of the config directory). Keymaps are stored in .IntelliJIDEA/config/keymaps/{map-name}.xml You could copy those files over.

Pin
  • 3,746
  • 4
  • 26
  • 33
Javaru
  • 30,412
  • 11
  • 93
  • 70
4

In my recent Flutter installation I found this option here here

cwhisperer
  • 1,478
  • 1
  • 32
  • 63
0

There are differences based on the version of Android Studio. The latest version (Android Studio Flamingo 2022) does it like this: Assuming that you have already installed the Flutter plugin which includes Dart.

Open your project

File -> settings -> under Languages & Frameworks -> select Flutter or Dart -> 
Select format code on save

Also, can be done via:

Tools -> Actions on Save -> Reformat code 
salah-1
  • 1,299
  • 11
  • 15