1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-07-23 17:05:26 +02:00

Patch for Andrew Niefer:

core:
- created new search pattern OrPattern, which returns a match if any of 
its constituent pattens return a match.

To use it, do something like:
OrPattern orPattern = new OrPattern();
orPattern.addPattern( SearchEngine.createSearchPattern( "::NS::B::e", 
ENUM, REFERENCES, true ) );
orPattern.addPattern( SearchEngine.createSearchPattern( "Hea*", CLASS, 
DECLARATIONS, true ) );

Searching for all occurences of something now uses the OrPattern. ie, 
SearchEngine.createSearchPattern( "A", TYPE, ALL_OCCURENCES, true );
is the same as
OrPattern orPattern = new OrPattern();
orPattern.addPattern( SearchEngine.createSearchPattern( "f", FUNCTION, 
DECLARATIONS, true ) );
orPattern.addPattern( SearchEngine.createSearchPattern( "f", FUNCTION, 
REFERENCES, true ) );
orPattern.addPattern( SearchEngine.createSearchPattern( "f", FUNCTION, 
DEFINITIONS, true ) );

For large projects this is much more efficient than the old method of 
finding all occurences

core.tests:
- added ClassDeclarationPatternTests.testAllOccurences
- added OtherPatternTests.testOrPattern
This commit is contained in:
Doug Schaefer 2003-08-07 14:52:18 +00:00
parent 65003d3ab2
commit b97611936d
13 changed files with 261 additions and 52 deletions

View file

@ -23,6 +23,7 @@ import org.eclipse.cdt.core.search.SearchEngine;
import org.eclipse.cdt.internal.core.search.CharOperation;
import org.eclipse.cdt.internal.core.search.matching.ClassDeclarationPattern;
import org.eclipse.cdt.internal.core.search.matching.MatchLocator;
import org.eclipse.cdt.internal.core.search.matching.OrPattern;
/**
@ -246,4 +247,14 @@ public class ClassDeclarationPatternTests extends BaseSearchTest implements ICSe
assertEquals( matches.size(), 2 );
}
public void testAllOccurences(){
ICSearchPattern pattern = SearchEngine.createSearchPattern( "A", TYPE, ALL_OCCURRENCES, true );
assertTrue( pattern instanceof OrPattern );
search( workspace, pattern, scope, resultCollector );
Set matches = resultCollector.getSearchResults();
assertEquals( matches.size(), 6 );
}
}

View file

@ -21,6 +21,7 @@ import org.eclipse.cdt.core.search.SearchEngine;
import org.eclipse.cdt.internal.core.search.CharOperation;
import org.eclipse.cdt.internal.core.search.matching.FieldDeclarationPattern;
import org.eclipse.cdt.internal.core.search.matching.NamespaceDeclarationPattern;
import org.eclipse.cdt.internal.core.search.matching.OrPattern;
import org.eclipse.cdt.internal.core.search.matching.VariableDeclarationPattern;
/**
@ -145,5 +146,27 @@ public class OtherPatternTests extends BaseSearchTest {
IMatch match = (IMatch) matches.iterator().next();
assertTrue( match.getParentName().equals( "" ) );
}
public void testOrPattern(){
OrPattern orPattern = new OrPattern();
orPattern.addPattern( SearchEngine.createSearchPattern( "::NS::B::e", ENUM, REFERENCES, true ) );
orPattern.addPattern( SearchEngine.createSearchPattern( "Hea*", CLASS, DECLARATIONS, true ) );
search( workspace, orPattern, scope, resultCollector );
Set matches = resultCollector.getSearchResults();
assertEquals( matches.size(), 3 );
orPattern = new OrPattern();
orPattern.addPattern( SearchEngine.createSearchPattern( "b?", VAR, DECLARATIONS, true ) );
orPattern.addPattern( SearchEngine.createSearchPattern( "a*Struct", FIELD, DECLARATIONS, true ) );
orPattern.addPattern( SearchEngine.createSearchPattern( "::NS::NS2", NAMESPACE, REFERENCES, true ) );
orPattern.addPattern( SearchEngine.createSearchPattern( "A::B::f( A )", METHOD, DECLARATIONS, true ) );
search( workspace, orPattern, scope, resultCollector );
matches = resultCollector.getSearchResults();
assertEquals( matches.size(), 6 );
}
}

View file

@ -1,3 +1,8 @@
2003-08-06 Andrew Niefer
- Create OrPattern which matches for search if any of its constituent patterns matches
- modified MatchLocator to support the OrPattern
- searching for All occurences now uses the OrPattern
2003-08-01 Andrew Niefer
- Modified BasicSearchResultCollector to only accept matches it has not already seen
- fixed bug in finding a resource when entering includes

View file

@ -32,7 +32,8 @@ public interface ICSearchPattern extends ICSearchConstants{
* @param node
* @return
*/
int matchLevel( ISourceElementCallbackDelegate node );
int matchLevel( ISourceElementCallbackDelegate node, LimitTo limit );
LimitTo getLimitTo();
LimitTo getLimitTo();
boolean canAccept( LimitTo limit );
}

View file

@ -57,13 +57,17 @@ public abstract class CSearchPattern implements ICSearchConstants, ICSearchPatte
_caseSensitive = caseSensitive;
_limitTo = limitTo;
}
public CSearchPattern() {
super();
}
public LimitTo getLimitTo(){
return _limitTo;
}
public CSearchPattern() {
super();
public boolean canAccept(LimitTo limit) {
return ( limit == getLimitTo() );
}
public static CSearchPattern createPattern( String patternString, SearchFor searchFor, LimitTo limitTo, int matchMode, boolean caseSensitive ){
@ -97,6 +101,13 @@ public abstract class CSearchPattern implements ICSearchConstants, ICSearchPatte
* @return
*/
private static CSearchPattern createNamespacePattern(String patternString, LimitTo limitTo, int matchMode, boolean caseSensitive) {
if( limitTo == ALL_OCCURRENCES ){
OrPattern orPattern = new OrPattern();
orPattern.addPattern( createNamespacePattern( patternString, DECLARATIONS, matchMode, caseSensitive ) );
orPattern.addPattern( createNamespacePattern( patternString, REFERENCES, matchMode, caseSensitive ) );
return orPattern;
}
IScanner scanner = ParserFactory.createScanner( new StringReader( patternString ), "TEXT", new ScannerInfo(), ParserMode.QUICK_PARSE, null );
LinkedList list = scanForNames( scanner, null );
@ -114,6 +125,14 @@ public abstract class CSearchPattern implements ICSearchConstants, ICSearchPatte
* @return
*/
private static CSearchPattern createFunctionPattern(String patternString, LimitTo limitTo, int matchMode, boolean caseSensitive) {
if( limitTo == ALL_OCCURRENCES ){
OrPattern orPattern = new OrPattern();
orPattern.addPattern( createFunctionPattern( patternString, DECLARATIONS, matchMode, caseSensitive ) );
orPattern.addPattern( createFunctionPattern( patternString, REFERENCES, matchMode, caseSensitive ) );
orPattern.addPattern( createFunctionPattern( patternString, DEFINITIONS, matchMode, caseSensitive ) );
return orPattern;
}
int index = patternString.indexOf( '(' );
String paramString = ( index == -1 ) ? "" : patternString.substring( index );
@ -139,6 +158,12 @@ public abstract class CSearchPattern implements ICSearchConstants, ICSearchPatte
* @return
*/
private static CSearchPattern createVariablePattern(String patternString, LimitTo limitTo, int matchMode, boolean caseSensitive) {
if( limitTo == ALL_OCCURRENCES ){
OrPattern orPattern = new OrPattern();
orPattern.addPattern( createVariablePattern( patternString, DECLARATIONS, matchMode, caseSensitive ) );
orPattern.addPattern( createVariablePattern( patternString, REFERENCES, matchMode, caseSensitive ) );
return orPattern;
}
return new VariableDeclarationPattern( patternString.toCharArray(), matchMode, limitTo, caseSensitive );
}
@ -150,6 +175,13 @@ public abstract class CSearchPattern implements ICSearchConstants, ICSearchPatte
* @return
*/
private static CSearchPattern createFieldPattern(String patternString, LimitTo limitTo, int matchMode, boolean caseSensitive) {
if( limitTo == ALL_OCCURRENCES ){
OrPattern orPattern = new OrPattern();
orPattern.addPattern( createFieldPattern( patternString, DECLARATIONS, matchMode, caseSensitive ) );
orPattern.addPattern( createFieldPattern( patternString, REFERENCES, matchMode, caseSensitive ) );
return orPattern;
}
IScanner scanner = ParserFactory.createScanner( new StringReader( patternString ), "TEXT", new ScannerInfo(), ParserMode.QUICK_PARSE, null );
LinkedList list = scanForNames( scanner, null );
@ -167,7 +199,15 @@ public abstract class CSearchPattern implements ICSearchConstants, ICSearchPatte
* @return
*/
private static CSearchPattern createMethodPattern(String patternString, LimitTo limitTo, int matchMode, boolean caseSensitive) {
if( limitTo == ALL_OCCURRENCES ){
OrPattern orPattern = new OrPattern();
orPattern.addPattern( createMethodPattern( patternString, DECLARATIONS, matchMode, caseSensitive ) );
orPattern.addPattern( createMethodPattern( patternString, REFERENCES, matchMode, caseSensitive ) );
orPattern.addPattern( createMethodPattern( patternString, DEFINITIONS, matchMode, caseSensitive ) );
return orPattern;
}
int index = patternString.indexOf( '(' );
String paramString = ( index == -1 ) ? "" : patternString.substring( index );
String nameString = ( index == -1 ) ? patternString : patternString.substring( 0, index );
@ -197,6 +237,14 @@ public abstract class CSearchPattern implements ICSearchConstants, ICSearchPatte
* @return
*/
private static CSearchPattern createClassPattern(String patternString, SearchFor searchFor, LimitTo limitTo, int matchMode, boolean caseSensitive) {
if( limitTo == ALL_OCCURRENCES ){
OrPattern orPattern = new OrPattern();
orPattern.addPattern( createClassPattern( patternString, searchFor, DECLARATIONS, matchMode, caseSensitive ) );
orPattern.addPattern( createClassPattern( patternString, searchFor, REFERENCES, matchMode, caseSensitive ) );
return orPattern;
}
IScanner scanner = ParserFactory.createScanner( new StringReader( patternString ), "TEXT", new ScannerInfo(), ParserMode.QUICK_PARSE, null );
IToken token = null;

View file

@ -57,10 +57,13 @@ public class ClassDeclarationPattern extends CSearchPattern {
classKind = kind;
}
public int matchLevel( ISourceElementCallbackDelegate node ){
public int matchLevel( ISourceElementCallbackDelegate node, LimitTo limit ){
if( !( node instanceof IASTClassSpecifier ) && !( node instanceof IASTEnumerationSpecifier ) )
return IMPOSSIBLE_MATCH;
if( ! canAccept( limit ) )
return IMPOSSIBLE_MATCH;
String nodeName = ((IASTOffsetableNamedElement)node).getName();
@ -205,5 +208,4 @@ public class ClassDeclarationPattern extends CSearchPattern {
return true;
}
}

View file

@ -48,12 +48,12 @@ public class FieldDeclarationPattern extends VariableDeclarationPattern {
}
public int matchLevel(ISourceElementCallbackDelegate node) {
public int matchLevel(ISourceElementCallbackDelegate node, LimitTo limit ) {
if( !(node instanceof IASTField) ){
return IMPOSSIBLE_MATCH;
}
if( super.matchLevel( node ) == IMPOSSIBLE_MATCH ){
if( super.matchLevel( node, limit ) == IMPOSSIBLE_MATCH ){
return IMPOSSIBLE_MATCH;
}

View file

@ -55,8 +55,8 @@ public class FunctionDeclarationPattern extends CSearchPattern {
/* (non-Javadoc)
* @see org.eclipse.cdt.core.search.ICSearchPattern#matchLevel(org.eclipse.cdt.core.parser.ast.IASTOffsetableElement)
*/
public int matchLevel(ISourceElementCallbackDelegate node) {
if( !( node instanceof IASTFunction ) )
public int matchLevel(ISourceElementCallbackDelegate node, LimitTo limit ) {
if( !( node instanceof IASTFunction ) || !canAccept( limit ) )
return IMPOSSIBLE_MATCH;
IASTFunction function = (IASTFunction) node;

View file

@ -97,7 +97,6 @@ public class MatchLocator implements ISourceElementRequestor, ICSearchConstants
}
public void acceptProblem(IProblem problem) { }
public void acceptMacro(IASTMacro macro) { }
public void acceptUsingDirective(IASTUsingDirective usageDirective) { }
public void acceptUsingDeclaration(IASTUsingDeclaration usageDeclaration) { }
public void acceptASMDefinition(IASTASMDefinition asmDefinition) { }
@ -116,65 +115,69 @@ public class MatchLocator implements ISourceElementRequestor, ICSearchConstants
public void exitTemplateExplicitInstantiation(IASTTemplateInstantiation instantiation) { }
public void exitLinkageSpecification(IASTLinkageSpecification linkageSpec) { }
public void acceptMacro(IASTMacro macro){
check( DECLARATIONS, macro );
}
public void acceptVariable(IASTVariable variable){
check( DECLARATIONS, VariableDeclarationPattern.class, variable );
check( DECLARATIONS, variable );
}
public void acceptField(IASTField field){
check( DECLARATIONS, FieldDeclarationPattern.class, field );
check( DECLARATIONS, field );
}
public void acceptEnumerationSpecifier(IASTEnumerationSpecifier enumeration){
check( DECLARATIONS, ClassDeclarationPattern.class, enumeration );
check( DECLARATIONS, enumeration );
Iterator iter = enumeration.getEnumerators();
while( iter.hasNext() ){
check ( DECLARATIONS, FieldDeclarationPattern.class, (ISourceElementCallbackDelegate) iter.next() );
check ( DECLARATIONS, (ISourceElementCallbackDelegate) iter.next() );
}
}
public void acceptFunctionDeclaration(IASTFunction function){
check( DECLARATIONS, FunctionDeclarationPattern.class, function );
check( DECLARATIONS, function );
}
public void acceptMethodDeclaration(IASTMethod method){
check( DECLARATIONS, MethodDeclarationPattern.class, method );
check( DECLARATIONS, method );
}
public void acceptClassReference(IASTClassReference reference) {
check( REFERENCES, ClassDeclarationPattern.class, reference );
check( REFERENCES, reference );
}
public void acceptNamespaceReference( IASTNamespaceReference reference ){
check( REFERENCES, NamespaceDeclarationPattern.class, reference );
check( REFERENCES, reference );
}
public void acceptVariableReference( IASTVariableReference reference ){
check( REFERENCES, VariableDeclarationPattern.class, reference );
check( REFERENCES, reference );
}
public void acceptFieldReference( IASTFieldReference reference ){
check( REFERENCES, FieldDeclarationPattern.class, reference );
check( REFERENCES, reference );
}
public void acceptEnumerationReference( IASTEnumerationReference reference ){
check( REFERENCES, ClassDeclarationPattern.class, reference );
check( REFERENCES, reference );
}
public void acceptFunctionReference( IASTFunctionReference reference ){
check( REFERENCES, FunctionDeclarationPattern.class, reference );
check( REFERENCES, reference );
}
public void acceptMethodReference( IASTMethodReference reference ){
check( REFERENCES, MethodDeclarationPattern.class, reference );
check( REFERENCES, reference );
}
public void enterFunctionBody(IASTFunction function){
check( DEFINITIONS, FunctionDeclarationPattern.class, function );
check( DEFINITIONS, function );
pushScope( function );
}
public void enterMethodBody(IASTMethod method) {
check( DEFINITIONS, MethodDeclarationPattern.class, method );
check( DEFINITIONS, method );
pushScope( method );
}
@ -183,12 +186,12 @@ public class MatchLocator implements ISourceElementRequestor, ICSearchConstants
}
public void enterNamespaceDefinition(IASTNamespaceDefinition namespaceDefinition) {
check( DECLARATIONS, NamespaceDeclarationPattern.class, namespaceDefinition );
check( DECLARATIONS, namespaceDefinition );
pushScope( namespaceDefinition );
}
public void enterClassSpecifier(IASTClassSpecifier classSpecification) {
check( DECLARATIONS, ClassDeclarationPattern.class, classSpecification );
check( DECLARATIONS, classSpecification );
pushScope( classSpecification );
}
@ -366,23 +369,21 @@ public class MatchLocator implements ISourceElementRequestor, ICSearchConstants
}
}
private void check( LimitTo limit, Class patternClass, ISourceElementCallbackDelegate node ){
if( searchPattern.getLimitTo() != limit && searchPattern.getLimitTo() != ALL_OCCURRENCES )
private void check( LimitTo limit, ISourceElementCallbackDelegate node ){
if( !searchPattern.canAccept( limit ) )
return;
if( searchPattern.getClass() == patternClass ){
int level = ICSearchPattern.IMPOSSIBLE_MATCH;
if( node instanceof IASTReference ){
level = searchPattern.matchLevel( ((IASTReference)node).getReferencedElement() );
} else {
level = searchPattern.matchLevel( node );
}
if( level != ICSearchPattern.IMPOSSIBLE_MATCH )
{
report( node, level );
}
int level = ICSearchPattern.IMPOSSIBLE_MATCH;
if( node instanceof IASTReference ){
level = searchPattern.matchLevel( ((IASTReference)node).getReferencedElement(), limit );
} else {
level = searchPattern.matchLevel( node, limit );
}
if( level != ICSearchPattern.IMPOSSIBLE_MATCH )
{
report( node, level );
}
}

View file

@ -45,12 +45,12 @@ public class MethodDeclarationPattern extends FunctionDeclarationPattern {
}
public int matchLevel(ISourceElementCallbackDelegate node) {
if( !(node instanceof IASTMethod) ){
public int matchLevel(ISourceElementCallbackDelegate node, LimitTo limit ) {
if( !(node instanceof IASTMethod) || !canAccept( limit ) ){
return IMPOSSIBLE_MATCH;
}
if( super.matchLevel( node ) == IMPOSSIBLE_MATCH ){
if( super.matchLevel( node, limit ) == IMPOSSIBLE_MATCH ){
return IMPOSSIBLE_MATCH;
}

View file

@ -51,8 +51,8 @@ public class NamespaceDeclarationPattern extends CSearchPattern {
/* (non-Javadoc)
* @see org.eclipse.cdt.core.search.ICSearchPattern#matchLevel(org.eclipse.cdt.core.parser.ast.IASTOffsetableElement)
*/
public int matchLevel(ISourceElementCallbackDelegate node) {
if( !( node instanceof IASTNamespaceDefinition ) )
public int matchLevel(ISourceElementCallbackDelegate node, LimitTo limit ) {
if( !( node instanceof IASTNamespaceDefinition ) || !canAccept( limit ) )
return IMPOSSIBLE_MATCH;
IASTNamespaceDefinition namespace = (IASTNamespaceDefinition)node;

View file

@ -0,0 +1,118 @@
/*******************************************************************************
* Copyright (c) 2003 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Common Public License v0.5
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/cpl-v05.html
*
* Contributors:
* IBM Corp. - Rational Software - initial implementation
******************************************************************************/
/*
* Created on Aug 6, 2003
*/
package org.eclipse.cdt.internal.core.search.matching;
import java.io.IOException;
import java.util.Iterator;
import java.util.LinkedList;
import org.eclipse.cdt.core.parser.ISourceElementCallbackDelegate;
import org.eclipse.cdt.core.search.ICSearchPattern;
import org.eclipse.cdt.core.search.ICSearchScope;
import org.eclipse.cdt.internal.core.index.IEntryResult;
import org.eclipse.cdt.internal.core.index.impl.IndexInput;
import org.eclipse.cdt.internal.core.search.IIndexSearchRequestor;
import org.eclipse.core.runtime.IProgressMonitor;
/**
* @author aniefer
*
* To change the template for this generated type comment go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
public class OrPattern extends CSearchPattern {
public OrPattern(){
super();
patterns = new LinkedList();
}
/**
* @param pattern
*/
public void addPattern( ICSearchPattern pattern) {
if( pattern != null )
patterns.add( pattern );
}
public int matchLevel(ISourceElementCallbackDelegate node, LimitTo limit ) {
Iterator iter = patterns.iterator();
int size = patterns.size();
int result = IMPOSSIBLE_MATCH;
for( int i = 0; i < size; i++ ){
ICSearchPattern pattern = (ICSearchPattern) iter.next();
result = pattern.matchLevel( node, limit );
if( result != IMPOSSIBLE_MATCH )
break;
}
return result;
}
public boolean canAccept(LimitTo limit) {
if( limit == ALL_OCCURRENCES ){
return true;
}
Iterator iter = patterns.iterator();
int size = patterns.size();
for( int i = 0; i < size; i++ ){
ICSearchPattern pattern = (ICSearchPattern) iter.next();
if( pattern.canAccept( limit ) )
return true;
}
return false;
}
public void findIndexMatches(IndexInput input, IIndexSearchRequestor requestor, int detailLevel, IProgressMonitor progressMonitor, ICSearchScope scope) throws IOException {
Iterator iter = patterns.iterator();
int size = patterns.size();
for( int i = 0; i < size; i++ ){
CSearchPattern pattern = (CSearchPattern) iter.next();
pattern.findIndexMatches( input, requestor, detailLevel, progressMonitor, scope );
}
}
public void feedIndexRequestor( IIndexSearchRequestor requestor, int detailLevel, int[] references, IndexInput input, ICSearchScope scope )
throws IOException {
//never called for OrPattern
}
protected void resetIndexInfo() {
//never called for OrPattern
}
protected void decodeIndexEntry(IEntryResult entryResult) {
//never called for OrPattern
}
public char[] indexEntryPrefix() {
//never called for OrPattern
return null;
}
protected boolean matchIndexEntry() {
//never called for OrPattern
return false;
}
private LinkedList patterns;
}

View file

@ -49,8 +49,8 @@ public class VariableDeclarationPattern extends CSearchPattern {
/* (non-Javadoc)
* @see org.eclipse.cdt.core.search.ICSearchPattern#matchLevel(org.eclipse.cdt.core.parser.ast.IASTOffsetableElement)
*/
public int matchLevel(ISourceElementCallbackDelegate node) {
if( !(node instanceof IASTVariable) ){
public int matchLevel(ISourceElementCallbackDelegate node, LimitTo limit ) {
if( !(node instanceof IASTVariable) || !canAccept( limit ) ){
return IMPOSSIBLE_MATCH;
}