0

Is there a way of doing the following formatting things automatically in Eclipse? They seem automatic in Visual studio, and I've gotten used to them.

  1. Replace things like int a=b; with int a = b;
  2. Same for the equality operators and && and || in conditionals
  3. Space after semicoloms in for loops
  4. Have it not mess up the <= and >= operators (when doing the replacement).

I really don't want to write a regex to process my source code files.

3 Answers 3

1

I assume you're editing Java code - anyway, Eclipse offers this kind of functionality for other languages, too.

As JohnDR and Tomas Lycken said, in Preferences -> Java -> Code Style -> Formatter you can define this kind of rules (you have to create a new Formatter profile).

Note, however, that the default rules (at least for Eclipse Indigo) should already satisfy your requirements. Try to edit the default profile, and you'll see them:

  1. White Space -> Expressions -> Assignments
  2. White Space -> Expressions -> Operators
  3. White Space -> Control Statements -> 'for'
  4. When I tried, the comparison operators were treated correctly.

The formatting rules are not applied while you type, though: you have to press Ctrl + Shift + F, as Jonas suggested, or set up Eclipse to perform source code formatting every time you save the file.

This can be done in Preferences -> Java -> Editor -> Save Actions: just check 'Perform selected actions on save' and 'Format source code'.

Enjoy your formatting.

2
  • 1
    Is there a way to get it to do it as I type?
    – soandos
    Aug 29, 2011 at 23:11
  • No, as far as I know you can't apply formatting as you type. The only workaround I can see would be to use Templates (Preferences -> Java -> Editor -> Templates), which allow you to start typing and then, after pressing the 'autocomplete' shortcut (should be CTRL-Space), will insert a snippet of code formatted as you like. There are lots of pre-defined templates, try typing 'if' and then autocompleting with CTRL-Space to see what I mean.
    – TataBlack
    Aug 30, 2011 at 8:43
1

On my machine (Windows 7 with a standard Eclipse Indigo Classic installation) Ctrl+Shift+F does that, and more; in fact it formats the selected code (or entire file, if no code is selected) according to a set of settings you can change for yourself. The formatter handles operators, braces, whitespace and blank lines, symbols and much more.

Open the preferences dialog and search for "format", and you'll find Java\Code Style\Formatter, where you can define your own conventions, or see the ones Eclipse uses by default.

3
  • Is there a way to get it to do it as I type?
    – soandos
    Aug 29, 2011 at 23:11
  • @soandos: Not really, but would you really want it to? It'll take a workday or two, and then you'll be hitting Ctrl+Shift+F on a very regular basis without even thinking about it =) Aug 30, 2011 at 7:16
  • Also, as TataBlack mentions, you can set up Eclipse to do this every time you save your file if you go to Preferences\Java\Editor\Save Actions. I think one reason it can't be done (or at least isn't done) as you type, is that in a vast majority of cases, the line would not be complete, the code wouldn't compile and the formatter would have problems identifying various symbols. By doing it on command, you can be sure that the formatter can emitt error messages, without getting an error message for every typed character that doesn't complete a line of valid syntax. Aug 30, 2011 at 7:19
0

Go to Project/Properties/Java Code Style/Formatter you can they click on Configure Workspace Settings. Then edit the Active profile: you can change most of these settings. I hope this helps

2
  • Is there a way to get it to do it as I type?
    – soandos
    Aug 29, 2011 at 23:12
  • What do you mean "as you type"? I do not know of keyboard short cut if that is what you mean.
    – N4TKD
    Aug 30, 2011 at 0:02

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .