Intellij-elixir: Use Elixir 1.6 `task format` for code formatting

Created on 18 Jan 2018  路  14Comments  路  Source: KronicDeth/intellij-elixir

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

Enhancement

Most helpful comment

@KronicDeth
Thanks, I choose 1. for me as a temporary workaround.

For reference if anyone is interested in that as well:

  1. Open Preferences
  2. Find "Tools" - "External Tools"
  3. Add +
  4. Name of your task (e.g. mix format), Program pathToMixBinary, Arguments (format $FilePathRelativeToProjectRoot$), Working directory ($ProjectFileDir$), [x] "Synchronize files after execution".
  5. OK
  6. Right click on the file, then "External Tools" - "mix format" should now correctly format your file.
  7. Now under "Keymap" - "External Tools" - "External Tools" - "mix format" you can add a custom keyboard shortcut (or override already assigned "Format Code" keymap).

All 14 comments

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:

  1. As an External Tool that you can run from the menu or assign to a keyboard shortcut
  2. Using IDEAVim plugin, you can run commands on a visual selection like normal VIM with !.

@KronicDeth
Thanks, I choose 1. for me as a temporary workaround.

For reference if anyone is interested in that as well:

  1. Open Preferences
  2. Find "Tools" - "External Tools"
  3. Add +
  4. Name of your task (e.g. mix format), Program pathToMixBinary, Arguments (format $FilePathRelativeToProjectRoot$), Working directory ($ProjectFileDir$), [x] "Synchronize files after execution".
  5. OK
  6. Right click on the file, then "External Tools" - "mix format" should now correctly format your file.
  7. Now under "Keymap" - "External Tools" - "External Tools" - "mix format" you can add a custom keyboard shortcut (or override already assigned "Format Code" keymap).

@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?

  • Find or Create .formatter.exs file in current directory
  • Runs mix format
  • Formats all .exs and .ex files in the directory/subdirectories

script gist here

  1. So, setup your Erlang SDK and Elixir SDK like normal.
  2. I created a directory and script at ~/Desktop/scripts/mix_format_script.sh
  3. In ruby mine, go to settings -> tools -> external tools
  4. Add a new one called Mix Format
  5. Program: ~/Desktop/scripts/mix_format_script.sh
  6. Arguments: $ProjectFileDir$
  7. Working Directory: $ProjectFileDir$
  8. Hit ok -> apply
  9. Close Settings window
  10. Reopen settings
  11. Keymap -> External Tools -> External Tools -> set key binding (I did ctrl + shift + alt + p)
    NOTE* you could also set it to fire on save as well.

@KronicDeth
Thanks, I choose 1. for me as a temporary workaround.

For reference if anyone is interested in that as well:

  1. Open Preferences
  2. Find "Tools" - "External Tools"
  3. Add +
  4. Name of your task (e.g. mix format), Program pathToMixBinary, Arguments (format $FilePathRelativeToProjectRoot$), Working directory ($ProjectFileDir$), [x] "Synchronize files after execution".
  5. OK
  6. Right click on the file, then "External Tools" - "mix format" should now correctly format your file.
  7. 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

Screen Shot 2020-12-12 at 8 22 18 PM

Would be possible to add something like Go does?

image

Was this page helpful?
0 / 5 - 0 ratings

Related issues

merqlove picture merqlove  路  4Comments

Nicholi picture Nicholi  路  6Comments

joshuataylor picture joshuataylor  路  3Comments

merqlove picture merqlove  路  3Comments

Istiakmorsalin picture Istiakmorsalin  路  4Comments