Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Elixir 1.6 task format for code formatting #997

Closed
tschmidleithner opened this issue Jan 18, 2018 · 14 comments · Fixed by #2578
Closed

Use Elixir 1.6 task format for code formatting #997

tschmidleithner opened this issue Jan 18, 2018 · 14 comments · Fixed by #2578

Comments

@tschmidleithner
Copy link

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

@nitingupta910
Copy link

nitingupta910 commented Jan 18, 2018

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.

@KronicDeth
Copy link
Owner

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 !.

@tschmidleithner
Copy link
Author

@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).

@wizonesolutions
Copy link

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

@tschmidleithner
Copy link
Author

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

@KronicDeth
Copy link
Owner

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

@harlantwood
Copy link

harlantwood commented Apr 18, 2018

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.

@jsvitolo
Copy link

jsvitolo commented May 5, 2018

@wizonesolutions use format $FileDir$/$FileName$ in Arguments
For me it worked

@Tsuyoshi84
Copy link

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 :)

@sfat
Copy link
Contributor

sfat commented Aug 31, 2018

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

@bahodge
Copy link

bahodge commented May 4, 2019

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.

@sasha1618
Copy link

sasha1618 commented Mar 4, 2020

@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

@yordis
Copy link
Contributor

yordis commented Dec 13, 2020

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

@yordis
Copy link
Contributor

yordis commented Dec 13, 2020

Would be possible to add something like Go does?

image

KronicDeth added a commit to yordis/intellij-elixir that referenced this issue May 27, 2022
Fixes KronicDeth#997

To format manually:

With the keyboard

* Mac: ⌥⇧⌘L
* Linux/Windows: Ctrl+Alt+Shift+L

From the menus

1. Code
2. Reformat File
3. Click Run in the "Reformat File" dialog

To tun on format on save:

1. Preferences
2. Tools > Actions on Save.
3. Check "Reformat code".
4. Make sure "All file types" is set or at least "Files: Elixir" is set.

Autosave

JetBrains IDEs have autosave turned on by default, but you can adjust the settings:

1. Preferences
2. Appearance & Behavior > System Settings.
3. Check or uncheck the settings in the Autosave section.
KronicDeth added a commit to yordis/intellij-elixir that referenced this issue May 27, 2022
Fixes KronicDeth#997

To format manually:

With the keyboard

* Mac: ⌥⇧⌘L
* Linux/Windows: Ctrl+Alt+Shift+L

From the menus

1. Code
2. Reformat File
3. Click Run in the "Reformat File" dialog

To tun on format on save:

1. Preferences
2. Tools > Actions on Save.
3. Check "Reformat code".
4. Make sure "All file types" is set or at least "Files: Elixir" is set.

Autosave

JetBrains IDEs have autosave turned on by default, but you can adjust the settings:

1. Preferences
2. Appearance & Behavior > System Settings.
3. Check or uncheck the settings in the Autosave section.
KronicDeth pushed a commit to yordis/intellij-elixir that referenced this issue May 27, 2022
Fixes KronicDeth#997

To format manually:

With the keyboard

* Mac: ⌥⇧⌘L
* Linux/Windows: Ctrl+Alt+Shift+L

From the menus

1. Code
2. Reformat File
3. Click Run in the "Reformat File" dialog

To tun on format on save:

1. Preferences
2. Tools > Actions on Save.
3. Check "Reformat code".
4. Make sure "All file types" is set or at least "Files: Elixir" is set.

Autosave

JetBrains IDEs have autosave turned on by default, but you can adjust the settings:

1. Preferences
2. Appearance & Behavior > System Settings.
3. Check or uncheck the settings in the Autosave section.
KronicDeth pushed a commit to yordis/intellij-elixir that referenced this issue May 27, 2022
Fixes KronicDeth#997

To format manually:

With the keyboard

* Mac: ⌥⇧⌘L
* Linux/Windows: Ctrl+Alt+Shift+L

From the menus

1. Code
2. Reformat File
3. Click Run in the "Reformat File" dialog

To tun on format on save:

1. Preferences
2. Tools > Actions on Save.
3. Check "Reformat code".
4. Make sure "All file types" is set or at least "Files: Elixir" is set.

Autosave

JetBrains IDEs have autosave turned on by default, but you can adjust the settings:

1. Preferences
2. Appearance & Behavior > System Settings.
3. Check or uncheck the settings in the Autosave section.
KronicDeth pushed a commit to yordis/intellij-elixir that referenced this issue May 30, 2022
Fixes KronicDeth#997

To format manually:

With the keyboard

* Mac: ⌥⇧⌘L
* Linux/Windows: Ctrl+Alt+Shift+L

From the menus

1. Code
2. Reformat File
3. Click Run in the "Reformat File" dialog

To tun on format on save:

1. Preferences
2. Tools > Actions on Save.
3. Check "Reformat code".
4. Make sure "All file types" is set or at least "Files: Elixir" is set.

Autosave

JetBrains IDEs have autosave turned on by default, but you can adjust the settings:

1. Preferences
2. Appearance & Behavior > System Settings.
3. Check or uncheck the settings in the Autosave section.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.