diff --git a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/search/BasicSearchTest.java b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/search/BasicSearchTest.java index 89d5411b1c0..e65daaa9a48 100644 --- a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/search/BasicSearchTest.java +++ b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/search/BasicSearchTest.java @@ -357,4 +357,28 @@ public class BasicSearchTest extends BaseUITestCase { PDOMSearchResult result= (PDOMSearchResult) query.getSearchResult(); assertEquals(expected, result.getMatchCount()); } + + // template class CT {}; + // template class CT {}; + // template void f(T) {}; + // template void f(T*) {}; + + // void a() { + // CT* r1; + // CT* r2; + // CT* r3; + // + // int a; + // f(a); + // f(&a); + // f(a); + // f(&a); + // } + public void testSearchAndTemplateIDs() throws Exception { + PDOMSearchQuery query= makeProjectQuery("CT"); + assertOccurrences(query, 5); + query= makeProjectQuery("f"); + assertOccurrences(query, 6); + } + } diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/search/LineSearchElement.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/search/LineSearchElement.java index 88cb6fa0c66..098a3998c98 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/search/LineSearchElement.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/search/LineSearchElement.java @@ -77,7 +77,10 @@ public class LineSearchElement extends PDOMSearchElement { private static final class MatchesComparator implements Comparator { public int compare(Match m1, Match m2) { - return m1.getOffset() - m2.getOffset(); + int diff= m1.getOffset() - m2.getOffset(); + if (diff == 0) + diff= m2.getLength() -m1.getLength(); + return diff; } } @@ -158,47 +161,88 @@ public class LineSearchElement extends PDOMSearchElement { public static LineSearchElement[] createElements(IIndexFileLocation fileLocation, Match[] matches, IDocument document) { - // sort matches according to their offsets + // Sort matches according to their offsets Arrays.sort(matches, MATCHES_COMPARATOR); - // group all matches by lines and create LineSearchElements + // Group all matches by lines and create LineSearchElements List result = new ArrayList(); - int firstMatch = 0; - while (firstMatch < matches.length) { - try { - int lineNumber = document.getLineOfOffset(matches[firstMatch].getOffset()); - int lineOffset = document.getLineOffset(lineNumber); - int lineLength = document.getLineLength(lineNumber); - int nextlineOffset = lineOffset + lineLength; - int nextMatch = firstMatch; - int nextMatchOffset = matches[nextMatch].getOffset(); - while (nextMatch < matches.length && nextMatchOffset < nextlineOffset) { - nextMatch++; - if (nextMatch < matches.length) - nextMatchOffset = matches[nextMatch].getOffset(); - } - int lineMatchesCount = nextMatch - firstMatch; - Match[] lineMatches = new Match[lineMatchesCount]; - System.arraycopy(matches, firstMatch, lineMatches, 0, lineMatchesCount); - String content = document.get(lineOffset, lineLength); - result.add(new LineSearchElement(fileLocation, lineMatches, lineNumber + 1, content, lineOffset)); - firstMatch = nextMatch; - } catch (BadLocationException e) { - CUIPlugin.log(e); + List matchCollector= new ArrayList(); + int minOffset = 0; + int lineNumber = 0; + int lineOffset = 0; + int lineLength = 0; + int lineEndOffset = 0; + + try { + for (final Match match : matches) { + final int offset= match.getOffset(); + if (offset < lineEndOffset) { + // Match on same line + if (offset < minOffset) { + // Match is not overlapped by previous one. + matchCollector.add(match); + minOffset= offset + match.getLength(); + } + } else { + // Match is on a new line + if (!matchCollector.isEmpty()) { + // Complete a line + String content = document.get(lineOffset, lineLength); + Match[] lineMatches= matchCollector.toArray(new Match[matchCollector.size()]); + result.add(new LineSearchElement(fileLocation, lineMatches, lineNumber + 1, content, lineOffset)); + matchCollector.clear(); + } + // Setup next line + lineNumber = document.getLineOfOffset(offset); + lineOffset = document.getLineOffset(lineNumber); + lineLength = document.getLineLength(lineNumber); + lineEndOffset = lineOffset + lineLength; + matchCollector.add(match); + } } + if (!matchCollector.isEmpty()) { + // Complete a line + String content = document.get(lineOffset, lineLength); + Match[] lineMatches= matchCollector.toArray(new Match[matchCollector.size()]); + result.add(new LineSearchElement(fileLocation, lineMatches, lineNumber + 1, content, lineOffset)); + matchCollector.clear(); + } + } catch (BadLocationException e) { + CUIPlugin.log(e); } return result.toArray(new LineSearchElement[result.size()]); } private static LineSearchElement[] collectLineElements(AbstractCharArray buf, Match[] matches, IIndexFileLocation fileLocation) { + List result = new ArrayList(); + List matchCollector= new ArrayList(); + boolean skipLF = false; int lineNumber = 1; int lineOffset = 0; - int lineFirstMatch = -1; // not matched - int nextMatch = 0; - int nextMatchOffset = matches[nextMatch].getOffset(); + int i = 0; + Match match= matches[i]; + int matchOffset = match.getOffset(); for (int pos = 0; buf.isValidOffset(pos); pos++) { + if (matchOffset <= pos && match != null) { + // We are on the line of the match, store it. + matchCollector.add(match); + final int minOffset= matchOffset + match.getLength(); + match= null; + matchOffset= Integer.MAX_VALUE; + for(i=i+1; i= minOffset) { + match= nextMatch; + matchOffset= nextOffset; + break; + } + } + } + char c = buf.get(pos); // consider '\n' and '\r' if (skipLF) { @@ -209,22 +253,18 @@ public class LineSearchElement extends PDOMSearchElement { } } if (c == '\n' || c == '\r') { - // create new LineElement if there were matches - if (lineFirstMatch != -1) { + // Create new LineElement for collected matches on this line + if (!matchCollector.isEmpty()) { int lineLength = pos - lineOffset; - int lineMatchesCount = nextMatch - lineFirstMatch; - Match[] lineMatches = new Match[lineMatchesCount]; - System.arraycopy(matches, lineFirstMatch, lineMatches, 0, lineMatchesCount); + Match[] lineMatches= matchCollector.toArray(new Match[matchCollector.size()]); char[] lineChars= new char[lineLength]; buf.arraycopy(lineOffset, lineChars, 0, lineLength); String lineContent = new String(lineChars); result.add(new LineSearchElement(fileLocation, lineMatches, lineNumber, lineContent, lineOffset)); - lineFirstMatch = -1; - if (nextMatch >= matches.length) + matchCollector.clear(); + if (match == null) break; - if (matches[nextMatch].getOffset() < pos) - lineFirstMatch = nextMatch; } lineNumber++; lineOffset = pos + 1; @@ -232,31 +272,16 @@ public class LineSearchElement extends PDOMSearchElement { skipLF = true; continue; } - // compare offset of next match with current position - if (nextMatchOffset > pos || nextMatch >= matches.length) - continue; - // next match was reached - // check if this match is the first for current line - if (lineFirstMatch == -1) - lineFirstMatch = nextMatch; - // goto to next match - nextMatch++; - if (nextMatch < matches.length) { - // update offset of next match - nextMatchOffset = matches[nextMatch].getOffset(); - } } - // check if there were matches on the last line - if (lineFirstMatch != -1) { + // Create new LineElement for matches on the last line + if (!matchCollector.isEmpty()) { int lineLength = buf.getLength() - lineOffset; - int lineMatchesCount = nextMatch - lineFirstMatch; - Match[] lineMatches = new Match[lineMatchesCount]; - System.arraycopy(matches, lineFirstMatch, lineMatches, 0, lineMatchesCount); - + Match[] lineMatches= matchCollector.toArray(new Match[matchCollector.size()]); char[] lineChars= new char[lineLength]; buf.arraycopy(lineOffset, lineChars, 0, lineLength); String lineContent = new String(lineChars); - result.add(new LineSearchElement(fileLocation, lineMatches, lineNumber, lineContent, lineOffset)); + result.add(new LineSearchElement(fileLocation, lineMatches, lineNumber, lineContent, + lineOffset)); } return result.toArray(new LineSearchElement[result.size()]); } diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/search/PDOMSearchPatternQuery.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/search/PDOMSearchPatternQuery.java index 46f174dabd6..734437da53e 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/search/PDOMSearchPatternQuery.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/search/PDOMSearchPatternQuery.java @@ -145,11 +145,7 @@ public class PDOMSearchPatternQuery extends PDOMSearchQuery { for (int i = 0; i < bindings.length; ++i) { IIndexBinding pdomBinding = bindings[i]; - //check for the element type of this binding and create matches if - //the element type checkbox is checked in the C/C++ Search Page - - //TODO search for macro - + // Select the requested bindings boolean matches= false; if ((flags & FIND_ALL_TYPES) == FIND_ALL_TYPES) { matches= true; diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/search/PDOMSearchQuery.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/search/PDOMSearchQuery.java index 014666bd4e5..83e89b0904a 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/search/PDOMSearchQuery.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/search/PDOMSearchQuery.java @@ -21,8 +21,8 @@ import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; -import java.util.Set; import java.util.Map.Entry; +import java.util.Set; import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.IPath; @@ -310,14 +310,20 @@ public abstract class PDOMSearchQuery implements ISearchQuery { return; List names= new ArrayList(); List polymorphicNames= null; + HashSet handled= new HashSet(); + for (IBinding binding : bindings) { - if (binding != null) { + if (binding != null && handled.add(binding)) { createMatches1(index, binding, names); - - if ((flags & FIND_REFERENCES) != 0) { + } + } + + if ((flags & FIND_REFERENCES) != 0) { + for (IBinding binding : bindings) { + if (binding != null) { List specializations = IndexUI.findSpecializations(binding); - if (!specializations.isEmpty()) { - for (IBinding spec : specializations) { + for (IBinding spec : specializations) { + if (spec != null && handled.add(spec)) { createMatches1(index, spec, names); } } @@ -331,7 +337,9 @@ public abstract class PDOMSearchQuery implements ISearchQuery { polymorphicNames= new ArrayList(); } for (ICPPMethod mInBase : msInBases) { - createMatches1(index, mInBase, polymorphicNames); + if (mInBase != null && handled.add(mInBase)) { + createMatches1(index, mInBase, polymorphicNames); + } } } } catch (DOMException e) {