Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
View | Details | Raw Unified | Return to bug 325385
Collapse All | Expand All

(-)src/org/eclipse/cdt/internal/ui/search/LineSearchElement.java (-55 / +80 lines)
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
	}
(-)src/org/eclipse/cdt/internal/ui/search/PDOMSearchPatternQuery.java (-5 / +1 lines)
Lines 145-155 Link Here
145
			for (int i = 0; i < bindings.length; ++i) {
145
			for (int i = 0; i < bindings.length; ++i) {
146
				IIndexBinding pdomBinding = bindings[i];
146
				IIndexBinding pdomBinding = bindings[i];
147
147
148
				//check for the element type of this binding and create matches if 
148
				// Select the requested bindings  
149
				//the element type checkbox is checked in the C/C++ Search Page
150
151
				//TODO search for macro
152
153
				boolean matches= false;
149
				boolean matches= false;
154
				if ((flags & FIND_ALL_TYPES) == FIND_ALL_TYPES) {
150
				if ((flags & FIND_ALL_TYPES) == FIND_ALL_TYPES) {
155
					matches= true;
151
					matches= true;
(-)src/org/eclipse/cdt/internal/ui/search/PDOMSearchQuery.java (-7 / +15 lines)
Lines 21-28 Link Here
21
import java.util.HashSet;
21
import java.util.HashSet;
22
import java.util.List;
22
import java.util.List;
23
import java.util.Map;
23
import java.util.Map;
24
import java.util.Set;
25
import java.util.Map.Entry;
24
import java.util.Map.Entry;
25
import java.util.Set;
26
26
27
import org.eclipse.core.runtime.CoreException;
27
import org.eclipse.core.runtime.CoreException;
28
import org.eclipse.core.runtime.IPath;
28
import org.eclipse.core.runtime.IPath;
Lines 310-323 Link Here
310
			return;
310
			return;
311
		List<IIndexName> names= new ArrayList<IIndexName>();
311
		List<IIndexName> names= new ArrayList<IIndexName>();
312
		List<IIndexName> polymorphicNames= null;
312
		List<IIndexName> polymorphicNames= null;
313
		HashSet<IBinding> handled= new HashSet<IBinding>();
314
		
313
		for (IBinding binding : bindings) {
315
		for (IBinding binding : bindings) {
314
			if (binding != null) {
316
			if (binding != null && handled.add(binding)) {
315
				createMatches1(index, binding, names);
317
				createMatches1(index, binding, names);
316
318
			}
317
				if ((flags & FIND_REFERENCES) != 0) {
319
		}
320
		
321
		if ((flags & FIND_REFERENCES) != 0) {
322
			for (IBinding binding : bindings) {
323
				if (binding != null) {
318
					List<? extends IBinding> specializations = IndexUI.findSpecializations(binding);
324
					List<? extends IBinding> specializations = IndexUI.findSpecializations(binding);
319
					if (!specializations.isEmpty()) {
325
					for (IBinding spec : specializations) {
320
						for (IBinding spec : specializations) {
326
						if (spec != null && handled.add(spec)) {
321
							createMatches1(index, spec, names);
327
							createMatches1(index, spec, names);
322
						}
328
						}
323
					}
329
					}
Lines 331-337 Link Here
331
									polymorphicNames= new ArrayList<IIndexName>();
337
									polymorphicNames= new ArrayList<IIndexName>();
332
								}
338
								}
333
								for (ICPPMethod mInBase : msInBases) {
339
								for (ICPPMethod mInBase : msInBases) {
334
									createMatches1(index, mInBase, polymorphicNames);
340
									if (mInBase != null && handled.add(mInBase)) {
341
										createMatches1(index, mInBase, polymorphicNames);
342
									}
335
								}
343
								}
336
							}
344
							}
337
						} catch (DOMException e) {
345
						} catch (DOMException e) {
(-)ui/org/eclipse/cdt/ui/tests/search/BasicSearchTest.java (+24 lines)
Lines 357-360 Link Here
357
		PDOMSearchResult result= (PDOMSearchResult) query.getSearchResult();
357
		PDOMSearchResult result= (PDOMSearchResult) query.getSearchResult();
358
		assertEquals(expected, result.getMatchCount());
358
		assertEquals(expected, result.getMatchCount());
359
	}
359
	}
360
	
361
	//	template<typename T> class CT {};
362
	//	template<typename T> class CT<T*> {};
363
	//	template<typename T> void f(T) {};
364
	//	template<typename T> void f(T*) {};
365
366
	//	void a() {
367
	//	  CT<int>* r1;
368
	//	  CT<char>* r2;
369
	//	  CT<int*>* r3;
370
	//
371
	//	  int a;
372
	//	  f(a);
373
	//	  f(&a);
374
	//	  f<int>(a);
375
	//	  f<int>(&a);
376
	//	}
377
	public void testSearchAndTemplateIDs() throws Exception {
378
		PDOMSearchQuery query= makeProjectQuery("CT");
379
		assertOccurrences(query, 5); 
380
		query= makeProjectQuery("f");
381
		assertOccurrences(query, 6);
382
	}
383
360
}
384
}

Return to bug 325385