4
votes

I'm developing an eclipse plugin where a user can search a java code given some text query, similar to the usual java search dialog in eclipse.

Java Search Image

I'm using the following code to search for a text provided by user

SearchPattern pattern = SearchPattern.createPattern("<search_string>",
            IJavaSearchConstants.TYPE, IJavaSearchConstants.PARAMETER_DECLARATION_TYPE_REFERENCE,
            SearchPattern.R_EXACT_MATCH);

    // step 2: Create search scope
    // IJavaSearchScope scope = SearchEngine.createJavaSearchScope(packages);
    IJavaSearchScope scope = SearchEngine.createWorkspaceScope();

    // step3: define a result collector
    SearchRequestor requestor = new SearchRequestor()
    {
        public void acceptSearchMatch(SearchMatch match) 
        {
            System.out.println(match.getElement());
        }
    };

    // step4: start searching
    SearchEngine searchEngine = new SearchEngine();
    try {
        searchEngine.search(pattern, new SearchParticipant[] { SearchEngine
                        .getDefaultSearchParticipant() }, scope, requestor,
                        null);
    } catch (CoreException e) {
        e.printStackTrace();
    }

Also I'm able to pass the query string from Search Dialogue to a class implementing ISearchPage.Search Dialog page Image

    public class QuerySearchPage extends DialogPage implements ISearchPage 
{
    ...
    public boolean performAction() 
    {
        System.out.println(txtQuery.getText());

        search();//search using the SearchEngine
        SearchOperation so = new SearchOperation(iFileSet);
        IRunnableWithProgress query = so;
        try 
        {
            container.getRunnableContext().run(true, true, query);
        } 
        catch (InvocationTargetException | InterruptedException e)
        {
            e.printStackTrace();
        }
        return true;
    }
}

Finally I got stuck at the point where I need to pass the search result to ISearchResultView. Basically, I have two questions:

  1. Matched results are of type Object. How to pass these results to ISearchResultView which takes IFile as input?
  2. How to get results in the below format?

Search Result View Image

I have already gone through the following links:

Any help is highly welcomed.

2
ISearchResultView is deprecated, ISearchResultPage declared using the org.eclipse.search.searchResultViewPages is the recommended method. - greg-449
Thanks for pointing it out, but it didn't help much. - Payam R

2 Answers

1
votes

Usually, you would implement a ISearchResultPage that is capable of displaying the search result. In its createControl() method you need to create a viewer that knows how to present the matches.

A commonly used abstract implementation of ISearchResultPage is AbstractTextSearchViewPage. This class uses a TableViewer or a TreeViewer to present the machtes, depending on whether they are hierarchical or not. In case you use the latter, implement its configureTreeViewer() and/or configureTableViewer() methods so that the viewers are equipped with label providers and content providers that know the specific type that represents a match, i.e. what you referred to as the 'Matched results of type Object'.

The AbstractTextSearchViewPage constructor must be told which kinds of layouts it supports: FLAG_LAYOUT_FLAT and/or FLAG_LAYOUT_TREE. The actual representation can be changed with setLayout().

To start with you could restrict the search view page to a flat layout and implement its configureTableViewer() like this:

viewer.setLabelProvider( new MyLabelProvider() );
viewer.setContentProvider( new MyContentProvider() );

The input for the content provider is your ISearchResult implementation. Hence the MyContentProvider could obtain the elements to be shown from the search result.

@Override
public void inputChanged( Viewer viewer, Object oldInput, Object newInput ) {
  searchResult = ( MySearchResult )newInput;
}

@Override
public Object[] getElements( Object inputElement ) {
  return searchResult.getElements();
}
0
votes