Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to have the formatter wrap code with IntelliJ?

People also ask

How do I enable wrap in IntelliJ?

You can enable soft wrap for the editor with ⇧⇧ (macOS), or *Shift+Shift (Windows/Linux), for the Search Everywhere dialogue, and then typing in soft wrap. You can also go to Preferences/Settings > Editor > General to enable Soft Wraps for more file types by default.

How do I add formatter XML to IntelliJ?

All you need is to export settings from Eclipse (go to Eclipse's Preferences → Java → Code Style → Formatter and export the settings to an XML file via the Export All button.), and then open IntelliJ IDEA Settings → Code Style → Java, click Manage, and import that XML file by simply clicking Import.


IntelliJ IDEA 14, 15, 2016 & 2017

Format existing code

  1. Ensure right margin is not exceeded

    File > Settings > Editor > Code Style > Java > Wrapping and Braces > Ensure right margin is not exceeded

    File SettingsEnsure right margin

  2. Reformat code

    Code > Reformat code...

    Reformat code

    or press Ctrl + Alt + L

    warning If you have something like this:

    thisLineIsVeryLongAndWillBeChanged();   // comment
    

    it will be converted to

    thisLineIsVeryLongAndWillBeChanged();   
    // comment  
    

    instead of

    // comment  
    thisLineIsVeryLongAndWillBeChanged();   
    

    This is why I select pieces of code before reformatting if the code looks like in the previous example.

Wrap when typing reaches right margin

  • IntelliJ IDEA 14: File > Settings > Editor > Code Style > Wrap when typing reaches right margin

    Wrap when typing

  • IntelliJ IDEA 15, 2016 & 2017: File > Settings > Editor > Code Style > Wrap on typing

    Wrap on typing


Do you mean that the formatter does not break long lines? Then please check:

Settings / Project Settings / Code Style / Wrapping

Update: In later versions of IntelliJ, the option can be found under:

Settings / Editor / Code Style

There select Wrap when typing reaches right margin.


Enabling "Ensure right margin is not exceeded" doesn't work for me in Intellij IDEA 2018.2. I have found the workaround, we need to change every elements below from "Do not wrap" to "Wrap if long".

enter image description here enter image description here

After that, we can preview what kind of wrap type will be changed by looking into right panel. If we are satisfied, Click "OK" or "Apply" to apply the changes. Finally we need a mannual format by using CTRL+ ALT+ L in Windows and Command+ Shift+ L in MacOS.


Or you can use the shortcut:

Ctrl + Alt + L