Shortcuts for Next / Previous result in the Search View

Paul Webster posted a very nice small Eclipse plugin to support Tor Norby from the Javaposse.

This plugin defines a command which defines shortcuts to navigate through the search results in the search view, even if the search view doesn’t have the focus.

I find such a plugin very useful therefore I decided to “re-blog” ;-) Pauls code. Here is the coding and the plugin.xml.

package z.ex.search;

import java.util.HashMap;

public class NextPrevSearchEntryHandler extends AbstractHandler implements IExecutableExtension {
	private String searchCommand = IWorkbenchCommandConstants.NAVIGATE_NEXT;

	public Object execute(ExecutionEvent event) throws ExecutionException {
		IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindowChecked(event);
		ICommandService cs = (ICommandService) window.getService(ICommandService.class);
		
		Command showView = cs.getCommand(IWorkbenchCommandConstants.VIEWS_SHOW_VIEW);
		HashMap<String, Object> parms = new HashMap<String, Object>();
		parms.put(IWorkbenchCommandConstants.VIEWS_SHOW_VIEW_PARM_ID, "org.eclipse.search.ui.views.SearchView");
		ParameterizedCommand showSearchView = ParameterizedCommand.generateCommand(showView, parms);
		
		IHandlerService hs = (IHandlerService) window.getService(IHandlerService.class);
		try {
			hs.executeCommand(showSearchView, (Event)event.getTrigger());
			hs.executeCommand(searchCommand, (Event)event.getTrigger());
			hs.executeCommand(IWorkbenchCommandConstants.WINDOW_ACTIVATE_EDITOR, (Event)event.getTrigger());
		} catch (NotDefinedException e) {
			throw new ExecutionException(e.getMessage(), e);
		} catch (NotEnabledException e) {
			throw new ExecutionException(e.getMessage(), e);
		} catch (NotHandledException e) {
			throw new ExecutionException(e.getMessage(), e);
		}
		
		return null;
	}

	public void setInitializationData(IConfigurationElement config,
			String propertyName, Object data) throws CoreException {
		if ("previous".equals(data)) {
			searchCommand = IWorkbenchCommandConstants.NAVIGATE_PREVIOUS;
		}
	}

}


<?xml version="1.0" encoding="UTF-8"?>
<?eclipse version="3.4"?>
<plugin>
   <extension
         point="org.eclipse.ui.commands">
      <command
            categoryId="org.eclipse.ui.category.navigate"
            defaultHandler="z.ex.search.NextPrevSearchEntryHandler:next"
            id="z.ex.search.nextSearchEntry"
            name="Next Search Entry">
      </command>
      <command
            categoryId="org.eclipse.ui.category.navigate"
            defaultHandler="z.ex.search.NextPrevSearchEntryHandler:previous"
            id="z.ex.search.prevSearchEntry"
            name="Previous Search Entry">
      </command>
   </extension>
   <extension
         point="org.eclipse.ui.bindings">
      <key
            commandId="z.ex.search.nextSearchEntry"
            contextId="org.eclipse.ui.contexts.window"
            schemeId="org.eclipse.ui.defaultAcceleratorConfiguration"
            sequence="ALT+.">
      </key>
      <key
            commandId="z.ex.search.prevSearchEntry"
            contextId="org.eclipse.ui.contexts.window"
            schemeId="org.eclipse.ui.defaultAcceleratorConfiguration"
            sequence="ALT+,">
      </key>
   </extension>

</plugin>

I think it would be cool to have this in standard Eclipse. If you agree please place your opinion in the bug report from Paul: Bug Report.

Thanks Paul, I hope it is ok to blog about your development.

If you are located in Germany you may be interested in my Eclipse RCP training which I deliver together with Ralf Ebert.

About Lars Vogel

Lars Vogel is the founder and CEO of the vogella GmbH and works as Eclipse and Android consultant, trainer and book author. He is a regular speaker at international conferences, With more than one million visitors per month his website vogella.com is one of the central sources for Eclipse and Android programming information.
This entry was posted in Eclipse and tagged , . Bookmark the permalink.

3 Responses to Shortcuts for Next / Previous result in the Search View

  1. Paul Webster says:

    Sure, this information is free for the taking :-)

    PW

  2. David M. Karr says:

    I posted this as a reply to the two Eclipse bug reports discussing this, but I’ll post it here for people who haven’t seen the bug reports.

    I now have this functionality by utilizing a saved keyboard macro with the
    Emacs+ plugin. The implementation I describe is specific to the search view, but the strategy is not. You can do this with anything you can do from the keyboard. There are some views that have issues fully supporting keyboard access, like the SVN history view (pressing the key to show the view doesn’t give the view focus).

    I described this implementation at
    .

  3. David M. Karr says:

    Ok, well, I guess URLs don’t render here. It was supposed to be “davidmichaelkarr.blogspot.com/2010/10/keyboard-nirvana-with-eclipse-and-emacs.html” with the normal protocol header.

Comments are closed.