views:

405

answers:

4

I have been wracking my brain trying to figure this out. For the first time I used jEdit the other day and I was pleasantly surprised that it auto indented my code (meaning that I'd put in the following code:

int method () {
    _ //<-- and it put me here automatically

I've tried to get the same thing working with eclipse but with no success. I got into the code formatter but I don't see how to make that happen.

Is it possible to do this? Also while I'm here, is there a such thing as a eclipse plugin that will allow you to search the methods and classes of the standard java library?

Thanks

+6  A: 

My clean eclipse install does this by default.

Have you changed any options? Make sure the file you are editing has the .java file extension. The preference options that control the typing automations are under Java -> Editor -> Typing in the Window -> Preferences menu.

Also, I find that the auto-indenting, and most of the other auto-complete functions of eclipse do not function well if the file I am editing has errors in it which prevent compilation. Make sure that your curly-braces are matched correctly, this is the main one that I've noticed blocks auto-indent.

Regarding searching through the standard Java libraries, use the Search -> Java.. menu option, and check the JRE libraries checkbox, then search away. You can also use the Hierarchy view to see how the classes relate. Also, in the Package and Project views you can expand the JRE System Library, and then expand rt.jar which holds pretty much all the standard Java pacakges.

Ben S
A: 

You should check:

Macarse
Although these are useful resources, they don't really answer the OP's question.
Ben S
+3  A: 

Personally all I use for this is the format options Window->preferences under Java->Code Style ->Formatter.

I once took the time to tweek how I like my code to look like when I work and exported the whole thing. After that I just code without too much bother on what it looks like. When I find the code looks messy ctrl-shit-f and the whole class becomes pretty again, comments and all.

After a while it pretty much became a reflex...

code code code

ctrl-s, ctrl-b (cause I disable auto build sometimes), ctrl-shift-f

code some more etc...

Once I got used to this I never really cared how it presented the code as i was typing because I knew it would look all pretty as soon as the loop/if/switch/method etc is finished

Newtopian
You can make it so that the formatting happens automatically when you save. This saves me `Java -> Editor -> Save Actions` I've turned on everything in that preference pane. Now I just hit `Ctrl-S` and everything looks pretty, including my imports any `final`s I might have forgotten, as well as sorting all the members to my preferences.
Ben S
woah... even better :-) tried it... love it thanks Ben
Newtopian
A: 

Eclipse has always done this for me by default.

One really cool thing about eclipse is that you can search preference pages. Just right click and go to prefrences. Go to the "Window" menu, and click "Prefrences". Then at the top of the tree view there's a text box that says "type filter text". Replace that with "indent" and it should bring up the page where the indent option is.

Make sure that eclipse recognizes your file as a java file, that you're using the Java distribution, the latest version, etc.

Chad Okere