From bcf023a6c80bfaf770bd00081f423c21f3e149f8 Mon Sep 17 00:00:00 2001 From: Doug Schaefer Date: Fri, 11 Jul 2003 22:12:35 +0000 Subject: [PATCH] Patch for Bogdan Gheorghe: - support for class specifier search - a new C/C++ search menu item. --- core/org.eclipse.cdt.core/index/ChangeLog | 129 ++++++------ .../internal/core/index/impl/IndexInput.java | 1 + .../core/index/impl/IndexerOutput.java | 2 +- .../cdt/internal/core/search/Util.java | 6 +- .../core/search/indexing/AbstractIndexer.java | 109 ++++++++-- .../indexing/AddCompilationUnitToIndex.java | 4 +- .../core/search/indexing/AddFileToIndex.java | 4 +- .../search/indexing/AddFolderToIndex.java | 6 +- .../indexing/RemoveFolderFromIndex.java | 8 +- core/org.eclipse.cdt.core/search/ChangeLog | 77 +++++--- .../cdt/core/search/ICSearchScope.java | 44 +++-- .../eclipse/cdt/core/search/SearchEngine.java | 42 +++- .../internal/core/search/CSearchScope.java | 156 +++++++++++++-- .../internal/core/search/CWorkspaceScope.java | 86 ++++++++ .../core/search/IIndexSearchRequestor.java | 69 +++++++ .../internal/core/search/IndexSelector.java | 118 +++++++++++ .../internal/core/search/PathCollector.java | 131 ++++++++++++ .../core/search/PatternSearchJob.java | 187 ++++++++++++++---- .../core/search/matching/CSearchPattern.java | 92 +++++++-- .../matching/ClassDeclarationPattern.java | 108 +++++++++- .../core/search/processing/JobManager.java | 2 +- 21 files changed, 1159 insertions(+), 222 deletions(-) create mode 100644 core/org.eclipse.cdt.core/search/org/eclipse/cdt/internal/core/search/CWorkspaceScope.java create mode 100644 core/org.eclipse.cdt.core/search/org/eclipse/cdt/internal/core/search/IIndexSearchRequestor.java create mode 100644 core/org.eclipse.cdt.core/search/org/eclipse/cdt/internal/core/search/IndexSelector.java create mode 100644 core/org.eclipse.cdt.core/search/org/eclipse/cdt/internal/core/search/PathCollector.java diff --git a/core/org.eclipse.cdt.core/index/ChangeLog b/core/org.eclipse.cdt.core/index/ChangeLog index cfa741559a0..26cbf97d30d 100644 --- a/core/org.eclipse.cdt.core/index/ChangeLog +++ b/core/org.eclipse.cdt.core/index/ChangeLog @@ -1,62 +1,69 @@ -2003-07-03 Bogdan Gheorghe - Updated copyright notices. - -2003-06-25 Bogdan Gheorghe - Added new Indexer framework: - - * index/org/eclipse/cdt/internal/core/index/IDocument.java - * index/org/eclipse/cdt/internal/core/index/IEntryResult.java - * index/org/eclipse/cdt/internal/core/index/IIndex.java - * index/org/eclipse/cdt/internal/core/index/IIndexer.java - * index/org/eclipse/cdt/internal/core/index/IIndexerOutput.java - * index/org/eclipse/cdt/internal/core/index/IQueryResult.java - - * index/org/eclipse/cdt/internal/core/index/impl/Block.java - * index/org/eclipse/cdt/internal/core/index/impl/BlocksIndexInput.java - * index/org/eclipse/cdt/internal/core/index/impl/BlocksIndexOutput.java - * index/org/eclipse/cdt/internal/core/index/impl/CodeByteStream.java - * index/org/eclipse/cdt/internal/core/index/impl/EntryResult.java - * index/org/eclipse/cdt/internal/core/index/impl/Field.java - * index/org/eclipse/cdt/internal/core/index/impl/GammaCompressedIndexBlock.java - * index/org/eclipse/cdt/internal/core/index/impl/IFileDocument.java - * index/org/eclipse/cdt/internal/core/index/impl/IIndexConstants.java - * index/org/eclipse/cdt/internal/core/index/impl/Index.java - * index/org/eclipse/cdt/internal/core/index/impl/IndexBlock.java - * index/org/eclipse/cdt/internal/core/index/impl/IndexedFile.java - * index/org/eclipse/cdt/internal/core/index/impl/IndexedFileHashedArray.java - * index/org/eclipse/cdt/internal/core/index/impl/IndexerOutput.java - * index/org/eclipse/cdt/internal/core/index/impl/IndexInput.java - * index/org/eclipse/cdt/internal/core/index/impl/IndexOutput.java - * index/org/eclipse/cdt/internal/core/index/impl/IndexSummary.java - * index/org/eclipse/cdt/internal/core/index/impl/InMemoryIndex.java - * index/org/eclipse/cdt/internal/core/index/impl/Int.java - * index/org/eclipse/cdt/internal/core/index/impl/MergeFactory.java - * index/org/eclipse/cdt/internal/core/index/impl/PropertyDocument.java - * index/org/eclipse/cdt/internal/core/index/impl/SafeRandomAccessFile.java - * index/org/eclipse/cdt/internal/core/index/impl/SimpleIndexInput.java - * index/org/eclipse/cdt/internal/core/index/impl/Util.java - * index/org/eclipse/cdt/internal/core/index/impl/WordEntry.java - * index/org/eclipse/cdt/internal/core/index/impl/WordEntryHashedArray.java - - * index/org/eclipse/cdt/internal/core/search/CharOperation.java - * index/org/eclipse/cdt/internal/core/search/HashtableOfInt.java - * index/org/eclipse/cdt/internal/core/search/SimpleLookupTable.java - * index/org/eclipse/cdt/internal/core/search/Util.java - * index/org/eclipse/cdt/internal/core/search/message.properties - - * index/org/eclipse/cdt/internal/core/search/indexing/AbstractIndexer.java - * index/org/eclipse/cdt/internal/core/search/indexing/AddCompilationUnitToIndex.java - * index/org/eclipse/cdt/internal/core/search/indexing/AddFileToIndex.java - * index/org/eclipse/cdt/internal/core/search/indexing/AddFolderToIndex.java - * index/org/eclipse/cdt/internal/core/search/indexing/IIndexConstants.java - * index/org/eclipse/cdt/internal/core/search/indexing/IndexAllProject.java - * index/org/eclipse/cdt/internal/core/search/indexing/IndexManager.java - * index/org/eclipse/cdt/internal/core/search/indexing/IndexRequest.java - * index/org/eclipse/cdt/internal/core/search/indexing/ReadWriteMonitor.java - * index/org/eclipse/cdt/internal/core/search/indexing/RemoveFolderFromIndex.java - * index/org/eclipse/cdt/internal/core/search/indexing/RemoveFromIndex.java - * index/org/eclipse/cdt/internal/core/search/indexing/SaveIndex.java - * index/org/eclipse/cdt/internal/core/search/indexing/SourceIndexer.java - * index/org/eclipse/cdt/internal/core/search/indexing/SourceIndexerRequestor.java - +2003-07-10 Bogdan Gheorghe + Added bestTypeDeclarationPrefix to AbstractIndexer to allow the + search engine to create a query string for the index. + Changed encoding in AbstractIndexer to encode fully qualified names. + + * index/org/eclipse/cdt/internal/core/search/indexing/AbstractIndexer.java + +2003-07-03 Bogdan Gheorghe + Updated copyright notices. + +2003-06-25 Bogdan Gheorghe + Added new Indexer framework: + + * index/org/eclipse/cdt/internal/core/index/IDocument.java + * index/org/eclipse/cdt/internal/core/index/IEntryResult.java + * index/org/eclipse/cdt/internal/core/index/IIndex.java + * index/org/eclipse/cdt/internal/core/index/IIndexer.java + * index/org/eclipse/cdt/internal/core/index/IIndexerOutput.java + * index/org/eclipse/cdt/internal/core/index/IQueryResult.java + + * index/org/eclipse/cdt/internal/core/index/impl/Block.java + * index/org/eclipse/cdt/internal/core/index/impl/BlocksIndexInput.java + * index/org/eclipse/cdt/internal/core/index/impl/BlocksIndexOutput.java + * index/org/eclipse/cdt/internal/core/index/impl/CodeByteStream.java + * index/org/eclipse/cdt/internal/core/index/impl/EntryResult.java + * index/org/eclipse/cdt/internal/core/index/impl/Field.java + * index/org/eclipse/cdt/internal/core/index/impl/GammaCompressedIndexBlock.java + * index/org/eclipse/cdt/internal/core/index/impl/IFileDocument.java + * index/org/eclipse/cdt/internal/core/index/impl/IIndexConstants.java + * index/org/eclipse/cdt/internal/core/index/impl/Index.java + * index/org/eclipse/cdt/internal/core/index/impl/IndexBlock.java + * index/org/eclipse/cdt/internal/core/index/impl/IndexedFile.java + * index/org/eclipse/cdt/internal/core/index/impl/IndexedFileHashedArray.java + * index/org/eclipse/cdt/internal/core/index/impl/IndexerOutput.java + * index/org/eclipse/cdt/internal/core/index/impl/IndexInput.java + * index/org/eclipse/cdt/internal/core/index/impl/IndexOutput.java + * index/org/eclipse/cdt/internal/core/index/impl/IndexSummary.java + * index/org/eclipse/cdt/internal/core/index/impl/InMemoryIndex.java + * index/org/eclipse/cdt/internal/core/index/impl/Int.java + * index/org/eclipse/cdt/internal/core/index/impl/MergeFactory.java + * index/org/eclipse/cdt/internal/core/index/impl/PropertyDocument.java + * index/org/eclipse/cdt/internal/core/index/impl/SafeRandomAccessFile.java + * index/org/eclipse/cdt/internal/core/index/impl/SimpleIndexInput.java + * index/org/eclipse/cdt/internal/core/index/impl/Util.java + * index/org/eclipse/cdt/internal/core/index/impl/WordEntry.java + * index/org/eclipse/cdt/internal/core/index/impl/WordEntryHashedArray.java + + * index/org/eclipse/cdt/internal/core/search/CharOperation.java + * index/org/eclipse/cdt/internal/core/search/HashtableOfInt.java + * index/org/eclipse/cdt/internal/core/search/SimpleLookupTable.java + * index/org/eclipse/cdt/internal/core/search/Util.java + * index/org/eclipse/cdt/internal/core/search/message.properties + + * index/org/eclipse/cdt/internal/core/search/indexing/AbstractIndexer.java + * index/org/eclipse/cdt/internal/core/search/indexing/AddCompilationUnitToIndex.java + * index/org/eclipse/cdt/internal/core/search/indexing/AddFileToIndex.java + * index/org/eclipse/cdt/internal/core/search/indexing/AddFolderToIndex.java + * index/org/eclipse/cdt/internal/core/search/indexing/IIndexConstants.java + * index/org/eclipse/cdt/internal/core/search/indexing/IndexAllProject.java + * index/org/eclipse/cdt/internal/core/search/indexing/IndexManager.java + * index/org/eclipse/cdt/internal/core/search/indexing/IndexRequest.java + * index/org/eclipse/cdt/internal/core/search/indexing/ReadWriteMonitor.java + * index/org/eclipse/cdt/internal/core/search/indexing/RemoveFolderFromIndex.java + * index/org/eclipse/cdt/internal/core/search/indexing/RemoveFromIndex.java + * index/org/eclipse/cdt/internal/core/search/indexing/SaveIndex.java + * index/org/eclipse/cdt/internal/core/search/indexing/SourceIndexer.java + * index/org/eclipse/cdt/internal/core/search/indexing/SourceIndexerRequestor.java + \ No newline at end of file diff --git a/core/org.eclipse.cdt.core/index/org/eclipse/cdt/internal/core/index/impl/IndexInput.java b/core/org.eclipse.cdt.core/index/org/eclipse/cdt/internal/core/index/impl/IndexInput.java index 2d13eb2f7bc..8e0ff8ee06a 100644 --- a/core/org.eclipse.cdt.core/index/org/eclipse/cdt/internal/core/index/impl/IndexInput.java +++ b/core/org.eclipse.cdt.core/index/org/eclipse/cdt/internal/core/index/impl/IndexInput.java @@ -11,6 +11,7 @@ package org.eclipse.cdt.internal.core.index.impl; import java.io.IOException; + import org.eclipse.cdt.internal.core.index.IDocument; import org.eclipse.cdt.internal.core.index.IEntryResult; import org.eclipse.cdt.internal.core.index.IQueryResult; diff --git a/core/org.eclipse.cdt.core/index/org/eclipse/cdt/internal/core/index/impl/IndexerOutput.java b/core/org.eclipse.cdt.core/index/org/eclipse/cdt/internal/core/index/impl/IndexerOutput.java index 55d0925c7e8..ad082274d66 100644 --- a/core/org.eclipse.cdt.core/index/org/eclipse/cdt/internal/core/index/impl/IndexerOutput.java +++ b/core/org.eclipse.cdt.core/index/org/eclipse/cdt/internal/core/index/impl/IndexerOutput.java @@ -10,8 +10,8 @@ *******************************************************************************/ package org.eclipse.cdt.internal.core.index.impl; -import org.eclipse.cdt.internal.core.index.IIndexerOutput; import org.eclipse.cdt.internal.core.index.IDocument; +import org.eclipse.cdt.internal.core.index.IIndexerOutput; /** * An indexerOutput is used by an indexer to add documents and word references to diff --git a/core/org.eclipse.cdt.core/index/org/eclipse/cdt/internal/core/search/Util.java b/core/org.eclipse.cdt.core/index/org/eclipse/cdt/internal/core/search/Util.java index 6b9c6f30ef8..413d2692004 100644 --- a/core/org.eclipse.cdt.core/index/org/eclipse/cdt/internal/core/search/Util.java +++ b/core/org.eclipse.cdt.core/index/org/eclipse/cdt/internal/core/search/Util.java @@ -10,9 +10,6 @@ *******************************************************************************/ package org.eclipse.cdt.internal.core.search; -import java.util.Locale; -import java.util.MissingResourceException; -import java.util.ResourceBundle; import java.io.BufferedInputStream; import java.io.File; import java.io.FileInputStream; @@ -20,6 +17,9 @@ import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.HashSet; +import java.util.Locale; +import java.util.MissingResourceException; +import java.util.ResourceBundle; import org.eclipse.core.resources.IContainer; import org.eclipse.core.resources.IResource; diff --git a/core/org.eclipse.cdt.core/index/org/eclipse/cdt/internal/core/search/indexing/AbstractIndexer.java b/core/org.eclipse.cdt.core/index/org/eclipse/cdt/internal/core/search/indexing/AbstractIndexer.java index 0d5369f3a9f..14e008768d4 100644 --- a/core/org.eclipse.cdt.core/index/org/eclipse/cdt/internal/core/search/indexing/AbstractIndexer.java +++ b/core/org.eclipse.cdt.core/index/org/eclipse/cdt/internal/core/search/indexing/AbstractIndexer.java @@ -33,17 +33,18 @@ public abstract class AbstractIndexer implements IIndexer, IIndexConstants, ICSe } public void addClassSpecifier(IASTClassSpecifier classSpecification){ + if (classSpecification.getClassKind().equals(ASTClassKind.CLASS)) { - this.output.addRef(encodeTypeEntry(classSpecification.getName().toCharArray(),CLASS)); + this.output.addRef(encodeTypeEntry(classSpecification.getFullyQualifiedName(),CLASS)); } else if (classSpecification.getClassKind().equals(ASTClassKind.STRUCT)) { - this.output.addRef(encodeTypeEntry(classSpecification.getName().toCharArray(),STRUCT)); + this.output.addRef(encodeTypeEntry(classSpecification.getFullyQualifiedName(),STRUCT)); } else if (classSpecification.getClassKind().equals(ASTClassKind.UNION)) { - this.output.addRef(encodeTypeEntry(classSpecification.getName().toCharArray(),UNION)); + this.output.addRef(encodeTypeEntry(classSpecification.getFullyQualifiedName(),UNION)); } } @@ -83,11 +84,14 @@ public abstract class AbstractIndexer implements IIndexer, IIndexConstants, ICSe /** * Type entries are encoded as follow: 'typeDecl/' ('C' | 'S' | 'U' ) '/' TypeName '/' */ - protected static final char[] encodeTypeEntry( char[] typeName, int classType) { - - int pos; - //3 - 1 for SUFFIX letter, 2 Separators - char[] result = new char[TYPE_DECL_LENGTH + typeName.length + 3]; + protected static final char[] encodeTypeEntry( String [] fullTypeName, int classType) { + int pos, nameLength = 0; + for (int i=0; i 0){ + //Extract the name first + char [] tempName = fullTypeName[fullTypeName.length-1].toCharArray(); + System.arraycopy(tempName, 0, result, pos, tempName.length); + pos += tempName.length; + } + //Extract the qualifiers + for (int i=0; i<(fullTypeName.length - 1); i++){ + result[pos++] = SEPARATOR; + char [] tempName = fullTypeName[i].toCharArray(); + System.arraycopy(tempName, 0, result, pos, tempName.length); + pos+=tempName.length; + } return result; } /** @@ -136,5 +150,74 @@ public abstract class AbstractIndexer implements IIndexer, IIndexConstants, ICSe } return false; } + /** + * Type entries are encoded as follow: 'typeDecl/' ('C' | 'S' | 'U' ) '/' TypeName '/' + * Current encoding is optimized for queries: all classes + */ + public static final char[] bestTypeDeclarationPrefix(char[] typeName, char[][] containingTypes, ASTClassKind classKind, int matchMode, boolean isCaseSensitive) { + // index is case sensitive, thus in case attempting case insensitive search, cannot consider + // type name. + if (!isCaseSensitive){ + typeName = null; + } + //Class kind not provided, best we can do + if (classKind == null){ + return TYPE_DECL; + } + + char classType=CLASS_SUFFIX; + if (classKind == ASTClassKind.STRUCT){ + classType = STRUCT_SUFFIX; + } + else if (classKind == ASTClassKind.UNION){ + classType = UNION_SUFFIX; + } + + switch(matchMode){ + case EXACT_MATCH : + case PREFIX_MATCH : + break; + case PATTERN_MATCH : + if (typeName != null){ + int starPos = CharOperation.indexOf('*', typeName); + switch(starPos) { + case -1 : + break; + case 0 : + typeName = null; + break; + default : + typeName = CharOperation.subarray(typeName, 0, starPos); + } + } + } + + int containingTypesLength=0; + int typeLength = typeName == null ? 0 : typeName.length; + int pos; + //figure out the total length of the qualifiers + for (int i=0; i 0){ + System.arraycopy(typeName, 0, result, pos, typeName.length); + pos += typeName.length; + } + + for (int i=0; iPreferences>Java>Code Generation>Code and Comments - */ +import org.eclipse.cdt.core.model.ICElement; +import org.eclipse.core.runtime.IPath; + public interface ICSearchScope { + /** + * Checks whether the resource at the given path is enclosed by this scope. + * + * @param resourcePath if the resource is contained in + * @return whether the resource is enclosed by this scope + */ + public boolean encloses(String resourcePath); + /** + * Checks whether this scope encloses the given element. + * + * @param element the given element + * @return true if the element is in this scope + */ + public boolean encloses(ICElement element); + /** + * Returns the paths to the enclosing projects for this search scope. + *
    + *
  • If the path is a project path, this is the full path of the project + * (see IResource.getFullPath()). + * For example, /MyProject + *
  • + * + * @return an array of paths to the enclosing projects. + */ + IPath[] enclosingProjects(); } diff --git a/core/org.eclipse.cdt.core/search/org/eclipse/cdt/core/search/SearchEngine.java b/core/org.eclipse.cdt.core/search/org/eclipse/cdt/core/search/SearchEngine.java index f9e7ccbe282..21e99914e7c 100644 --- a/core/org.eclipse.cdt.core/search/org/eclipse/cdt/core/search/SearchEngine.java +++ b/core/org.eclipse.cdt.core/search/org/eclipse/cdt/core/search/SearchEngine.java @@ -1,23 +1,26 @@ /******************************************************************************* - * Copyright (c) 2003 IBM Corporation and others. + * Copyright (c) 2000, 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 + * are made available under the terms of the Common Public License v1.0 * which accompanies this distribution, and is available at - * http://www.eclipse.org/legal/cpl-v05.html + * http://www.eclipse.org/legal/cpl-v10.html * * Contributors: - * IBM Corp. - Rational Software - initial implementation - ******************************************************************************/ + * IBM Corporation - initial API and implementation + *******************************************************************************/ /* * Created on Jun 11, 2003 */ package org.eclipse.cdt.core.search; import org.eclipse.cdt.core.model.ICElement; -import org.eclipse.cdt.internal.core.search.indexing.IndexManager; import org.eclipse.cdt.internal.core.model.CModelManager; +import org.eclipse.cdt.internal.core.model.IWorkingCopy; +import org.eclipse.cdt.internal.core.search.CWorkspaceScope; +import org.eclipse.cdt.internal.core.search.PathCollector; import org.eclipse.cdt.internal.core.search.PatternSearchJob; import org.eclipse.cdt.internal.core.search.Util; +import org.eclipse.cdt.internal.core.search.indexing.IndexManager; import org.eclipse.cdt.internal.core.search.matching.CSearchPattern; import org.eclipse.cdt.internal.core.search.matching.MatchLocator; import org.eclipse.core.resources.IWorkspace; @@ -35,6 +38,12 @@ public class SearchEngine implements ICSearchConstants{ private boolean VERBOSE = false; + /** + * A list of working copies that take precedence over their original + * compilation units. + */ + private IWorkingCopy[] workingCopies = null; + /** * */ @@ -43,12 +52,16 @@ public class SearchEngine implements ICSearchConstants{ // TODO Auto-generated constructor stub } + public SearchEngine(IWorkingCopy[] workingCopies) { + this.workingCopies = workingCopies; + } + /** * @return */ public static ICSearchScope createWorkspaceScope() { // TODO Auto-generated method stub - return null; + return new CWorkspaceScope(); } /** @@ -97,13 +110,21 @@ public class SearchEngine implements ICSearchConstants{ progressMonitor.beginTask( Util.bind("engine.searching"), 100 ); //$NON_NLS-1$ } + /* index search */ + PathCollector pathCollector = new PathCollector(); + CModelManager modelManager = CModelManager.getDefault(); IndexManager indexManager = modelManager.getIndexManager(); SubProgressMonitor subMonitor = (progressMonitor == null ) ? null : new SubProgressMonitor( progressMonitor, 5 ); - + indexManager.performConcurrentJob( - new PatternSearchJob(), + new PatternSearchJob( + (CSearchPattern) pattern, + scope, + pathCollector, + indexManager + ), ICSearchConstants.WAIT_UNTIL_READY_TO_SEARCH, subMonitor ); @@ -114,7 +135,8 @@ public class SearchEngine implements ICSearchConstants{ if( progressMonitor != null && progressMonitor.isCanceled() ) throw new OperationCanceledException(); - //matchLocator.locateMatches( pathCollector.getPaths(), workspace, workingCopies ); + //TODO: BOG Filter Working Copies... + matchLocator.locateMatches( pathCollector.getPaths(), workspace, this.workingCopies); } finally { collector.done(); } diff --git a/core/org.eclipse.cdt.core/search/org/eclipse/cdt/internal/core/search/CSearchScope.java b/core/org.eclipse.cdt.core/search/org/eclipse/cdt/internal/core/search/CSearchScope.java index 130e64a893c..436725c3742 100644 --- a/core/org.eclipse.cdt.core/search/org/eclipse/cdt/internal/core/search/CSearchScope.java +++ b/core/org.eclipse.cdt.core/search/org/eclipse/cdt/internal/core/search/CSearchScope.java @@ -1,34 +1,152 @@ /******************************************************************************* - * Copyright (c) 2003 IBM Corporation and others. + * Copyright (c) 2000, 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 + * are made available under the terms of the Common Public License v1.0 * which accompanies this distribution, and is available at - * http://www.eclipse.org/legal/cpl-v05.html + * http://www.eclipse.org/legal/cpl-v10.html * * Contributors: - * IBM Corp. - Rational Software - initial implementation - ******************************************************************************/ -/* - * Created on Jun 12, 2003 - */ + * IBM Corporation - initial API and implementation + *******************************************************************************/ + package org.eclipse.cdt.internal.core.search; -import org.eclipse.cdt.core.search.ICSearchScope; +import java.util.ArrayList; +import java.util.HashSet; + +import org.eclipse.cdt.core.model.ICElement; +import org.eclipse.cdt.core.model.ICProject; +import org.eclipse.cdt.core.search.ICSearchScope; +import org.eclipse.core.resources.IProject; +import org.eclipse.core.runtime.IPath; +import org.eclipse.core.runtime.Path; -/** - * @author aniefer - * - * To change the template for this generated type comment go to - * Window>Preferences>Java>Code Generation>Code and Comments - */ public class CSearchScope implements ICSearchScope { - /** - * - */ + private ArrayList elements; + + /* The paths of the resources in this search scope*/ + private IPath[] paths; + private boolean[] pathWithSubFolders; + private int pathsCount; + + private IPath[] enclosingProjects; + public CSearchScope() { super(); - // TODO Auto-generated constructor stub + this.initialize(); + } + + protected void initialize() { + this.paths = new IPath[1]; + this.pathWithSubFolders = new boolean[1]; + this.pathsCount = 0; + this.enclosingProjects = new IPath[0]; } + private void addEnclosingProject(IPath path) { + int length = this.enclosingProjects.length; + for (int i = 0; i < length; i++) { + if (this.enclosingProjects[i].equals(path)) return; + } + System.arraycopy( + this.enclosingProjects, + 0, + this.enclosingProjects = new IPath[length+1], + 0, + length); + this.enclosingProjects[length] = path; + } + + public void add(ICProject cProject, boolean includesPrereqProjects, HashSet visitedProjects) { + IProject project = cProject.getProject(); + if (!project.isAccessible() || !visitedProjects.add(project)) return; + + this.addEnclosingProject(project.getFullPath()); + } + /** + * Adds the given path to this search scope. Remember if subfolders need to be included as well. + */ + private void add(IPath path, boolean withSubFolders) { + if (this.paths.length == this.pathsCount) { + System.arraycopy( + this.paths, + 0, + this.paths = new IPath[this.pathsCount * 2], + 0, + this.pathsCount); + System.arraycopy( + this.pathWithSubFolders, + 0, + this.pathWithSubFolders = new boolean[this.pathsCount * 2], + 0, + this.pathsCount); + } + this.paths[this.pathsCount] = path; + this.pathWithSubFolders[this.pathsCount++] = withSubFolders; + } + + public boolean encloses(String resourcePathString) { + IPath resourcePath; + int separatorIndex = -1; //resourcePathString.indexOf(JAR_FILE_ENTRY_SEPARATOR); + if (separatorIndex != -1) { + resourcePath = + new Path(resourcePathString.substring(0, separatorIndex)). + append(new Path(resourcePathString.substring(separatorIndex+1))); + } else { + resourcePath = new Path(resourcePathString); + } + return this.encloses(resourcePath); + } + + /** + * Returns whether this search scope encloses the given path. + */ + private boolean encloses(IPath path) { + for (int i = 0; i < this.pathsCount; i++) { + if (this.pathWithSubFolders[i]) { + if (this.paths[i].isPrefixOf(path)) { + return true; + } + } else { + // if not looking at subfolders, this scope encloses the given path + // if this path is a direct child of the scope's ressource + // or if this path is the scope's resource (see bug 13919 Declaration for package not found if scope is not project) + IPath scopePath = this.paths[i]; + if (scopePath.isPrefixOf(path) + && ((scopePath.segmentCount() == path.segmentCount() - 1) + || (scopePath.segmentCount() == path.segmentCount()))) { + return true; + } + } + } + return false; + } + + public boolean encloses(ICElement element) { + if (this.elements != null) { + for (int i = 0, length = this.elements.size(); i < length; i++) { + ICElement scopeElement = (ICElement)this.elements.get(i); + ICElement searchedElement = element; + while (searchedElement != null) { + if (searchedElement.equals(scopeElement)) { + return true; + } else { + searchedElement = searchedElement.getParent(); + } + } + } + return false; + } else { + return this.encloses(this.fullPath(element)); + } + } + + public IPath[] enclosingProjects() { + return this.enclosingProjects; + } + + private IPath fullPath(ICElement element) { + return null; + } } diff --git a/core/org.eclipse.cdt.core/search/org/eclipse/cdt/internal/core/search/CWorkspaceScope.java b/core/org.eclipse.cdt.core/search/org/eclipse/cdt/internal/core/search/CWorkspaceScope.java new file mode 100644 index 00000000000..607f99f80bc --- /dev/null +++ b/core/org.eclipse.cdt.core/search/org/eclipse/cdt/internal/core/search/CWorkspaceScope.java @@ -0,0 +1,86 @@ +/******************************************************************************* + * Copyright (c) 2000, 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 v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ + +package org.eclipse.cdt.internal.core.search; + +import java.util.HashSet; + +import org.eclipse.cdt.core.model.CoreModel; +import org.eclipse.cdt.core.model.ICElement; +import org.eclipse.cdt.core.model.ICElementDelta; +import org.eclipse.cdt.core.model.ICProject; +import org.eclipse.core.runtime.IPath; + +public class CWorkspaceScope extends CSearchScope { + + protected boolean needsInitialize; + + public boolean encloses(String resourcePath) { + return true; + } + + public boolean encloses(ICElement element) { + return true; + } + + public IPath[] enclosingProjects() { + if (this.needsInitialize) { + this.initialize(); + } + return super.enclosingProjects(); + } + + public void initialize() { + super.initialize(); + ICProject[] projects = CoreModel.getDefault().getCModel().getCProjects(); // ModelManager ;//.getJavaModelManager().getJavaModel().getJavaProjects(); + for (int i = 0, length = projects.length; i < length; i++) + this.add(projects[i], false, new HashSet(2)); + this.needsInitialize = false; + } + + + public void processDelta(ICElementDelta delta) { +//TODO: BOG Hook this up to the model manager to give us updates when the workspace changes +// if (this.needsInitialize) return; +// ICElement element = delta.getElement(); +// switch (element.getElementType()) { +// case ICElement.C_MODEL: +// ICElementDelta[] children = delta.getAffectedChildren(); +// for (int i = 0, length = children.length; i < length; i++) { +// ICElementDelta child = children[i]; +// this.processDelta(child); +// } +// break; +// case ICElement.C_PROJECT: +// int kind = delta.getKind(); +// switch (kind) { +// case ICElementDelta.ADDED: +// case ICElementDelta.REMOVED: +// this.needsInitialize = true; +// break; +// case ICElementDelta.CHANGED: +// children = delta.getAffectedChildren(); +// for (int i = 0, length = children.length; i < length; i++) { +// ICElementDelta child = children[i]; +// this.processDelta(child); +// } +// break; +// } +// break; +// } + } + + + public String toString() { + return "CWorkspaceScope"; //$NON-NLS-1$ + } + +} diff --git a/core/org.eclipse.cdt.core/search/org/eclipse/cdt/internal/core/search/IIndexSearchRequestor.java b/core/org.eclipse.cdt.core/search/org/eclipse/cdt/internal/core/search/IIndexSearchRequestor.java new file mode 100644 index 00000000000..f13b0c666f6 --- /dev/null +++ b/core/org.eclipse.cdt.core/search/org/eclipse/cdt/internal/core/search/IIndexSearchRequestor.java @@ -0,0 +1,69 @@ +/******************************************************************************* + * Copyright (c) 2000, 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 v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.cdt.internal.core.search; + +public interface IIndexSearchRequestor { +/** + * Accepts the declaration of a class in the compilation unit with the given resource path. + * The class is declared in the given package and with the given type name. + *

    + * Note that the resource path can be null if the search query doesn't require it (eg. get all class names). + */ +void acceptClassDeclaration(String resourcePath, char[] simpleTypeName, char[][] enclosingTypeNames); +/** + * Accepts the declaration of a constructor in the compilation unit with the given resource path. + * The constructor is declared with a given name and number of arguments. + */ +void acceptConstructorDeclaration(String resourcePath, char[] typeName, int parameterCount); +/** + * Accepts the reference to a constructor in the compilation unit with the given resource path. + * The constructor is referenced using the given name and a number of arguments. + * + * Note that the resource path can be null if the search query doesn't require it. + */ +void acceptConstructorReference(String resourcePath, char[] typeName, int parameterCount); +/** + * Accepts the declaration of a field in the compilation unit with the given resource path. + *

    + * Note that the resource path can be null if the search query doesn't require it (eg. get all class names). + * Likewise, the declaring package name and the declaring type names if the query doesn't require them. + */ +void acceptFieldDeclaration(String resourcePath, char[] fieldName); +/** + * Accepts the reference to a field in the compilation unit with the given resource path. + * The field is referenced using the given name + */ +void acceptFieldReference(String resourcePath, char[] fieldName); +/** + * Accepts the declaration of a method in the compilation unit with the given resource path. + * The method is declared with a given method name and number of arguments. + */ +void acceptMethodDeclaration(String resourcePath, char[] methodName, int parameterCount); +/** + * Accepts the reference to a method in the compilation unit with the given resource path. + * The method is referenced using the given selector and a number of arguments. + * + * Note that the resource path can be null if the search query doesn't require it. + */ +void acceptMethodReference(String resourcePath, char[] methodName, int parameterCount); +/** + * Accepts the reference to a supertype in the compilation unit with the given resource path. + * Note that the resource path and/or the package name can be null. + */ +void acceptSuperTypeReference(String resourcePath, char[] qualification, char[] typeName, char[] enclosingTypeName, char classOrInterface, char[] superQualification, char[] superTypeName, char superClassOrInterface, int modifiers); +/** + * Accepts the reference to a class in the compilation unit with the given resource path. + * The class is referenced using the given type name. + *

    + * Note that the resource path can be null if the search query doesn't require it. + */ +void acceptTypeReference(String resourcePath, char[] typeName); +} \ No newline at end of file diff --git a/core/org.eclipse.cdt.core/search/org/eclipse/cdt/internal/core/search/IndexSelector.java b/core/org.eclipse.cdt.core/search/org/eclipse/cdt/internal/core/search/IndexSelector.java new file mode 100644 index 00000000000..f7ec5b0f305 --- /dev/null +++ b/core/org.eclipse.cdt.core/search/org/eclipse/cdt/internal/core/search/IndexSelector.java @@ -0,0 +1,118 @@ +/******************************************************************************* + * Copyright (c) 2000, 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 v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.cdt.internal.core.search; + +import java.util.ArrayList; + +import org.eclipse.cdt.core.model.ICElement; +import org.eclipse.cdt.core.model.ICModel; +import org.eclipse.cdt.core.model.ICProject; +import org.eclipse.cdt.core.search.ICSearchScope; +import org.eclipse.cdt.internal.core.index.IIndex; +import org.eclipse.cdt.internal.core.search.indexing.IndexManager; +import org.eclipse.core.resources.IWorkspaceRoot; +import org.eclipse.core.resources.ResourcesPlugin; +import org.eclipse.core.runtime.IPath; + +/** + * Selects the indexes that correspond to projects in a given search scope + * and that are dependent on a given focus element. + */ +public class IndexSelector { + ICSearchScope searchScope; + ICElement focus; + IndexManager indexManager; + IPath[] indexKeys; // cache of the keys for looking index up + boolean isPolymorphicSearch; + public IndexSelector( + ICSearchScope searchScope, + ICElement focus, + boolean isPolymorphicSearch, + IndexManager indexManager) { + this.searchScope = searchScope; + this.focus = focus; + this.indexManager = indexManager; + this.isPolymorphicSearch = isPolymorphicSearch; + } + /** + * Returns whether elements of the given project can see the given focus (an ICProject) + */ + public static boolean canSeeFocus(ICElement focus, boolean isPolymorphicSearch, IPath projectPath) { + //TODO: BOG Temp - Provide Proper Impl + ICModel model = focus.getCModel(); + ICProject project = getCProject(projectPath, model); + return true; + } + /* + * Compute the list of paths which are keying index files. + */ + private void initializeIndexKeys() { + + ArrayList requiredIndexKeys = new ArrayList(); + IPath[] projects = this.searchScope.enclosingProjects(); + IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot(); + ICElement projectFocus = this.focus == null ? null : getProject(this.focus); + for (int i = 0; i < projects.length; i++) { + IPath location; + IPath path = projects[i]; + if ((!root.getProject(path.lastSegment()).exists()) // if project does not exist + && path.segmentCount() > 1 + && ((location = root.getFile(path).getLocation()) == null + || !new java.io.File(location.toOSString()).exists()) // and internal jar file does not exist + && !new java.io.File(path.toOSString()).exists()) { // and external jar file does not exist + continue; + } + if (projectFocus == null || canSeeFocus(projectFocus, this.isPolymorphicSearch, path)) { + if (requiredIndexKeys.indexOf(path) == -1) { + requiredIndexKeys.add(path); + } + } + } + this.indexKeys = new IPath[requiredIndexKeys.size()]; + requiredIndexKeys.toArray(this.indexKeys); + } + public IIndex[] getIndexes() { + if (this.indexKeys == null) { + this.initializeIndexKeys(); + } + // acquire the in-memory indexes on the fly + int length = this.indexKeys.length; + IIndex[] indexes = new IIndex[length]; + int count = 0; + for (int i = 0; i < length; i++){ + // may trigger some index recreation work + IIndex index = indexManager.getIndex(indexKeys[i], true /*reuse index file*/, false /*do not create if none*/); + if (index != null) indexes[count++] = index; // only consider indexes which are ready yet + } + if (count != length) { + System.arraycopy(indexes, 0, indexes=new IIndex[count], 0, count); + } + return indexes; + } + /** + * Returns the project that corresponds to the given path. + * Returns null if the path doesn't correspond to a project. + */ + private static ICProject getCProject(IPath path, ICModel model) { + ICProject project = model.getCProject(path.lastSegment()); + if (project.exists()) { + return project; + } else { + return null; + } + } + public static ICElement getProject(ICElement element) { + while (!(element instanceof ICProject)) { + element = element.getParent(); + } + return element; + } +} diff --git a/core/org.eclipse.cdt.core/search/org/eclipse/cdt/internal/core/search/PathCollector.java b/core/org.eclipse.cdt.core/search/org/eclipse/cdt/internal/core/search/PathCollector.java new file mode 100644 index 00000000000..a3e04d7ac9c --- /dev/null +++ b/core/org.eclipse.cdt.core/search/org/eclipse/cdt/internal/core/search/PathCollector.java @@ -0,0 +1,131 @@ +/******************************************************************************* + * Copyright (c) 2000, 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 v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.cdt.internal.core.search; + +import java.util.HashSet; +import java.util.Iterator; + +import org.eclipse.core.resources.IFile; +import org.eclipse.core.resources.IWorkspace; +import org.eclipse.core.runtime.IPath; +import org.eclipse.core.runtime.Path; + + /** + * Collects the resource paths reported by a client to this search requestor. + */ + public class PathCollector implements IIndexSearchRequestor { + + /* a set of resource paths */ + public HashSet paths = new HashSet(5); + /** + * @see IIndexSearchRequestor + */ + public void acceptClassDeclaration(String resourcePath, char[] simpleTypeName, char[][] enclosingTypeNames) { + this.paths.add( resourcePath); + } + /** + * @see IIndexSearchRequestor + */ + public void acceptConstructorDeclaration(String resourcePath, char[] typeName, int parameterCount) { + + this.paths.add(resourcePath); + } + /** + * @see IIndexSearchRequestor + */ + public void acceptConstructorReference(String resourcePath, char[] typeName, int parameterCount) { + + this.paths.add(resourcePath); + } + /** + * @see IIndexSearchRequestor + */ + public void acceptFieldDeclaration(String resourcePath, char[] fieldName) { + this.paths.add(resourcePath); + } + /** + * @see IIndexSearchRequestor + */ + public void acceptFieldReference(String resourcePath, char[] fieldName) { + + this.paths.add(resourcePath); + } + /** + * @see IIndexSearchRequestor + */ + public void acceptInterfaceDeclaration(String resourcePath, char[] simpleTypeName, char[][] enclosingTypeNames, char[] packageName) { + + this.paths.add(resourcePath); + } + /** + * @see IIndexSearchRequestor + */ + public void acceptMethodDeclaration(String resourcePath, char[] methodName, int parameterCount) { + + this.paths.add(resourcePath); + } + /** + * @see IIndexSearchRequestor + */ + public void acceptMethodReference(String resourcePath, char[] methodName, int parameterCount) { + + this.paths.add(resourcePath); + } + /** + * @see IIndexSearchRequestor + */ + public void acceptPackageReference(String resourcePath, char[] packageName) { + + this.paths.add(resourcePath); + } + /** + * @see IIndexSearchRequestor + */ + public void acceptSuperTypeReference(String resourcePath, char[] qualification, char[] typeName, char[] enclosingTypeName, char classOrInterface, char[] superQualification, char[] superTypeName, char superClassOrInterface, int modifiers) { + this.paths.add(resourcePath); + } + /** + * @see IIndexSearchRequestor + */ + public void acceptSuperTypeReference(String resourcePath, char[] qualification, char[] typeName, char classOrInterface, char[] superQualification, char[] superTypeName, char superClassOrInterface, int modifiers) { + this.paths.add(resourcePath); + } + /** + * @see IIndexSearchRequestor + */ + public void acceptTypeReference(String resourcePath, char[] typeName) { + this.paths.add(resourcePath); + } + /** + * Returns the files that correspond to the paths that have been collected. + */ + public IFile[] getFiles(IWorkspace workspace) { + IFile[] result = new IFile[this.paths.size()]; + int i = 0; + for (Iterator iter = this.paths.iterator(); iter.hasNext();) { + String resourcePath = (String)iter.next(); + IPath path = new Path(resourcePath); + result[i++] = workspace.getRoot().getFile(path); + } + return result; + } + /** + * Returns the paths that have been collected. + */ + public String[] getPaths() { + String[] result = new String[this.paths.size()]; + int i = 0; + for (Iterator iter = this.paths.iterator(); iter.hasNext();) { + result[i++] = (String)iter.next(); + } + return result; + } +} diff --git a/core/org.eclipse.cdt.core/search/org/eclipse/cdt/internal/core/search/PatternSearchJob.java b/core/org.eclipse.cdt.core/search/org/eclipse/cdt/internal/core/search/PatternSearchJob.java index b39961065e4..06e2f301262 100644 --- a/core/org.eclipse.cdt.core/search/org/eclipse/cdt/internal/core/search/PatternSearchJob.java +++ b/core/org.eclipse.cdt.core/search/org/eclipse/cdt/internal/core/search/PatternSearchJob.java @@ -1,67 +1,168 @@ /******************************************************************************* - * Copyright (c) 2003 IBM Corporation and others. + * Copyright (c) 2000, 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 + * are made available under the terms of the Common Public License v1.0 * which accompanies this distribution, and is available at - * http://www.eclipse.org/legal/cpl-v05.html + * http://www.eclipse.org/legal/cpl-v10.html * * Contributors: - * IBM Corp. - Rational Software - initial implementation - ******************************************************************************/ + * IBM Corporation - initial API and implementation + *******************************************************************************/ /* * Created on Jun 13, 2003 */ package org.eclipse.cdt.internal.core.search; -import org.eclipse.cdt.internal.core.search.processing.IJob; -import org.eclipse.core.runtime.IProgressMonitor; +import java.io.IOException; + +import org.eclipse.cdt.core.model.ICElement; +import org.eclipse.cdt.core.search.ICSearchScope; +import org.eclipse.cdt.internal.core.index.IIndex; +import org.eclipse.cdt.internal.core.search.indexing.IndexManager; +import org.eclipse.cdt.internal.core.search.indexing.ReadWriteMonitor; +import org.eclipse.cdt.internal.core.search.matching.CSearchPattern; +import org.eclipse.cdt.internal.core.search.processing.IJob; +import org.eclipse.cdt.internal.core.search.processing.JobManager; +import org.eclipse.core.runtime.IProgressMonitor; +import org.eclipse.core.runtime.OperationCanceledException; + -/** - * @author aniefer - * - * To change the template for this generated type comment go to - * Window>Preferences>Java>Code Generation>Code and Comments - */ public class PatternSearchJob implements IJob { - /** - * - */ - public PatternSearchJob() { - super(); - // TODO Auto-generated constructor stub - } - /* (non-Javadoc) - * @see org.eclipse.cdt.internal.core.search.processing.IJob#belongsTo(java.lang.String) - */ + protected CSearchPattern pattern; + protected ICSearchScope scope; + protected ICElement focus; + protected IIndexSearchRequestor requestor; + protected IndexManager indexManager; + protected int detailLevel; + protected IndexSelector indexSelector; + protected boolean isPolymorphicSearch; + protected long executionTime = 0; + + public PatternSearchJob( + CSearchPattern pattern, + ICSearchScope scope, + IIndexSearchRequestor requestor, + IndexManager indexManager) { + + this( + pattern, + scope, + null, + false, + requestor, + indexManager); + } + public PatternSearchJob( + CSearchPattern pattern, + ICSearchScope scope, + ICElement focus, + boolean isPolymorphicSearch, + IIndexSearchRequestor requestor, + IndexManager indexManager) { + + this.pattern = pattern; + this.scope = scope; + this.focus = focus; + this.isPolymorphicSearch = isPolymorphicSearch; + this.requestor = requestor; + this.indexManager = indexManager; + } public boolean belongsTo(String jobFamily) { - // TODO Auto-generated method stub - return false; + return true; } - - /* (non-Javadoc) - * @see org.eclipse.cdt.internal.core.search.processing.IJob#cancel() - */ public void cancel() { - // TODO Auto-generated method stub - } + public boolean execute(IProgressMonitor progressMonitor) { - /* (non-Javadoc) - * @see org.eclipse.cdt.internal.core.search.processing.IJob#execute(org.eclipse.core.runtime.IProgressMonitor) - */ - public boolean execute(IProgressMonitor progress) { - // TODO Auto-generated method stub - return false; + if (progressMonitor != null && progressMonitor.isCanceled()) + throw new OperationCanceledException(); + boolean isComplete = COMPLETE; + executionTime = 0; + if (this.indexSelector == null) { + this.indexSelector = + new IndexSelector(this.scope, this.focus, this.isPolymorphicSearch, this.indexManager); + } + IIndex[] searchIndexes = this.indexSelector.getIndexes(); + try { + int max = searchIndexes.length; + if (progressMonitor != null) { + progressMonitor.beginTask("", max); //$NON-NLS-1$ + } + for (int i = 0; i < max; i++) { + isComplete &= search(searchIndexes[i], progressMonitor); + if (progressMonitor != null) { + if (progressMonitor.isCanceled()) { + throw new OperationCanceledException(); + } else { + progressMonitor.worked(1); + } + } + } + if (JobManager.VERBOSE) { + JobManager.verbose("-> execution time: " + executionTime + "ms - " + this);//$NON-NLS-1$//$NON-NLS-2$ + } + return isComplete; + } finally { + if (progressMonitor != null) { + progressMonitor.done(); + } + } } - - /* (non-Javadoc) - * @see org.eclipse.cdt.internal.core.search.processing.IJob#isReadyToRun() - */ public boolean isReadyToRun() { - // TODO Auto-generated method stub - return false; + if (this.indexSelector == null) { // only check once. As long as this job is used, it will keep the same index picture + this.indexSelector = new IndexSelector(this.scope, this.focus, this.isPolymorphicSearch, this.indexManager); + this.indexSelector.getIndexes(); // will only cache answer if all indexes were available originally + } + return true; } + public boolean search(IIndex index, IProgressMonitor progressMonitor) { + if (progressMonitor != null && progressMonitor.isCanceled()) + throw new OperationCanceledException(); + +// IIndex inMemIndex = indexManager.peekAtIndex(new Path(((Index)index).toString.substring("Index for ".length()).replace('\\','/'))); +// if (inMemIndex != index) { +// System.out.println("SANITY CHECK: search job using obsolete index: ["+index+ "] instead of: ["+inMemIndex+"]"); +// } + + if (index == null) + return COMPLETE; + ReadWriteMonitor monitor = indexManager.getMonitorFor(index); + if (monitor == null) + return COMPLETE; // index got deleted since acquired + try { + monitor.enterRead(); // ask permission to read + + /* if index has changed, commit these before querying */ + if (index.hasChanged()) { + try { + monitor.exitRead(); // free read lock + monitor.enterWrite(); // ask permission to write + this.indexManager.saveIndex(index); + } catch (IOException e) { + return FAILED; + } finally { + monitor.exitWriteEnterRead(); // finished writing and reacquire read permission + } + } + long start = System.currentTimeMillis(); + pattern.findIndexMatches( + index, + requestor, + detailLevel, + progressMonitor, + this.scope); + executionTime += System.currentTimeMillis() - start; + return COMPLETE; + } catch (IOException e) { + return FAILED; + } finally { + monitor.exitRead(); // finished reading + } + } + public String toString() { + return "searching " + pattern.toString(); //$NON-NLS-1$ + } } diff --git a/core/org.eclipse.cdt.core/search/org/eclipse/cdt/internal/core/search/matching/CSearchPattern.java b/core/org.eclipse.cdt.core/search/org/eclipse/cdt/internal/core/search/matching/CSearchPattern.java index b456b0e3d6d..8b143de41e7 100644 --- a/core/org.eclipse.cdt.core/search/org/eclipse/cdt/internal/core/search/matching/CSearchPattern.java +++ b/core/org.eclipse.cdt.core/search/org/eclipse/cdt/internal/core/search/matching/CSearchPattern.java @@ -1,18 +1,19 @@ /******************************************************************************* - * Copyright (c) 2003 IBM Corporation and others. + * Copyright (c) 2000, 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 + * are made available under the terms of the Common Public License v1.0 * which accompanies this distribution, and is available at - * http://www.eclipse.org/legal/cpl-v05.html + * http://www.eclipse.org/legal/cpl-v10.html * * Contributors: - * IBM Corp. - Rational Software - initial implementation - ******************************************************************************/ + * IBM Corporation - initial API and implementation + *******************************************************************************/ /* * Created on Jun 13, 2003 */ package org.eclipse.cdt.internal.core.search.matching; +import java.io.IOException; import java.io.StringReader; import java.util.LinkedList; @@ -25,16 +26,22 @@ import org.eclipse.cdt.core.parser.ScannerException; import org.eclipse.cdt.core.parser.ast.ASTClassKind; import org.eclipse.cdt.core.search.ICSearchConstants; 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.IIndex; +import org.eclipse.cdt.internal.core.index.impl.BlocksIndexInput; +import org.eclipse.cdt.internal.core.index.impl.IndexInput; import org.eclipse.cdt.internal.core.parser.ScannerInfo; import org.eclipse.cdt.internal.core.search.CharOperation; +import org.eclipse.cdt.internal.core.search.IIndexSearchRequestor; +import org.eclipse.cdt.internal.core.search.indexing.IIndexConstants; +import org.eclipse.core.runtime.IProgressMonitor; +import org.eclipse.core.runtime.OperationCanceledException; /** * @author aniefer - * - * To change the template for this generated type comment go to - * Window>Preferences>Java>Code Generation>Code and Comments */ -public abstract class CSearchPattern implements ICSearchConstants, ICSearchPattern { +public abstract class CSearchPattern implements ICSearchConstants, ICSearchPattern, IIndexConstants { public static final int IMPOSSIBLE_MATCH = 0; public static final int POSSIBLE_MATCH = 1; @@ -50,12 +57,8 @@ public abstract class CSearchPattern implements ICSearchConstants, ICSearchPatte _caseSensitive = caseSensitive; } - /** - * - */ public CSearchPattern() { super(); - // TODO Auto-generated constructor stub } public static CSearchPattern createPattern( String patternString, SearchFor searchFor, LimitTo limitTo, int matchMode, boolean caseSensitive ){ @@ -74,8 +77,6 @@ public abstract class CSearchPattern implements ICSearchConstants, ICSearchPatte //case ICSearchConstants.FIELD: // pattern = createFieldPattern( patternString, limitTo, matchMode, caseSensitive ); // break; - - return pattern; } @@ -157,7 +158,7 @@ public abstract class CSearchPattern implements ICSearchConstants, ICSearchPatte } else if ( searchFor == UNION ) { kind = ASTClassKind.UNION; } - + char [][] qualifications = new char[0][]; return new ClassDeclarationPattern( name.toCharArray(), (char[][])list.toArray( qualifications ), kind, matchMode, caseSensitive ); } @@ -184,7 +185,66 @@ public abstract class CSearchPattern implements ICSearchConstants, ICSearchPatte return false; } + /** + * Query a given index for matching entries. + */ + public void findIndexMatches(IIndex index, IIndexSearchRequestor requestor, int detailLevel, IProgressMonitor progressMonitor, ICSearchScope scope) throws IOException { + + if (progressMonitor != null && progressMonitor.isCanceled()) throw new OperationCanceledException(); + + IndexInput input = new BlocksIndexInput(index.getIndexFile()); + try { + input.open(); + findIndexMatches(input, requestor, detailLevel, progressMonitor,scope); + } finally { + input.close(); + } + } + /** + * Query a given index for matching entries. + */ + public void findIndexMatches(IndexInput input, IIndexSearchRequestor requestor, int detailLevel, IProgressMonitor progressMonitor, ICSearchScope scope) throws IOException { + + if (progressMonitor != null && progressMonitor.isCanceled()) throw new OperationCanceledException(); + /* narrow down a set of entries using prefix criteria */ + IEntryResult[] entries = input.queryEntriesPrefixedBy(indexEntryPrefix()); + if (entries == null) return; + + /* only select entries which actually match the entire search pattern */ + for (int i = 0, max = entries.length; i < max; i++){ + + if (progressMonitor != null && progressMonitor.isCanceled()) throw new OperationCanceledException(); + + /* retrieve and decode entry */ + IEntryResult entry = entries[i]; + decodeIndexEntry(entry); + if (matchIndexEntry()){ + feedIndexRequestor(requestor, detailLevel, entry.getFileReferences(), input, scope); + } + } + } + + /** + * Feed the requestor according to the current search pattern + */ + public abstract void feedIndexRequestor(IIndexSearchRequestor requestor, int detailLevel, int[] references, IndexInput input, ICSearchScope scope) throws IOException ; + /** + * Decodes the index entry + */ + protected abstract void decodeIndexEntry(IEntryResult entryResult); + /** + * Answers the suitable prefix that should be used in order + * to query indexes for the corresponding item. + * The more accurate the prefix and the less false hits will have + * to be eliminated later on. + */ + public abstract char[] indexEntryPrefix(); + /** + * Checks whether an entry matches the current search pattern + */ + protected abstract boolean matchIndexEntry(); + protected int _matchMode; protected boolean _caseSensitive; } diff --git a/core/org.eclipse.cdt.core/search/org/eclipse/cdt/internal/core/search/matching/ClassDeclarationPattern.java b/core/org.eclipse.cdt.core/search/org/eclipse/cdt/internal/core/search/matching/ClassDeclarationPattern.java index 496e41d502c..8a8e54d42d2 100644 --- a/core/org.eclipse.cdt.core/search/org/eclipse/cdt/internal/core/search/matching/ClassDeclarationPattern.java +++ b/core/org.eclipse.cdt.core/search/org/eclipse/cdt/internal/core/search/matching/ClassDeclarationPattern.java @@ -1,29 +1,35 @@ /******************************************************************************* - * Copyright (c) 2003 IBM Corporation and others. + * Copyright (c) 2000, 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 + * are made available under the terms of the Common Public License v1.0 * which accompanies this distribution, and is available at - * http://www.eclipse.org/legal/cpl-v05.html + * http://www.eclipse.org/legal/cpl-v10.html * * Contributors: - * IBM Corp. - Rational Software - initial implementation - ******************************************************************************/ + * IBM Corporation - initial API and implementation + *******************************************************************************/ /* * Created on Jun 13, 2003 */ package org.eclipse.cdt.internal.core.search.matching; +import java.io.IOException; + import org.eclipse.cdt.core.parser.ast.ASTClassKind; import org.eclipse.cdt.core.parser.ast.IASTClassSpecifier; import org.eclipse.cdt.core.parser.ast.IASTOffsetableElement; +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.index.impl.IndexedFile; import org.eclipse.cdt.internal.core.search.CharOperation; +import org.eclipse.cdt.internal.core.search.IIndexSearchRequestor; +import org.eclipse.cdt.internal.core.search.indexing.AbstractIndexer; /** * @author aniefer - * - * To change the template for this generated type comment go to - * Window>Preferences>Java>Code Generation>Code and Comments */ + public class ClassDeclarationPattern extends CSearchPattern { public ClassDeclarationPattern( int matchMode, boolean caseSensitive ){ @@ -50,7 +56,8 @@ public class ClassDeclarationPattern extends CSearchPattern { return IMPOSSIBLE_MATCH; IASTClassSpecifier clsSpec = (IASTClassSpecifier) node; - + String nodeName = clsSpec.getName(); + //check name, if simpleName == null, its treated the same as "*" if( simpleName != null && !matchesName( simpleName, clsSpec.getName().toCharArray() ) ){ return IMPOSSIBLE_MATCH; @@ -92,4 +99,87 @@ public class ClassDeclarationPattern extends CSearchPattern { private char[][] containingTypes; private ASTClassKind classKind; + protected char[] decodedSimpleName; + private char[][] decodedContainingTypes; + protected char decodedType; + + + public void feedIndexRequestor(IIndexSearchRequestor requestor, int detailLevel, int[] references, IndexInput input, ICSearchScope scope) throws IOException { + boolean isClass = decodedType == CLASS_SUFFIX; + for (int i = 0, max = references.length; i < max; i++) { + IndexedFile file = input.getIndexedFile(references[i]); + String path; + if (file != null && scope.encloses(path =file.getPath())) { + //TODO: BOG Fix this up - even if it's not a class we still care + if (isClass) { + requestor.acceptClassDeclaration(path, decodedSimpleName, decodedContainingTypes); + } else { + requestor.acceptClassDeclaration(path, decodedSimpleName, decodedContainingTypes); + } + } + } + } + + protected void decodeIndexEntry(IEntryResult entryResult) { + char[] word = entryResult.getWord(); + int size = word.length; + + this.decodedType = word[TYPE_DECL_LENGTH]; + int oldSlash = TYPE_DECL_LENGTH+1; + int slash = CharOperation.indexOf(SEPARATOR, word, oldSlash+1); + + this.decodedSimpleName = CharOperation.subarray(word, oldSlash+1, slash); + + if (slash != -1){ + if (slash+1 < size){ + this.decodedContainingTypes = CharOperation.splitOn('/', CharOperation.subarray(word, slash+1, size)); + } + } + + } + + public char[] indexEntryPrefix() { + return AbstractIndexer.bestTypeDeclarationPrefix( + simpleName, + containingTypes, + classKind, + _matchMode, + _caseSensitive + ); + } + + protected boolean matchIndexEntry() { + + //TODO: BOG PUT QUALIFIER CHECKING BACK IN +// if (containingTypes != null){ +// // empty char[][] means no enclosing type (in which case, the decoded one is the empty char array) +// if (containingTypes.length == 0){ +// if (decodedContainingTypes != CharOperation.NO_CHAR_CHAR) return false; +// } else { +// if (!CharOperation.equals(containingTypes, decodedContainingTypes, _caseSensitive)) return false; +// } +// } + + /* check simple name matches */ + if (simpleName != null){ + switch(_matchMode){ + case EXACT_MATCH : + if (!CharOperation.equals(simpleName, decodedSimpleName, _caseSensitive)){ + return false; + } + break; + case PREFIX_MATCH : + if (!CharOperation.prefixEquals(simpleName, decodedSimpleName, _caseSensitive)){ + return false; + } + break; + case PATTERN_MATCH : + if (!CharOperation.match(simpleName, decodedSimpleName, _caseSensitive)){ + return false; + } + } + } + return true; + } + } diff --git a/core/org.eclipse.cdt.core/search/org/eclipse/cdt/internal/core/search/processing/JobManager.java b/core/org.eclipse.cdt.core/search/org/eclipse/cdt/internal/core/search/processing/JobManager.java index 92caddd44e8..bd6c0220f1e 100644 --- a/core/org.eclipse.cdt.core/search/org/eclipse/cdt/internal/core/search/processing/JobManager.java +++ b/core/org.eclipse.cdt.core/search/org/eclipse/cdt/internal/core/search/processing/JobManager.java @@ -13,10 +13,10 @@ */ package org.eclipse.cdt.internal.core.search.processing; +import org.eclipse.cdt.internal.core.search.Util; import org.eclipse.core.runtime.IProgressMonitor; import org.eclipse.core.runtime.OperationCanceledException; import org.eclipse.core.runtime.SubProgressMonitor; -import org.eclipse.cdt.internal.core.search.Util; public abstract class JobManager implements Runnable {