1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
|
/*******************************************************************************
* Copyright (c) 2000, 2008 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
package org.eclipse.search.tests.filesearch;
import java.util.regex.Pattern;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.filebuffers.FileBuffers;
import org.eclipse.core.filebuffers.LocationKind;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IDocument;
import org.eclipse.search.core.text.TextSearchEngine;
import org.eclipse.search.core.text.TextSearchMatchAccess;
import org.eclipse.search.core.text.TextSearchRequestor;
import org.eclipse.search.internal.ui.SearchPlugin;
import org.eclipse.search.internal.ui.text.FileMatch;
import org.eclipse.search.internal.ui.text.FileSearchQuery;
import org.eclipse.search.ui.text.AbstractTextSearchResult;
import org.eclipse.search.ui.text.FileTextSearchScope;
/**
*/
public class LineBasedFileSearch extends FileSearchQuery {
private static class LineBasedTextSearchResultCollector extends TextSearchRequestor {
private final AbstractTextSearchResult fResult;
private IFile fLastFile;
private IDocument fLastDocument;
private LineBasedTextSearchResultCollector(AbstractTextSearchResult result) {
fResult= result;
fLastFile= null;
fLastDocument= null;
}
/* (non-Javadoc)
* @see org.eclipse.search.core.text.FileSearchRequestor#acceptPatternMatch(org.eclipse.search.core.text.FileSearchMatchRequestor)
*/
public boolean acceptPatternMatch(TextSearchMatchAccess matchRequestor) throws CoreException {
IFile file= matchRequestor.getFile();
try {
IDocument doc= getDocument(file);
if (doc == null) {
throw new IllegalArgumentException("No document for file: " + file.getName());
}
int startLine= doc.getLineOfOffset(matchRequestor.getMatchOffset());
int endLine= doc.getLineOfOffset(matchRequestor.getMatchOffset() + matchRequestor.getMatchLength());
fResult.addMatch(new FileMatch(file, startLine, endLine - startLine + 1, null));
} catch (BadLocationException e) {
throw new CoreException(new Status(IStatus.ERROR, SearchPlugin.getID(), IStatus.ERROR, "bad location", e));
}
return true;
}
private IDocument getDocument(IFile file) throws CoreException {
if (file.equals(fLastFile)) {
return fLastDocument;
}
if (fLastFile != null) {
FileBuffers.getTextFileBufferManager().disconnect(fLastFile.getFullPath(), LocationKind.IFILE, null);
}
fLastFile= file;
FileBuffers.getTextFileBufferManager().connect(file.getFullPath(), LocationKind.IFILE, null);
fLastDocument= FileBuffers.getTextFileBufferManager().getTextFileBuffer(file.getFullPath(), LocationKind.IFILE).getDocument();
return fLastDocument;
}
/* (non-Javadoc)
* @see org.eclipse.search.core.text.FileSearchRequestor#endReporting()
*/
public void endReporting() {
if (fLastFile != null) {
try {
FileBuffers.getTextFileBufferManager().disconnect(fLastFile.getFullPath(), LocationKind.IFILE, null);
} catch (CoreException e) {
}
}
}
}
private final FileTextSearchScope fScope;
public LineBasedFileSearch(FileTextSearchScope scope, boolean isRegEx, boolean isCaseSensitive, String searchString) {
super(searchString, isRegEx, isCaseSensitive, scope);
fScope= scope;
}
public IStatus run(IProgressMonitor monitor) {
AbstractTextSearchResult textResult= (AbstractTextSearchResult) getSearchResult();
textResult.removeAll();
LineBasedTextSearchResultCollector collector= new LineBasedTextSearchResultCollector(textResult);
Pattern searchPattern= getSearchPattern();
return TextSearchEngine.create().search(fScope, collector, searchPattern, monitor);
}
}
|