|
Lines 77-83
Link Here
|
| 77 |
|
77 |
|
| 78 |
private static final class MatchesComparator implements Comparator<Match> { |
78 |
private static final class MatchesComparator implements Comparator<Match> { |
| 79 |
public int compare(Match m1, Match m2) { |
79 |
public int compare(Match m1, Match m2) { |
| 80 |
return m1.getOffset() - m2.getOffset(); |
80 |
int diff= m1.getOffset() - m2.getOffset(); |
|
|
81 |
if (diff == 0) |
| 82 |
diff= m2.getLength() -m1.getLength(); |
| 83 |
return diff; |
| 81 |
} |
84 |
} |
| 82 |
} |
85 |
} |
| 83 |
|
86 |
|
|
Lines 158-204
Link Here
|
| 158 |
|
161 |
|
| 159 |
public static LineSearchElement[] createElements(IIndexFileLocation fileLocation, Match[] matches, |
162 |
public static LineSearchElement[] createElements(IIndexFileLocation fileLocation, Match[] matches, |
| 160 |
IDocument document) { |
163 |
IDocument document) { |
| 161 |
// sort matches according to their offsets |
164 |
// Sort matches according to their offsets |
| 162 |
Arrays.sort(matches, MATCHES_COMPARATOR); |
165 |
Arrays.sort(matches, MATCHES_COMPARATOR); |
| 163 |
// group all matches by lines and create LineSearchElements |
166 |
// Group all matches by lines and create LineSearchElements |
| 164 |
List<LineSearchElement> result = new ArrayList<LineSearchElement>(); |
167 |
List<LineSearchElement> result = new ArrayList<LineSearchElement>(); |
| 165 |
int firstMatch = 0; |
168 |
List<Match> matchCollector= new ArrayList<Match>(); |
| 166 |
while (firstMatch < matches.length) { |
169 |
int minOffset = 0; |
| 167 |
try { |
170 |
int lineNumber = 0; |
| 168 |
int lineNumber = document.getLineOfOffset(matches[firstMatch].getOffset()); |
171 |
int lineOffset = 0; |
| 169 |
int lineOffset = document.getLineOffset(lineNumber); |
172 |
int lineLength = 0; |
| 170 |
int lineLength = document.getLineLength(lineNumber); |
173 |
int lineEndOffset = 0; |
| 171 |
int nextlineOffset = lineOffset + lineLength; |
174 |
|
| 172 |
int nextMatch = firstMatch; |
175 |
try { |
| 173 |
int nextMatchOffset = matches[nextMatch].getOffset(); |
176 |
for (final Match match : matches) { |
| 174 |
while (nextMatch < matches.length && nextMatchOffset < nextlineOffset) { |
177 |
final int offset= match.getOffset(); |
| 175 |
nextMatch++; |
178 |
if (offset < lineEndOffset) { |
| 176 |
if (nextMatch < matches.length) |
179 |
// Match on same line |
| 177 |
nextMatchOffset = matches[nextMatch].getOffset(); |
180 |
if (offset < minOffset) { |
| 178 |
} |
181 |
// Match is not overlapped by previous one. |
| 179 |
int lineMatchesCount = nextMatch - firstMatch; |
182 |
matchCollector.add(match); |
| 180 |
Match[] lineMatches = new Match[lineMatchesCount]; |
183 |
minOffset= offset + match.getLength(); |
| 181 |
System.arraycopy(matches, firstMatch, lineMatches, 0, lineMatchesCount); |
184 |
} |
|
|
185 |
} else { |
| 186 |
// Match is on a new line |
| 187 |
if (!matchCollector.isEmpty()) { |
| 188 |
// Complete a line |
| 189 |
String content = document.get(lineOffset, lineLength); |
| 190 |
Match[] lineMatches= matchCollector.toArray(new Match[matchCollector.size()]); |
| 191 |
result.add(new LineSearchElement(fileLocation, lineMatches, lineNumber + 1, content, lineOffset)); |
| 192 |
matchCollector.clear(); |
| 193 |
} |
| 194 |
// Setup next line |
| 195 |
lineNumber = document.getLineOfOffset(offset); |
| 196 |
lineOffset = document.getLineOffset(lineNumber); |
| 197 |
lineLength = document.getLineLength(lineNumber); |
| 198 |
lineEndOffset = lineOffset + lineLength; |
| 199 |
matchCollector.add(match); |
| 200 |
} |
| 201 |
} |
| 202 |
if (!matchCollector.isEmpty()) { |
| 203 |
// Complete a line |
| 182 |
String content = document.get(lineOffset, lineLength); |
204 |
String content = document.get(lineOffset, lineLength); |
|
|
205 |
Match[] lineMatches= matchCollector.toArray(new Match[matchCollector.size()]); |
| 183 |
result.add(new LineSearchElement(fileLocation, lineMatches, lineNumber + 1, content, lineOffset)); |
206 |
result.add(new LineSearchElement(fileLocation, lineMatches, lineNumber + 1, content, lineOffset)); |
| 184 |
firstMatch = nextMatch; |
207 |
matchCollector.clear(); |
| 185 |
} catch (BadLocationException e) { |
|
|
| 186 |
CUIPlugin.log(e); |
| 187 |
} |
208 |
} |
|
|
209 |
} catch (BadLocationException e) { |
| 210 |
CUIPlugin.log(e); |
| 188 |
} |
211 |
} |
| 189 |
return result.toArray(new LineSearchElement[result.size()]); |
212 |
return result.toArray(new LineSearchElement[result.size()]); |
| 190 |
} |
213 |
} |
| 191 |
|
214 |
|
| 192 |
private static LineSearchElement[] collectLineElements(AbstractCharArray buf, Match[] matches, |
215 |
private static LineSearchElement[] collectLineElements(AbstractCharArray buf, Match[] matches, |
| 193 |
IIndexFileLocation fileLocation) { |
216 |
IIndexFileLocation fileLocation) { |
|
|
217 |
|
| 194 |
List<LineSearchElement> result = new ArrayList<LineSearchElement>(); |
218 |
List<LineSearchElement> result = new ArrayList<LineSearchElement>(); |
|
|
219 |
List<Match> matchCollector= new ArrayList<Match>(); |
| 220 |
|
| 195 |
boolean skipLF = false; |
221 |
boolean skipLF = false; |
| 196 |
int lineNumber = 1; |
222 |
int lineNumber = 1; |
| 197 |
int lineOffset = 0; |
223 |
int lineOffset = 0; |
| 198 |
int lineFirstMatch = -1; // not matched |
224 |
int i = 0; |
| 199 |
int nextMatch = 0; |
225 |
Match match= matches[i]; |
| 200 |
int nextMatchOffset = matches[nextMatch].getOffset(); |
226 |
int matchOffset = match.getOffset(); |
| 201 |
for (int pos = 0; buf.isValidOffset(pos); pos++) { |
227 |
for (int pos = 0; buf.isValidOffset(pos); pos++) { |
|
|
228 |
if (matchOffset <= pos && match != null) { |
| 229 |
// We are on the line of the match, store it. |
| 230 |
matchCollector.add(match); |
| 231 |
final int minOffset= matchOffset + match.getLength(); |
| 232 |
match= null; |
| 233 |
matchOffset= Integer.MAX_VALUE; |
| 234 |
for(i=i+1; i<matches.length; i++) { |
| 235 |
// Advance to next match that is not overlapped |
| 236 |
final Match nextMatch= matches[i]; |
| 237 |
final int nextOffset= nextMatch.getOffset(); |
| 238 |
if (nextOffset >= minOffset) { |
| 239 |
match= nextMatch; |
| 240 |
matchOffset= nextOffset; |
| 241 |
break; |
| 242 |
} |
| 243 |
} |
| 244 |
} |
| 245 |
|
| 202 |
char c = buf.get(pos); |
246 |
char c = buf.get(pos); |
| 203 |
// consider '\n' and '\r' |
247 |
// consider '\n' and '\r' |
| 204 |
if (skipLF) { |
248 |
if (skipLF) { |
|
Lines 209-230
Link Here
|
| 209 |
} |
253 |
} |
| 210 |
} |
254 |
} |
| 211 |
if (c == '\n' || c == '\r') { |
255 |
if (c == '\n' || c == '\r') { |
| 212 |
// create new LineElement if there were matches |
256 |
// Create new LineElement for collected matches on this line |
| 213 |
if (lineFirstMatch != -1) { |
257 |
if (!matchCollector.isEmpty()) { |
| 214 |
int lineLength = pos - lineOffset; |
258 |
int lineLength = pos - lineOffset; |
| 215 |
int lineMatchesCount = nextMatch - lineFirstMatch; |
259 |
Match[] lineMatches= matchCollector.toArray(new Match[matchCollector.size()]); |
| 216 |
Match[] lineMatches = new Match[lineMatchesCount]; |
|
|
| 217 |
System.arraycopy(matches, lineFirstMatch, lineMatches, 0, lineMatchesCount); |
| 218 |
char[] lineChars= new char[lineLength]; |
260 |
char[] lineChars= new char[lineLength]; |
| 219 |
buf.arraycopy(lineOffset, lineChars, 0, lineLength); |
261 |
buf.arraycopy(lineOffset, lineChars, 0, lineLength); |
| 220 |
String lineContent = new String(lineChars); |
262 |
String lineContent = new String(lineChars); |
| 221 |
result.add(new LineSearchElement(fileLocation, lineMatches, lineNumber, lineContent, |
263 |
result.add(new LineSearchElement(fileLocation, lineMatches, lineNumber, lineContent, |
| 222 |
lineOffset)); |
264 |
lineOffset)); |
| 223 |
lineFirstMatch = -1; |
265 |
matchCollector.clear(); |
| 224 |
if (nextMatch >= matches.length) |
266 |
if (match == null) |
| 225 |
break; |
267 |
break; |
| 226 |
if (matches[nextMatch].getOffset() < pos) |
|
|
| 227 |
lineFirstMatch = nextMatch; |
| 228 |
} |
268 |
} |
| 229 |
lineNumber++; |
269 |
lineNumber++; |
| 230 |
lineOffset = pos + 1; |
270 |
lineOffset = pos + 1; |
|
Lines 232-262
Link Here
|
| 232 |
skipLF = true; |
272 |
skipLF = true; |
| 233 |
continue; |
273 |
continue; |
| 234 |
} |
274 |
} |
| 235 |
// compare offset of next match with current position |
|
|
| 236 |
if (nextMatchOffset > pos || nextMatch >= matches.length) |
| 237 |
continue; |
| 238 |
// next match was reached |
| 239 |
// check if this match is the first for current line |
| 240 |
if (lineFirstMatch == -1) |
| 241 |
lineFirstMatch = nextMatch; |
| 242 |
// goto to next match |
| 243 |
nextMatch++; |
| 244 |
if (nextMatch < matches.length) { |
| 245 |
// update offset of next match |
| 246 |
nextMatchOffset = matches[nextMatch].getOffset(); |
| 247 |
} |
| 248 |
} |
275 |
} |
| 249 |
// check if there were matches on the last line |
276 |
// Create new LineElement for matches on the last line |
| 250 |
if (lineFirstMatch != -1) { |
277 |
if (!matchCollector.isEmpty()) { |
| 251 |
int lineLength = buf.getLength() - lineOffset; |
278 |
int lineLength = buf.getLength() - lineOffset; |
| 252 |
int lineMatchesCount = nextMatch - lineFirstMatch; |
279 |
Match[] lineMatches= matchCollector.toArray(new Match[matchCollector.size()]); |
| 253 |
Match[] lineMatches = new Match[lineMatchesCount]; |
|
|
| 254 |
System.arraycopy(matches, lineFirstMatch, lineMatches, 0, lineMatchesCount); |
| 255 |
|
| 256 |
char[] lineChars= new char[lineLength]; |
280 |
char[] lineChars= new char[lineLength]; |
| 257 |
buf.arraycopy(lineOffset, lineChars, 0, lineLength); |
281 |
buf.arraycopy(lineOffset, lineChars, 0, lineLength); |
| 258 |
String lineContent = new String(lineChars); |
282 |
String lineContent = new String(lineChars); |
| 259 |
result.add(new LineSearchElement(fileLocation, lineMatches, lineNumber, lineContent, lineOffset)); |
283 |
result.add(new LineSearchElement(fileLocation, lineMatches, lineNumber, lineContent, |
|
|
284 |
lineOffset)); |
| 260 |
} |
285 |
} |
| 261 |
return result.toArray(new LineSearchElement[result.size()]); |
286 |
return result.toArray(new LineSearchElement[result.size()]); |
| 262 |
} |
287 |
} |