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;
}
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);
}
}
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);
}
}
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);
}
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);
}
Aggregations