Introduction
In this Java tip, I will go through a few basic tools provided by the Eclipse IDE for navigating through your code.
Tools that will be covered:
1. Using the Outline tool
2. View History tools
3. Showing code of selected Item
4. Code Folding
5. Open Declaration
For this tip, you will want to get the Eclipse Helios IDE* (also known as Eclipse 3.6, it was released June 24, 2010).
Download link: Eclipse Downloads.
*note: older Eclipse IDE's are likely to have some or all of these features, but I can't guarantee that all of them will be there, or if they will even have the same functionality.
Eclipse Code Outline tool
The outline tool is a nice little tool that shows an overview of the current file opened.
outline_1.jpg
As you can see from the screenshot, The outliner has a list of different classes, methods, fields, import declarations, etc. You can click on each item inside of the outliner and the text editor will go to the declaration of that item. Do note that the outliner does not show local variables (or at least, I haven't figured out to get it to do this yet).
If you notice in the icon next to the field name, you can see all sorts of different icons for different properties:
1. The return type of methods or the type of the field is on the far right.
2. The visibility of the item is an icon on the right:
- Private is a red square
- Protected is a yellow diamond
- Default is a blue triangle
- Public is a green circle
3. Whether the item is static or abstract (denoted by a little A or S next to the item visibility icon.
4. Whether there is an error/warning with the item. This is located in the bottom left portion of the visibility icon.
5. Some other misc. information, such as overrided methods have a small triangle next to the visibility icon.
Along the top row, you can filter the items shown in the outliner by clicking on the various buttons.
icons.png
From left to right, the icons are:
1. Sort alphabetically. This sorts the different items alphabetically, with those alphabetically first put towards the top. Unchecking this option will re-order the items in the order they occur in the source.
2. Hide Fields.
3. Hide Static fields and methods.
4. Hide non-public members. Basically anything not declared public will be hidden in the outliner.
5. Hide local type. Not actually sure what qualifies as a local type...
6. Focus on active task. This will probably be talked about later in another tip.
You can also provide a custom filter for the outliner. The only useful filter feature I've found is the name filter. Basically, this lets you create a filter that searches through for different items by their name.
To create a custom filter:
Click on the little triangle next to the icons. Select the "Filters..." item. I think it's basically a simplified regular expression engine that allows you to filter items you want to hide. You can also choose the options of hiding imports, package declarations, and/or synthetic members (not quite sure what synthetic members are yet).
Quick Outlines
It is possible to open up a quick outliner by right-clicking in the editor, then choosing the "Quick Outline" option. This basically opens up a outline viewer inside a small pop-up. You can also open up the quick outline view via the CTRL+O hotkey (kind of strange since most other applications reserve CTRL+O for open...)
View History Tools
View history tools allow you to jump between different sections of code that you've previously looked over. So say in the code below:
public class MyClass { String string; public static void main(String[] args) { Scanner reader = new Scanner("test.txt"); StringBuilder text = new StringBuilder(); while(reader.hasNext()) { text.append(reader.nextLine()); text.append("\r\n"); } MyClass tempObject = new MyClass(text.toString()); System.out.println(tempObject); System.out.println(tempObject.hashCode()); System.out.println(tempObject.doIt()); } public MyClass(String string) { this.string = string; // comment } public String toString() { return this.string(); } public int hashCode() { return this.string.hashCode(); } public String doIt() { return "Wrong method to use!"; } public String doItCorrect() { return "Correct method to use!"; } }
view_history.png
In the above code, say you were looking through this code and you get to the last line and wondered what doIt() did. You could use any of the other features outlined in this tip (such as the outline view or the open declaration) to go and take a look at what doIt() does. In this case, say it's not the correct method you wanted to use. You can use the "Back to..." button to jump back to the view you were previously looking at and change the code accordingly, so basically this is just an "undo/redo" feature for where you are looking in the editor. Note that this functionality does work across different files, so you can actually jump between several classes just by pressing the arrows, or you can even press the drop-down arrows to jump to a certain point in the view history.
The button on the far left of the view history tools jumps your view to the last edit location. Basically, this is the last location where you actually changed something in the file. Again, this feature does work across multiple files, however unlike the buttons that change the view, this will only remember the last edit location, not a stack of all previous edits (one edit location period, not one edit location/file).
Showing code of selected item
It is possible to just show the code of a single section of a file. This allows you to edit a method or section of code without having to view through the other code (especially in large classes). This feature is actually not visible by default (at least not in Java editing). You have to click on the toolbar, choose customize perspective, in the Tool Bar Visibility tab, under Editor Presentation, check Show Source of Selected Element Only.
enable_source_selected.jpg
Once you've got the button enabled, simply enable it by clicking it and now you can use the outline viewer to not only jump to the method, but you can also hide all code other than the item you have selected.
A note about using this feature: I've noticed that after using this feature, sometimes the code folding feature does break, I'm not sure if this is intended or if this is an actual bug.
Code Folding
Code folding is a useful feature similar to the above item which hides sections of code. Code folding is enabled by default. Unfortunately, there is only a specific list of items that you can fold:
1. Import declarations
2. Javadoc comments
3. Block comments (as long as they are not inside any methods)
4. Methods
5. Anonymous classes
6. Internal classes/enums/interfaces
Hopefully that's all of them, there might be a few other items I've missed.
To fold a section of code, simply find the item you want to fold, then click on the little circle on the right with a minus sign in it to fold the code, and click on the circle with a plus sign in the same location to un-fold that section of code.
folding.jpg
Folding icons are highlighted by small red circles.
Open Declaration
Open Declaration is a useful feature if you want to jump to the declaration of different items. This allows you to quickly move between several pieces of code to find where an item is you want to look at/edit.
To open the declaration
Select the text you want to goto the definition of, Right click inside the editor, choose Open Declaration. You can actually put the edit cursor anywhere inside the item you want to open the declaration of and Eclipse will automatically pick up on which item it is you want to jump to.
You can also use the open declaration hotkey (F3)
Conclusion
Hopefully this helps you when navigating through code, particularly code that is either very long, or code that you either didn't write or haven't looked at in a long time.
Happy Coding