VMware offers training and certification to turbo-charge your progress.Learn more
Are you an Eclipse user? Do you want a fast and easy way to search for text snippets and patterns in your workspace? Then read on!
A new "Quick Search" is included in Spring ToolSuite (STS) 3.3.0 and Groovy Gails Tool Suite (GGTS) 3.3.0 which have just been released. Even if you are not a Spring or Grails developer, you might be interested in this Feature because it can also be installed separately into a vanilla Eclipse.
You open the dialog by pressing CTRL+SHIFT+L (or CMD+SHIFT+L on Mac):
Of course you can also use the mouse if you want to, but there's no need for your hands to leave the keyboard.
If you select a text snippet in an Eclipse Editor or View, that string will be automatically entered in the search box when you open Quick Search. Start typing to replace it. For example you can select a log or error message string in the console to quickly search for it:
If nothing is selected then your last search will be entered in the search box. This helps when you are repeatedly searching for the same pattern.
So how is this possible? Well, to be honest, we get a big boost because modern hardware is really quite fast. A fast CPU, a large amount of RAM and a good filesystem cache in the OS go a long way when you use heavily optimized native Java RegExp.
Another, more interesting part of the story is that the speed you 'experience' is actually a bit of an illusion. Worst case, you might paste a unique search term into the dialog and have to wait a few seconds as it searches all the files in your workspace. In practice you don't hit this worst case very often. So let's have a look at the two 'tricks' used behind the magic curtain. (Note: I don't claim to have invented these techniques. I'm sure they have been used before to provide responsive search experiences. This article is about how I used these techniques to create a nice text search tool for myself and other Eclipse users.)
Trick 1: asynchronicity
As soon as you type the first character the search process is kicked of and it starts searching for matches to your query. Results are shown as soon as they are found. Typically single character searches will have many matches in just about any file. So, the dialog fills up instantly. To avoid the dialog from exploding the searcher will pause when it hits a limit of about 200 results.
When you type another character the search term is updated. But rather than restarting the search from scratch the search term is updated inside the running search process. That means any results going forward will match the new search term. But what about already found results? Well, far from being useless, we know they already contain the results of the new query! For example if we type "av" then anything containing "av" must obviously also contain "a". So all we have to do to with results from old query "a" is filter out the results that don't match the new query "av". This is much faster then starting the search over again from scratch, because we only have to filter a relatively small number of in memory items rather than scan the file system again. Thus, the update as you type happens in an instant.
Trick 2: prioritization
The second trick is 'prioritization'. If left to run, the Quick Search engine would eventually walk all the files in your workspace. But it tries to be smart in walking files you are probably more interested in first. The way we do that isn't particularly sophisticated. We just prioritize based on the files currently open in editors. The highest priority is assigned to the 'current active editor'. Then files open in other editors. Then files in the same folders alongside those files and so on.
That has two benefits. Firstly, it makes the search return interesting results faster. Second, it makes the more interesting results appear near the top of the list.
For a separate install, you can get it from the Eclipse Market place (search for "Quick Search"). Or you can install directly from the update site. Open the Eclipse installer via Menu "Help >> Install New Software" and paste the update site url in the dialog (at the end of this article). Then select only the "Eclipse Quicksearch" Feature and hit the "Install" Button.