Yesterday, Jos茅 Valim announced the release of Elixir 1.6 with a new mix task called _code formatter_. Running mix format task automatically formats the code into a unique and consistent layout.
Is it possible to make use of the mix format task via the Reformat Code command in IntelliJ?
Thanks and best,
Tom
Ideally, I don't want to use IDE's File Watcher for this, as I had many issues with it in the past (conflicts with auto save, safe save etc.). Formatting with mix format would be useful instead of plugin's built-in formatter since many upstream projects would now probably require code to be formatted exactly like how mix format would do it -- replicating that same formatting using this plugin's built-in format may be difficult.
As work-arounds until the feature is completed, you should be able to invoke mix format in the following ways:
!.@KronicDeth
Thanks, I choose 1. for me as a temporary workaround.
For reference if anyone is interested in that as well:
mix format), Program pathToMixBinary, Arguments (format $FilePathRelativeToProjectRoot$), Working directory ($ProjectFileDir$), [x] "Synchronize files after execution".@tschmidleithner Hmm, this doesn't seem to work well for me. I get compilation errors that I don't get when running the same command from the CLI. Maybe it's because I use asdf, not sure. Guess I'll have to wait for this to land in the plugin and just run mix format before I commit.
@wizonesolutions I am not familiar with asdf but it could be that your pathToMixBinary differs from the one you are using with your CLI directly. Maybe you can fix that for asdf as well?
asdf is different from most of the other package managers in that it requires more paths to be setup or additional environment variables to work. Supporting asdf to supply the SDKs is still an open issue
Workaround: I made a local script, bin/format containing the single line:
PATH=$ASDF_DIR/shims:$ASDF_DIR/bin:$PATH mix format $*
In External Tools > Program I entered bin/format and it works.
Otherwise, I followed the instructions posted earlier: #997 (comment).
Note that $* passes through the filename argument (or any arguments) given by the IDE to mix format.
@wizonesolutions use format $FileDir$/$FileName$ in Arguments
For me it worked
Thank you for sharing useful information. I just started using a plugin "File watcher" so that edited file will be formatted automatically, and it works great :)
I've been looking in another language that has the same functionality, Go, specifically go fmt. The go plugin had a functionality that would perform go fmt on save and from what I understood, also GoLand (the Jetbrains IDE for Go) and seems that they will be deprecating that functionality in favor of File Watchers, as @Tsuyoshi84 suggested.
I would suggest it could be done in the same way for Elixir as well, since I am guessing they probably couldn't make the plugin or IDE fast enough on saves because of performing format every time.
Maybe it can be documented this in the README.md how to do this in case someone is not aware how to do it and close this one.
Source of the go fmt discussion: https://stackoverflow.com/questions/33774950/execute-gofmt-on-file-save-in-intellij
Hey everyone, I may have found a similar solution to what @tschmidleithner & @KronicDeth proposed.
I write a lot of standalone elixir code as part of tutorials that don't require mix projects. This means that I don't get the .formatter.exs for free.
What does it do?
.formatter.exs file in current directorymix format.exs and .ex files in the directory/subdirectoriesscript gist here
~/Desktop/scripts/mix_format_script.shMix Format~/Desktop/scripts/mix_format_script.sh$ProjectFileDir$$ProjectFileDir$ctrl + shift + alt + p)@KronicDeth
Thanks, I choose 1. for me as a temporary workaround.For reference if anyone is interested in that as well:
- Open Preferences
- Find "Tools" - "External Tools"
- Add +
- Name of your task (e.g.
mix format), ProgrampathToMixBinary, Arguments (format $FilePathRelativeToProjectRoot$), Working directory ($ProjectFileDir$), [x] "Synchronize files after execution".- OK
- Right click on the file, then "External Tools" - "mix format" should now correctly format your file.
- Now under "Keymap" - "External Tools" - "External Tools" - "mix format" you can add a custom keyboard shortcut (or override already assigned "Format Code" keymap).
Made it work with asdf:
In step 4 find 'pathToMixBinary' using 'whereis mix', output would be like '/home/$USER/.asdf/shims/mix'
And instead of format $FilePathRelativeToProjectRoot$ put $FileRelativePath$, because $FilePathRelativeToProjectRoot$ doesn't work for umbrella apps

Would be possible to add something like Go does?

Most helpful comment
@KronicDeth
Thanks, I choose 1. for me as a temporary workaround.
For reference if anyone is interested in that as well:
mix format), ProgrampathToMixBinary, Arguments (format $FilePathRelativeToProjectRoot$), Working directory ($ProjectFileDir$), [x] "Synchronize files after execution".