< Day Day Up > |
Recipe 4.4 Searching Code4.4.1 ProblemYou want to search a single file, or all files in a project, for some matching text. 4.4.2 SolutionUse the built-in Eclipse Search dialog. The Search menu contains multiple items (Search Search, Search File, Search Help, and Search Java), but all of them open the same dialog. 4.4.3 DiscussionAs you'd expect of a good IDE, Eclipse has a lot of built-in search support. However, searching in Eclipse is not accomplished via the standard Edit Find/Replace operation, which enables you to search the current file. Instead, searching displays all matches in the Search view so that you can select the one or ones you want to jump to. In addition, the items in the Search menu enable you to search across all files in workspaces, through the help system, all plug-ins, and more. Wildcards (* and ?) work as well. The Search dialog appears in Figure 4-7; note the four tabs there:
Figure 4-7. The Search dialogFor example, take a look at the File Search tab, which enables you to search across multiple files for specified text (printem in this example). You can give the pattern(s) of the files to search, such as "*.java, *.*", and so on, in the "File name patterns" box, and set the scope of the search with the radio buttons beneath that box.
When you click the Search button, the results appear in the Search view, shown in Figure 4-8. Double-clicking a match opens the match in the JDT editor with an arrow in the marker bar, as shown in the figure. Figure 4-8. Search resultsYou also can perform Java searches with the Java Search tab in the Search dialog. This kind of search enables you to search for Java elements by kind—types, methods, packages, constructors, and fields, as shown in Figure 4-9. You can limit the search so that it matches only references, also shown in the figure. Figure 4-9. Performing a Java search
Being able to perform multifile searches such as this is one of the biggest reasons for using an IDE. If you've been writing Java with a simple text editor, you'll find there's no comparison. Besides the Search Search, Search File, Search Help, and Search Java items in the Search menu, you also can highlight an element in your code and select the following Search menu items to do a quick search:
These items are handy for tracking down all references to a method or variable, for example; just search for the references, and double-click the results in the Search view. To limit the scope of the search, select Workspace, Hierarchy, or Working Set from each item's submenu.
4.4.3.1 Eclipse 3.0In Eclipse 3.0, you also can click a radio button to automatically limit the searches to the enclosing project in the File Search, Java Search, and Plug-in Search tabs. Plans are in the works to enable you to use regular expressions in searches as well. |
< Day Day Up > |