/** * @see * org.eclipse.search.core.text.TextSearchRequestor#acceptPatternMatch(org.eclipse.search.core.text.TextSearchMatchAccess) */ public boolean acceptPatternMatch(TextSearchMatchAccess matchRequestor) throws CoreException { int matchOffset = matchRequestor.getMatchOffset(); LineElement lineElement = getLineElement(matchOffset, matchRequestor); if (lineElement != null) { AptanaFileMatch fileMatch = new AptanaFileMatch( matchRequestor.getFile(), matchOffset, matchRequestor.getMatchLength(), lineElement); fCachedMatches.add(fileMatch); } return true; }
private LineElement getLineElement(int offset, TextSearchMatchAccess matchRequestor) { int lineNumber = 1; int lineStart = 0; if (!fCachedMatches.isEmpty()) { // match on same line as last? FileMatch last = (FileMatch) fCachedMatches.get(fCachedMatches.size() - 1); LineElement lineElement = last.getLineElement(); if (lineElement.contains(offset)) { return lineElement; } // start with the offset and line information from the last match lineStart = lineElement.getOffset() + lineElement.getLength(); lineNumber = lineElement.getLine() + 1; } if (offset < lineStart) { return null; // offset before the last line } int i = lineStart; int contentLength = matchRequestor.getFileContentLength(); while (i < contentLength) { char ch = matchRequestor.getFileContentChar(i++); if (ch == '\n' || ch == '\r') { if (ch == '\r' && i < contentLength && matchRequestor.getFileContentChar(i) == '\n') { i++; } if (offset < i) { String lineContent = getContents(matchRequestor, lineStart, i); // include line delimiter return new LineElement(matchRequestor.getFile(), lineNumber, lineStart, lineContent); } lineNumber++; lineStart = i; } } if (offset < i) { String lineContent = getContents(matchRequestor, lineStart, i); // until end of file return new LineElement(matchRequestor.getFile(), lineNumber, lineStart, lineContent); } return null; // offset outside of range }
private static String getContents(TextSearchMatchAccess matchRequestor, int start, int end) { StringBuffer buf = new StringBuffer(); for (int i = start; i < end; i++) { char ch = matchRequestor.getFileContentChar(i); if (Character.isWhitespace(ch) || Character.isISOControl(ch)) { buf.append(' '); } else { buf.append(ch); } } return buf.toString(); }