How enable auto-format code for Intellij IDEA?

后端 未结 15 1063
小鲜肉
小鲜肉 2020-12-22 17:05

Is it possible in Intellij IDEA after typing \';\' or press \'Enter\' happened formatting of this string?

For instance, to:

a+b=10;

相关标签:
15条回答
  • 2020-12-22 17:42

    The way I implemented automatical reformating like in Microsoft Visual Studio (It doesn't work perfect):

    1. Edit > Macros > Start Macro Recording
    2. Press continuously: Enter + Ctrl+Alt+I
    3. Edit > Macros > Stop Macro Recording (Name it for example ReformatingByEnter)
    

    Now we need to perform same actions but for Ctrl+Alt+L+;

    4. Edit > Macros > Start Macro Recording
    5. Press continuously: ; + Ctrl+Alt+I
    6. Edit > Macros > Stop Macro Recording (Name it for example ReformatingBy;)
    

    Now we need to assign HotKeys to these macros:

    7. File > Settings > Keymap > press on a gear button > Duplicate...
    8. Unfold a macro button (beneath) and by right clicking on two 
    ours macros assign HotKeys for them: "Enter" and ";" correspondingly.
    
    0 讨论(0)
  • 2020-12-22 17:45

    None of the solutions in Intellij is as elegant (or useful) as in Eclipse. What we need is feature request to the intellij so that we can add a hook (what actions to perform) when the IDE autosaves.

    In Eclipse we can add "post-save" actions, such as organize imports and format the class. Yes you have to do a "save" or ctrl-s but the hook is very convenient.

    0 讨论(0)
  • 2020-12-22 17:45

    if you want, you can use a saveActions plugin. You can reformat file, optimized the imports and more things, it's really customizable and easy to setup.

    0 讨论(0)
  • 2020-12-22 17:47

    This can also be achieved by Ctrl+WindowsBtn+Alt+L. This will be important to some people,because in some Virtual Machines, Ctrl+Alt+L can log you out.

    0 讨论(0)
  • 2020-12-22 17:47

    Well that's not possible, but in intellij 13, how about adding a mouse gesture, something like single left mouse click to reformat the code? Or if you don't use the mouse much then add a very simple keyboard hotkey that you use all the time (possibly the "enter"? not sure if intellij would be happy with that to be honest)

    0 讨论(0)
  • 2020-12-22 17:49

    I have found two ways to do this:

    1. Go to Settings> Keymap.In the right portion go to Editor Actions> complete current statement.Click on it and select add keyboard shortcut. Press ; and select ok.

    2. Use macro. Go to

    Edit> Macros> Start Macro Recording.

    Now press semicolon and keyboard shortcut to reformat code (you can find the keyboard shortcut from other answers or from settings > keymap).

    After doing reformat go to

    Edit> Macros> Stop Macro Recording

    Save the macro with a name (auto format or something else). Then go to

    Settings> Keymap> Macros> auto format (the macro name).

    Click there and select add keyboard shortcut, then press semicolon and click ok. Now whenever you will press semicolon it will write semicolon and do auto format.

    0 讨论(0)
提交回复
热议问题