Wednesday, November 21, 2007

Using Eclipse Efficiently

Eclipse is a powerful tool with a lot of features. I’m always looking for tips on working more efficiently. Most of the time when I come across a page of Eclipse tips it’s just the most basic usage, like “hit F3 to go to a declaration!” I’ve tried to put together some lesser known tips and features that I see my coworkers overlooking when I stop by their cube.

Faster Navigation

If you work with interfaces a lot you’ve probably run into the issue where going to the declaration of a function brings you to the interface, not the implementation. Instead of F3, hit Ctrl-T while on the function name. You’ll see all the classes where it’s implemented. Use the arrow keys to choose the right one and hit return to go to that definition.

Remember to use Open Type (Ctrl-Shift-T) to open a class, and Open Resource to browse to a file. (Most of my coworkers seem to use only Open Resource). Although you can use Open Resource to open Java files, Open Type shows only class names making it a shorter list. Open Type and Open Resource both support wildcards. Open Type supports camel case.

Ctrl-O in a class definition brings up Quick Outline. Start typing a member name and hit return once it’s unambiguous. Combined with Open Type this is a lightning fast way to go to any method in any class.

Ctrl-F6 is a great way to jump around between open editors. The order of editors is very intuitive with most recently used at the top. I find this much better than Switch To Editor. Ctrl-F7 does the same for views and Ctrl-F8 for perspectives.

Use Shift-Alt-X T to run the unit tests in the class you’re working on. (The keystroke seems long but they are logical, Shift-Alt-X for execute, Shift-Alt-D for debug. T for test). You can really get a fast turn around time by making changes, saving the file, then Shift-Alt-X T to see if the test succeeds.

Instead of searching for all references, try searching for Write Access or Read Access references only if you only care about one type. There are usually a lot fewer writes so this can really narrow down where a variable is changed.

Instead of jumping into a reference to see what a function does, trying leaving the Declaration view open. It shows the body of any function your cursor is over without having to use any special keys so you can always glance down for reference.

Less Typing

I think of Quick Fix as a tool for writing code, not something that just corrects accidental errors. Instead of trying to type perfect code and using Quick Fix only when you make a mistake, try intentionally leaving out code, then using Quick Fix to add it in. For example, call a function that isn’t defined, passing in all the arguments you want to to take. Use Ctrl-1, Enter to create an empty version of the function with all the right parameter types. I also like Quick Fix for creating casts. I assign an expression to a variable without a cast, then use Quick Fix to add it in.

Instead of declaring a variable and then assiging the value of an expression to it, try just writing the expression, then use Quick Assist – Assign to Field (Ctrl-2 L) to generate the declaration. The variable name will be highlighted and the drop down gives you several reasonable alternatives to use for the variable name. Tab again to get to the type to choose an alternative. For example if you had new ArrayList() and used Assign to Field you might choose List from the list of type alternatives.

Completion on empty string works. For example if your cursor is in an empty function, Alt-/ will show some reasonable completion choices like the member names in your class.

The Rename Refactoring (Shift-Alt-R) works without having to take your fingers off the keyboard, allowing you to safely rename variables or functions quickly. Try it from the keyboard instead of the refactoring menu. Inline also works well from the keyboard (Shift-Alt-I).

To improve completion try the Content Assist – Favorites setting. Add any static imports you use frequently. You’ll be prompted for these when completing even if you haven’t imported them. Also use Type Filters to remove classes you never want to see. If I want to use java.util.List and complete on List I see antlr.collections.List at the top, which is never what I want. Adding antlr.collections.* to the type filters suppresses this.

Taking it to the next level

To get productivity increases across the board, try the Mylyn extension. It takes some getting used to but improves the UI in lots of small subtle ways—completion works better (showing interesting choices first), opening types is easier, the Package Explorer has less junk and checkins are easier when you’re working on more than one thing at a time.

Code templates can really be a time saver. Try completing on “foreach” after a collection has been declared. Most of the time you get a good loop template.

Any more?

If you have other favorite, lesser known tips, feel free to add them to the comments.

  1. CTRL+SHIFT+G – search references of class in workspace

    SHIFT+F2 – open external javadoc

    ALT+LEFT ARROW – back

    CTRL+SHIFT+O – organize imports

    CTRL+M maximalize window

    ALT+SHIFT+J – add javadoc

    ace Nov 14, 10:06 AM #
  2. ctrl-o ctrl-o to show inherited members too!

    Chris Richardson Nov 14, 07:27 PM #
  3. Ctrl+Shift+O is a really big one. Just enter unqualified class names and use Ctrl+Shift+O to resolve everything. If there’s only one class with that name, it will be auto-imported. If there’s more than one, you get a dialog similar to Open Type in which you can choose the correct class.

    Ctrl+Alt+H (show call hierarchy) is another I use on a daily basis.

    Shift+Alt+L extracts an expression to a variable.

    And when in doubt, let’s not forget Ctrl+3, which lets you access any command by typing the name (or a fragment of the name).

    Daniel Spiewak Nov 14, 08:11 PM #
  4. I would also suggest changing the hot keys combination also.
    Like switching editors using Ctrl+F6 is awkward compared to Ctrl+Tab. Similarly running your java program using Ctrl+R is much more convenient.

    You can change the keys from Window -> Preferences -> General -> Keys

    Chetan Nov 15, 09:02 AM #
  5. The coolest thing in 3.3 I use is Save Actions – these are actions performed on a file each time its saved.

    So – organize imports (Ctrl-Shift-O), apply source formatting (Ctrl-Shift-S), and code cleanup (add annotations, make things final, etc) all just happen without me thinking about it.

    fred Nov 15, 09:36 AM #
  6. I love and use most of the shortcuts above, but I would suggest that if you are in a team environment to make sure that you export and share your settings so that you have consistent check-ins.

    Qaiser Nov 15, 10:26 AM #
  7. alt-shift-upArrow

    Increases the selection to next next largest java expression.

    alt-shift-downArrow to decrease.
    alt-shift-leftArrow to add the expression to the left
    alt-shift-rightArrow similarly

    EclipseUser Nov 15, 11:54 AM #
  8. Ctrl+M – full screen

    Sivaswami Nov 15, 03:11 PM #
  9. @Chetan – I agree, when I set up a new workspace, the first thing I do is set the “Quick Fix/Quick Assist” key combination to Ctrl+Enter, from the carpal tunnel-inducing default of Ctrl+1 :)

    One note that isn’t mentioned in the blog post regarding Open Type (Ctrl+Shift+T) is that it is also the only way (that I know of) to open class files in third-party libraries – extremely helpful.

    Other nice key equivalents:

    Alt+Shift+W: “Show in…”, easy way to highlight the file you’re currently editing in the Navigator panel.

    Alt+Shift+T: Refactor sub-menu

    Alt+Shift+S: Source sub-menu

    Alt+Shift+V: Move (refactor)

    Alt+Shift+Z: Surround With (generally requires a block selection) – easy way to wrap do/while, if, etc. block elements around something.

    Peter Mularien Nov 15, 03:35 PM #
  10. Nice tips.

    MM Nov 15, 06:49 PM #
  11. “Open Type supports camel case”

    So does Open Resource, as of 3.3.

    Good tips though.

No comments: