Bringing in Eclipse Formatting

Hi all

So I'm struggling a little bit to use IDEA.  Mostly, I want to use it for development in both corp and personal.  For corp, we have rigid code formatting and standards that need to be complied with.  In Eclipse, we do this with a combination of save actions and auto formatting.  It seems like there is no save action equivalent in IDEA.

In addition, we have certain standards that I don't see a way to implement in IDEA.  Namely:

- automatic adding of final to method params, variables.  IT looks like IDEA can throw a warning, but not automatically add these.
- Code formatting, such as alignment of all fields.
- The copyright header doesn't seem to support variable substitution.  I can't add ${year} in my header and have it replace automatically.

Am I doing something wrong? Are there extra plugins I need to add to support these things?

7 comments
In Eclipse, we do this with a combination of save actions and auto formatting.  It seems like there is no save action equivalent in IDEA.

What do you mean by a Save Action? (I do not use eclipse). Do you mean to have something execute upon a save? If so, to the best of my knowledge, IDEA does not have such a thing.

Of note, when you commit to version control, you can have IDEA automatically format the code, update the copyright, and run inspections. The options to do so are on the commit dialog. The dialog remembers your selections from one use to the next. That may be useful for you.

- automatic adding of final to method params, variables.  IT looks like IDEA can throw a warning, but not automatically add these.

I do not believe there is a way to have this done automatically. However, what you can do is the following. You would need the "Local variable or parameter can be final" inspection turned on (File > Settings > [Project Settings] > Inspections > Code Style Issues >  "Local variable or parameter can be final"). It sounds like you have this turned on since you are seeing the warnings. When you see the warning, place your cursor on the warning. (You can also use F2/Shift+F2 to go to the next/previous error). With your cursor on (or next to) the error, type Alt+Enter. There will be an option to fix the error by adding the final. If you select that, it will fix the current error. If you hit the right arrow key, you will see more options. One of which is to fix all such issues.

That will fix all the issues in the file.

To fix all in the project, you can run the inspection on the entire project. There are two ways to do this. 1) in the above side menu, select "Run inspection on..." and then run it on the project (or module).  2) From the menu select Analyze > Inspect Code. You can run your entire inspection profile (which may take a few minutes) or have one defined that is just the "Local variable or parameter can be final" inspection. After running the inspection using either of those methodologies, in the inspection result window, select the "Local variable or parameter can be final" inspection. and select "Apply Fix". It will add the final word to all locations found.

- Code formatting, such as alignment of all fields.

File > Settings > [Project Settings] > Code Style > Java > "Wrapping and Braces" tab > Filed Groups > Align in columns
If you have not previously altered any code styles, you need to make a copy of the default schema since it is not editable. Click the "Manage" button at the top and copy the default schema to a new schema named as desired.

- The copyright header doesn't seem to support variable substitution.  I can't add ${year} in my header and have it replace automatically.

You need to use the variable: ${today.year}

0

Save action - whenever you save the file it does this formatting.  You can tell it to either format the changed lines or the whole file.

Not having that is kind of a killer for me, and it seems like even the addition of final I would expect to be inserted automatically when you do ALT+ENTER but you have to select it from the menu (which is a lot of key strokes).  In Eclipse, I can do a format (CTRL+SHIFT+F) and it'll reformat the entire file.

0

When you trigger an intention with alt-enter you can have it fix all of them by pressing the right arrow then select "Fix all blah".

There is a shortcut for reformatting code, on a Mac it is alt-command-L. Not sure what is on Windows but you can check by looking at the menu item which is Code->Reformat Code

0

johndament wrote:

Save action - whenever you save the file it does this formatting.  You can tell it to either format the changed lines or the whole file.
Not having that is kind of a killer for me, and it seems like even the addition of final I would expect to be inserted automatically when you do ALT+ENTER but you have to select it from the menu (which is a lot of key strokes).  In Eclipse, I can do a format (CTRL+SHIFT+F) and it'll reformat the entire file.

As Michael mentioned, you can reformat an entire file, (including either just selected text of version control modified code), a directory, module, or project. On windows the shortcut is Ctrl+Alt+L (L=Layout). You can of course modify the shortcut in the Keymap.

I personally would argue that inserting the 'final' word  should not be part of a code reformat. That is a code modification, not a  code reformat. It's just a matter of how you look at things.

johndament wrote:

... it seems like even the addition of final I would expect to be inserted automatically when you do ALT+ENTER but you have to select it from the menu (which is a lot of key strokes).

The reason for the menu is you have options. You can fix the issue, or suppress it. For some inspections there are multiple fix options. (Such as string concatenation can be fixed with a StringBuilder or String.format).

johndament wrote:

...ALT+ENTER but you have to select it from the menu (which is a lot of key strokes).

Record a macro (Edit > Macro) to do it, then map the macro to a keyboard short cut (File > Settings > [IDE Settings] > Keymap > Macros > the name of your macro. You may even be able to create a macro for running and fixing the issue on the entire project as I described in my original reply.

johndament wrote:

Not having that is kind of a killer for me

When switching to any new tool, there are going to be a few things that do not have a 1 for 1 replacement. IMHO, IntelliJ IDEA brings so much to the table over Eclipse, you should give it a try a bit longer. Having to adjust one or two things won't seem so bad after a couple of weeks, especially given how much you gain.

Keep in mind, in IDEA adding the final to the parameter when you are writing the method is only two keystrokes: f[tab]


You can also request a feature enhancement to add reformat on save. http://youtrack.jetbrains.com  You could request a second new feature to run some inspections with predefined fixes. (I request these as two separate features).

0

I tried to install this, but I get lost on this screen: http://www.jetbrains.com/idea/plugins/index.html

I don't see a way to reference your plugin.

0

Please sign in to leave a comment.