Search in sources :

Example 21 with Match

use of org.eclipse.search.ui.text.Match in project webtools.sourceediting by eclipse.

the class BasicSearchLabelProvider method getStyledText.

/**
 * <p>Given a <code>Match</code> object containing a <code>BasicSearchMatchElement</code> element
 * returns a <code>StyledString</code> in the form of:</p>
 *
 * <p>lineNum: Message (# matches)</br>
 * 1: <a></a> (2 matches)</p>
 *
 * @see org.eclipse.jface.viewers.DelegatingStyledCellLabelProvider.IStyledLabelProvider#getStyledText(java.lang.Object)
 */
public StyledString getStyledText(Object obj) {
    StyledString styledText = new StyledString();
    BasicSearchMatchElement element = null;
    if (obj instanceof Match) {
        Match match = (Match) obj;
        if (match.getElement() instanceof BasicSearchMatchElement) {
            element = (BasicSearchMatchElement) match.getElement();
        }
    } else if (obj instanceof BasicSearchMatchElement) {
        element = (BasicSearchMatchElement) obj;
    }
    // get the match count if possible
    int matchCount = 0;
    Match[] matches = new Match[0];
    if (fPage != null) {
        matches = fPage.getInput().getMatches(obj);
        matchCount = matches.length;
    }
    // else use the toString of the given object for the message
    if (element != null) {
        // $NON-NLS-1$
        String message = element.getLine().trim();
        int trimedAmount = element.getLine().indexOf(message);
        // $NON-NLS-1$
        String lineNum = element.getLineNum() + 1 + ": ";
        styledText.append(lineNum, StyledString.QUALIFIER_STYLER);
        styledText.append(message);
        // get the match count if possible
        for (int i = 0; i < matchCount; ++i) {
            int offset = matches[i].getOffset() - element.geLineOffset() + lineNum.length() - trimedAmount;
            styledText.setStyle(offset, matches[i].getLength(), HIGHLIGHT_WRITE_STYLE);
        }
    } else {
        styledText.append(obj.toString());
    }
    // append the match count if its worth appending
    if (matchCount > 1) {
        String matchesMsg = " " + MessageFormat.format(SSEUIMessages.TextSearchLabelProvider_matchCountFormat, new Object[] { new Integer(matchCount) });
        styledText.append(matchesMsg, StyledString.COUNTER_STYLER);
    }
    return styledText;
}
Also used : StyledString(org.eclipse.jface.viewers.StyledString) StyledString(org.eclipse.jface.viewers.StyledString) Match(org.eclipse.search.ui.text.Match)

Example 22 with Match

use of org.eclipse.search.ui.text.Match in project webtools.sourceediting by eclipse.

the class BasicSearchQuery method addMatch.

/**
 * Adds a match to the results of this query.
 *
 * <p><b>IMPORTANT: </b>It is very important that after
 * creating the query and <b>before</b> running the query
 * that you call {@link #setResult(AbstractTextSearchResult)}.
 * This is not a parameter because typically a search result needs
 * a reference to its query and thus the query needs to be constructed
 * before its result object can be set.</p>
 *
 * @param document the document the match is being added too
 * @param matchStart the start character of the match
 * @param matchEnd the end character of the match
 */
public void addMatch(IDocument document, int matchStart, int matchEnd) {
    try {
        int lineNumber = document.getLineOfOffset(matchStart);
        int lineStart = document.getLineOffset(lineNumber);
        int lineLength = document.getLineLength(lineNumber);
        String elementMessage = document.get().substring(lineStart, lineStart + lineLength);
        // add the match to the result
        BasicSearchMatchElement element = new BasicSearchMatchElement(fFile, lineNumber, lineStart, elementMessage);
        fResult.addMatch(new Match(element, Match.UNIT_CHARACTER, matchStart, matchEnd - matchStart));
    } catch (BadLocationException e) {
        Logger.logException(e);
    }
}
Also used : BadLocationException(org.eclipse.jface.text.BadLocationException) Match(org.eclipse.search.ui.text.Match)

Example 23 with Match

use of org.eclipse.search.ui.text.Match in project webtools.sourceediting by eclipse.

the class JSPSearchRequestor method addSearchMatch.

protected void addSearchMatch(IDocument jspDocument, IFile jspFile, int jspStart, int jspEnd, String jspText) {
    if (!jspFile.exists())
        return;
    int lineNumber = -1;
    try {
        lineNumber = jspDocument.getLineOfOffset(jspStart);
    } catch (BadLocationException e) {
        // $NON-NLS-1$
        Logger.logException("offset: " + Integer.toString(jspStart), e);
    }
    createSearchMarker(jspFile, jspStart, jspEnd, lineNumber);
    if (this.fJavaRequestor != null) {
        Match match = new Match(jspFile, jspStart, jspEnd - jspStart);
        this.fJavaRequestor.reportMatch(match);
    }
}
Also used : BadLocationException(org.eclipse.jface.text.BadLocationException) Match(org.eclipse.search.ui.text.Match)

Example 24 with Match

use of org.eclipse.search.ui.text.Match in project eclipse.platform.text by eclipse.

the class TestSearchResult method testAddMatches.

@Test
public void testAddMatches() {
    ISearchQuery query = new NullQuery();
    AbstractTextSearchResult result = (AbstractTextSearchResult) query.getSearchResult();
    // $NON-NLS-1$
    String object = "object";
    Match match1 = new Match(object, 0, 0);
    Match match2 = new Match(object, 0, 0);
    result.addMatches(new Match[] { match1, match2 });
    assertEquals(result.getMatchCount(), 2);
    result.addMatch(match1);
    assertEquals(result.getMatchCount(), 2);
}
Also used : AbstractTextSearchResult(org.eclipse.search.ui.text.AbstractTextSearchResult) ISearchQuery(org.eclipse.search.ui.ISearchQuery) Match(org.eclipse.search.ui.text.Match) Test(org.junit.Test)

Example 25 with Match

use of org.eclipse.search.ui.text.Match in project eclipse.platform.text by eclipse.

the class TestSearchResult method testAddMatchDifferentStartInOrder.

@Test
public void testAddMatchDifferentStartInOrder() {
    ISearchQuery query = new NullQuery();
    AbstractTextSearchResult result = (AbstractTextSearchResult) query.getSearchResult();
    // $NON-NLS-1$
    String object = "object";
    Match match1 = new Match(object, 0, 0);
    result.addMatch(match1);
    assertEquals(result.getMatchCount(), 1);
    Match match2 = new Match(object, 1, 1);
    result.addMatch(match2);
    Match match3 = new Match(object, 2, 2);
    result.addMatch(match3);
    Match[] matches = result.getMatches(object);
    assertTrue("matches[0]", matches[0] == match1);
    assertTrue("matches[1]", matches[1] == match2);
    assertTrue("matches[2]", matches[2] == match3);
}
Also used : AbstractTextSearchResult(org.eclipse.search.ui.text.AbstractTextSearchResult) ISearchQuery(org.eclipse.search.ui.ISearchQuery) Match(org.eclipse.search.ui.text.Match) Test(org.junit.Test)

Aggregations

Match (org.eclipse.search.ui.text.Match)45 AbstractTextSearchResult (org.eclipse.search.ui.text.AbstractTextSearchResult)19 Test (org.junit.Test)14 ISearchQuery (org.eclipse.search.ui.ISearchQuery)11 ITextFileBuffer (org.eclipse.core.filebuffers.ITextFileBuffer)7 IFile (org.eclipse.core.resources.IFile)7 Position (org.eclipse.jface.text.Position)7 Annotation (org.eclipse.jface.text.source.Annotation)6 ArrayList (java.util.ArrayList)5 BadLocationException (org.eclipse.jface.text.BadLocationException)5 IDocument (org.eclipse.jface.text.IDocument)5 HashMap (java.util.HashMap)4 HashSet (java.util.HashSet)4 IAnnotationModel (org.eclipse.jface.text.source.IAnnotationModel)4 ISearchResult (org.eclipse.search.ui.ISearchResult)4 IResource (org.eclipse.core.resources.IResource)3 MatchEvent (org.eclipse.search.ui.text.MatchEvent)3 IStatus (org.eclipse.core.runtime.IStatus)2 Status (org.eclipse.core.runtime.Status)2 IJavaElement (org.eclipse.jdt.core.IJavaElement)2