mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-06-07 17:56:01 +02:00
PDOM Search - more functional now. Satisfies simple searches.
This commit is contained in:
parent
1ee376bf92
commit
d78c26534a
11 changed files with 164 additions and 54 deletions
|
@ -26,8 +26,6 @@ public interface IPDOM {
|
||||||
|
|
||||||
public IBinding resolveBinding(IASTName name);
|
public IBinding resolveBinding(IASTName name);
|
||||||
|
|
||||||
public IBinding[] resolvePrefix(IASTName name);
|
|
||||||
|
|
||||||
public IASTName[] getDeclarations(IBinding binding);
|
public IASTName[] getDeclarations(IBinding binding);
|
||||||
|
|
||||||
public void delete() throws CoreException;
|
public void delete() throws CoreException;
|
||||||
|
|
|
@ -1906,13 +1906,6 @@ public class CVisitor {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
IASTTranslationUnit tu = name.getTranslationUnit();
|
|
||||||
if (tu != null) {
|
|
||||||
IPDOM pdom = tu.getIndex();
|
|
||||||
if (pdom != null)
|
|
||||||
result = (IBinding[])ArrayUtil.addAll(IBinding.class, result, pdom.resolvePrefix(name));
|
|
||||||
}
|
|
||||||
|
|
||||||
return (IBinding[]) ArrayUtil.trim( IBinding.class, result );
|
return (IBinding[]) ArrayUtil.trim( IBinding.class, result );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1466,7 +1466,7 @@ public class CPPSemantics {
|
||||||
item = nodes[idx];
|
item = nodes[idx];
|
||||||
} else {
|
} else {
|
||||||
item = null;
|
item = null;
|
||||||
nullItem: while( item == null ){
|
while( item == null ){
|
||||||
if( namespaceIdx > -1 ) {
|
if( namespaceIdx > -1 ) {
|
||||||
//check all definitions of this namespace
|
//check all definitions of this namespace
|
||||||
while( namespaceIdx > -1 && namespaceDefs.length > ++namespaceIdx ){
|
while( namespaceIdx > -1 && namespaceDefs.length > ++namespaceIdx ){
|
||||||
|
@ -3279,13 +3279,6 @@ public class CPPSemantics {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
IASTTranslationUnit tu = name.getTranslationUnit();
|
|
||||||
if (tu != null) {
|
|
||||||
IPDOM pdom = tu.getIndex();
|
|
||||||
if (pdom != null)
|
|
||||||
result = (IBinding[])ArrayUtil.addAll(IBinding.class, result, pdom.resolvePrefix(name));
|
|
||||||
}
|
|
||||||
|
|
||||||
return (IBinding[]) ArrayUtil.trim( IBinding.class, result );
|
return (IBinding[]) ArrayUtil.trim( IBinding.class, result );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -10,6 +10,7 @@
|
||||||
*******************************************************************************/
|
*******************************************************************************/
|
||||||
package org.eclipse.cdt.internal.core.pdom;
|
package org.eclipse.cdt.internal.core.pdom;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
import java.util.LinkedList;
|
import java.util.LinkedList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
@ -191,8 +192,14 @@ public class PDOMDatabase implements IPDOM {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public IBinding[] resolvePrefix(IASTName name) {
|
public PDOMBinding[] findBindings(String pattern) throws CoreException {
|
||||||
return new IBinding[0];
|
List bindings = new ArrayList();
|
||||||
|
PDOMLinkage linkage = getFirstLinkage();
|
||||||
|
while (linkage != null) {
|
||||||
|
linkage.findBindings(pattern, bindings);
|
||||||
|
linkage = linkage.getNextLinkage();
|
||||||
|
}
|
||||||
|
return (PDOMBinding[])bindings.toArray(new PDOMBinding[bindings.size()]);
|
||||||
}
|
}
|
||||||
|
|
||||||
public PDOMLinkage getLinkage(ILanguage language) throws CoreException {
|
public PDOMLinkage getLinkage(ILanguage language) throws CoreException {
|
||||||
|
|
|
@ -11,11 +11,16 @@
|
||||||
|
|
||||||
package org.eclipse.cdt.internal.core.pdom.dom;
|
package org.eclipse.cdt.internal.core.pdom.dom;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.regex.Matcher;
|
||||||
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
import org.eclipse.cdt.core.dom.ast.IASTName;
|
import org.eclipse.cdt.core.dom.ast.IASTName;
|
||||||
import org.eclipse.cdt.core.dom.ast.IBinding;
|
import org.eclipse.cdt.core.dom.ast.IBinding;
|
||||||
import org.eclipse.cdt.internal.core.pdom.PDOMDatabase;
|
import org.eclipse.cdt.internal.core.pdom.PDOMDatabase;
|
||||||
import org.eclipse.cdt.internal.core.pdom.db.BTree;
|
import org.eclipse.cdt.internal.core.pdom.db.BTree;
|
||||||
import org.eclipse.cdt.internal.core.pdom.db.Database;
|
import org.eclipse.cdt.internal.core.pdom.db.Database;
|
||||||
|
import org.eclipse.cdt.internal.core.pdom.db.IBTreeVisitor;
|
||||||
import org.eclipse.core.runtime.CoreException;
|
import org.eclipse.core.runtime.CoreException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -26,6 +31,35 @@ import org.eclipse.core.runtime.CoreException;
|
||||||
*/
|
*/
|
||||||
public abstract class PDOMLinkage extends PDOMNode {
|
public abstract class PDOMLinkage extends PDOMNode {
|
||||||
|
|
||||||
|
protected static final class MatchBinding implements IBTreeVisitor {
|
||||||
|
private final PDOMDatabase pdom;
|
||||||
|
private final List bindings;
|
||||||
|
private final Pattern pattern;
|
||||||
|
|
||||||
|
public MatchBinding(PDOMDatabase pdom, String pattern, List bindings) {
|
||||||
|
this.pdom = pdom;
|
||||||
|
this.bindings = bindings;
|
||||||
|
this.pattern = Pattern.compile(pattern);
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean visit(int record) throws CoreException {
|
||||||
|
if (record == 0)
|
||||||
|
return true;
|
||||||
|
|
||||||
|
// TODO of course do something smarter here
|
||||||
|
PDOMBinding binding = pdom.getBinding(record);
|
||||||
|
if (binding != null) {
|
||||||
|
Matcher matcher = pattern.matcher(binding.getName());
|
||||||
|
if (matcher.matches())
|
||||||
|
bindings.add(binding);
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
public int compare(int record) throws CoreException {
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private static final int ID_OFFSET = PDOMNode.RECORD_SIZE + 0;
|
private static final int ID_OFFSET = PDOMNode.RECORD_SIZE + 0;
|
||||||
private static final int NEXT_OFFSET = PDOMNode.RECORD_SIZE + 4;
|
private static final int NEXT_OFFSET = PDOMNode.RECORD_SIZE + 4;
|
||||||
private static final int INDEX_OFFSET = PDOMNode.RECORD_SIZE + 8;
|
private static final int INDEX_OFFSET = PDOMNode.RECORD_SIZE + 8;
|
||||||
|
@ -89,4 +123,6 @@ public abstract class PDOMLinkage extends PDOMNode {
|
||||||
|
|
||||||
public abstract PDOMBinding resolveBinding(IASTName name) throws CoreException;
|
public abstract PDOMBinding resolveBinding(IASTName name) throws CoreException;
|
||||||
|
|
||||||
|
public abstract void findBindings(String pattern, List bindings) throws CoreException;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,6 +11,8 @@
|
||||||
|
|
||||||
package org.eclipse.cdt.internal.core.pdom.dom.c;
|
package org.eclipse.cdt.internal.core.pdom.dom.c;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
import org.eclipse.cdt.core.dom.ast.IASTCompoundStatement;
|
import org.eclipse.cdt.core.dom.ast.IASTCompoundStatement;
|
||||||
import org.eclipse.cdt.core.dom.ast.IASTFunctionCallExpression;
|
import org.eclipse.cdt.core.dom.ast.IASTFunctionCallExpression;
|
||||||
import org.eclipse.cdt.core.dom.ast.IASTIdExpression;
|
import org.eclipse.cdt.core.dom.ast.IASTIdExpression;
|
||||||
|
@ -208,4 +210,8 @@ public class PDOMCLinkage extends PDOMLinkage {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void findBindings(String pattern, List bindings) throws CoreException {
|
||||||
|
MatchBinding visitor = new MatchBinding(pdom, pattern, bindings);
|
||||||
|
getIndex().visit(visitor);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,6 +11,8 @@
|
||||||
|
|
||||||
package org.eclipse.cdt.internal.core.pdom.dom.cpp;
|
package org.eclipse.cdt.internal.core.pdom.dom.cpp;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
import org.eclipse.cdt.core.dom.ast.IASTFunctionCallExpression;
|
import org.eclipse.cdt.core.dom.ast.IASTFunctionCallExpression;
|
||||||
import org.eclipse.cdt.core.dom.ast.IASTIdExpression;
|
import org.eclipse.cdt.core.dom.ast.IASTIdExpression;
|
||||||
import org.eclipse.cdt.core.dom.ast.IASTName;
|
import org.eclipse.cdt.core.dom.ast.IASTName;
|
||||||
|
@ -252,4 +254,9 @@ public class PDOMCPPLinkage extends PDOMLinkage {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void findBindings(String pattern, List bindings) throws CoreException {
|
||||||
|
MatchBinding visitor = new MatchBinding(pdom, pattern, bindings);
|
||||||
|
getIndex().visit(visitor);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,9 +11,7 @@
|
||||||
|
|
||||||
package org.eclipse.cdt.internal.ui.search;
|
package org.eclipse.cdt.internal.ui.search;
|
||||||
|
|
||||||
import org.eclipse.cdt.core.dom.ast.IASTFileLocation;
|
|
||||||
import org.eclipse.cdt.internal.core.pdom.dom.PDOMBinding;
|
import org.eclipse.cdt.internal.core.pdom.dom.PDOMBinding;
|
||||||
import org.eclipse.cdt.internal.core.pdom.dom.PDOMName;
|
|
||||||
import org.eclipse.cdt.ui.CUIPlugin;
|
import org.eclipse.cdt.ui.CUIPlugin;
|
||||||
import org.eclipse.core.runtime.CoreException;
|
import org.eclipse.core.runtime.CoreException;
|
||||||
import org.eclipse.core.runtime.IProgressMonitor;
|
import org.eclipse.core.runtime.IProgressMonitor;
|
||||||
|
@ -37,31 +35,7 @@ public class PDOMSearchBindingQuery extends PDOMSearchQuery {
|
||||||
|
|
||||||
public IStatus run(IProgressMonitor monitor) throws OperationCanceledException {
|
public IStatus run(IProgressMonitor monitor) throws OperationCanceledException {
|
||||||
try {
|
try {
|
||||||
if ((flags & FIND_DECLARATIONS) != 0) {
|
createMatches(binding);
|
||||||
PDOMName name = binding.getFirstDeclaration();
|
|
||||||
while (name != null) {
|
|
||||||
IASTFileLocation loc = name.getFileLocation();
|
|
||||||
result.addMatch(new PDOMSearchMatch(name, loc.getNodeOffset(), loc.getNodeLength()));
|
|
||||||
name = name.getNextInBinding();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if ((flags & (FIND_DECLARATIONS)) != 0) {
|
|
||||||
// for decls we do defs too
|
|
||||||
PDOMName name = binding.getFirstDefinition();
|
|
||||||
while (name != null) {
|
|
||||||
IASTFileLocation loc = name.getFileLocation();
|
|
||||||
result.addMatch(new PDOMSearchMatch(name, loc.getNodeOffset(), loc.getNodeLength()));
|
|
||||||
name = name.getNextInBinding();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if ((flags & FIND_REFERENCES) != 0) {
|
|
||||||
PDOMName name = binding.getFirstReference();
|
|
||||||
while (name != null) {
|
|
||||||
IASTFileLocation loc = name.getFileLocation();
|
|
||||||
result.addMatch(new PDOMSearchMatch(name, loc.getNodeOffset(), loc.getNodeLength()));
|
|
||||||
name = name.getNextInBinding();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return Status.OK_STATUS;
|
return Status.OK_STATUS;
|
||||||
} catch (CoreException e) {
|
} catch (CoreException e) {
|
||||||
return new Status(IStatus.ERROR, CUIPlugin.PLUGIN_ID, 0, e.getLocalizedMessage(), e);
|
return new Status(IStatus.ERROR, CUIPlugin.PLUGIN_ID, 0, e.getLocalizedMessage(), e);
|
||||||
|
|
|
@ -234,7 +234,26 @@ public class PDOMSearchPage extends DialogPage implements ISearchPage {
|
||||||
IDialogSettings settings = getDialogSettings();
|
IDialogSettings settings = getDialogSettings();
|
||||||
settings.put(STORE_CASE_SENSITIVE, isCaseSensitive);
|
settings.put(STORE_CASE_SENSITIVE, isCaseSensitive);
|
||||||
|
|
||||||
if (previousPatterns != null)
|
if (previousPatterns == null)
|
||||||
|
previousPatterns = new String[] { pattern };
|
||||||
|
else {
|
||||||
|
// Add only if we don't have it already
|
||||||
|
boolean addit = true;
|
||||||
|
for (int i = 0; i < previousPatterns.length; ++i) {
|
||||||
|
if (pattern.equals(previousPatterns[i])) {
|
||||||
|
addit = false;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (addit) {
|
||||||
|
// Insert it into the beginning of the list
|
||||||
|
String[] newPatterns = new String[previousPatterns.length + 1];
|
||||||
|
System.arraycopy(previousPatterns, 0, newPatterns, 1, previousPatterns.length);
|
||||||
|
newPatterns[0] = pattern;
|
||||||
|
previousPatterns = newPatterns;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
settings.put(STORE_PREVIOUS_PATTERNS, previousPatterns);
|
settings.put(STORE_PREVIOUS_PATTERNS, previousPatterns);
|
||||||
|
|
||||||
settings.put(STORE_SEARCH_FLAGS, searchFlags);
|
settings.put(STORE_SEARCH_FLAGS, searchFlags);
|
||||||
|
@ -452,6 +471,10 @@ public class PDOMSearchPage extends DialogPage implements ISearchPage {
|
||||||
// Int was unitialized, assume the defaults
|
// Int was unitialized, assume the defaults
|
||||||
}
|
}
|
||||||
|
|
||||||
|
previousPatterns = settings.getArray(STORE_PREVIOUS_PATTERNS);
|
||||||
|
if (previousPatterns != null)
|
||||||
|
patternCombo.setItems(previousPatterns);
|
||||||
|
|
||||||
// Initialize the selection
|
// Initialize the selection
|
||||||
ISelection selection = getContainer().getSelection();
|
ISelection selection = getContainer().getSelection();
|
||||||
if (selection instanceof IStructuredSelection) {
|
if (selection instanceof IStructuredSelection) {
|
||||||
|
@ -510,9 +533,6 @@ public class PDOMSearchPage extends DialogPage implements ISearchPage {
|
||||||
// the selection is valid.
|
// the selection is valid.
|
||||||
}
|
}
|
||||||
|
|
||||||
String[] previousPatterns = settings.getArray(STORE_PREVIOUS_PATTERNS);
|
|
||||||
if (previousPatterns != null)
|
|
||||||
patternCombo.setItems(previousPatterns);
|
|
||||||
caseSensitiveButton.setSelection(settings.getBoolean(STORE_CASE_SENSITIVE));
|
caseSensitiveButton.setSelection(settings.getBoolean(STORE_CASE_SENSITIVE));
|
||||||
|
|
||||||
if ((searchFlags & PDOMSearchPatternQuery.FIND_ALL_TYPES) == PDOMSearchPatternQuery.FIND_ALL_TYPES) {
|
if ((searchFlags & PDOMSearchPatternQuery.FIND_ALL_TYPES) == PDOMSearchPatternQuery.FIND_ALL_TYPES) {
|
||||||
|
|
|
@ -11,8 +11,18 @@
|
||||||
|
|
||||||
package org.eclipse.cdt.internal.ui.search;
|
package org.eclipse.cdt.internal.ui.search;
|
||||||
|
|
||||||
import org.eclipse.cdt.core.model.ICElement;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Iterator;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.eclipse.cdt.core.dom.PDOM;
|
||||||
|
import org.eclipse.cdt.core.model.CoreModel;
|
||||||
|
import org.eclipse.cdt.internal.core.pdom.PDOMDatabase;
|
||||||
|
import org.eclipse.cdt.internal.core.pdom.dom.PDOMBinding;
|
||||||
|
import org.eclipse.core.resources.IProject;
|
||||||
import org.eclipse.core.resources.IResource;
|
import org.eclipse.core.resources.IResource;
|
||||||
|
import org.eclipse.core.resources.IWorkspaceRoot;
|
||||||
|
import org.eclipse.core.runtime.CoreException;
|
||||||
import org.eclipse.core.runtime.IProgressMonitor;
|
import org.eclipse.core.runtime.IProgressMonitor;
|
||||||
import org.eclipse.core.runtime.IStatus;
|
import org.eclipse.core.runtime.IStatus;
|
||||||
import org.eclipse.core.runtime.OperationCanceledException;
|
import org.eclipse.core.runtime.OperationCanceledException;
|
||||||
|
@ -58,8 +68,37 @@ public class PDOMSearchPatternQuery extends PDOMSearchQuery {
|
||||||
}
|
}
|
||||||
|
|
||||||
public IStatus run(IProgressMonitor monitor) throws OperationCanceledException {
|
public IStatus run(IProgressMonitor monitor) throws OperationCanceledException {
|
||||||
// TODO Auto-generated method stub
|
// get the list of projects we need to search
|
||||||
|
List projects = new ArrayList();
|
||||||
|
for (int i = 0; i < scope.length; ++i) {
|
||||||
|
if (scope[i] instanceof IWorkspaceRoot) {
|
||||||
|
IProject[] p = ((IWorkspaceRoot)scope[i]).getProjects();
|
||||||
|
for (int j = 0; j < p.length; ++j)
|
||||||
|
projects.add(p[j]);
|
||||||
|
} else
|
||||||
|
projects.add(scope[i].getProject());
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
for (Iterator iproject = projects.iterator(); iproject.hasNext();)
|
||||||
|
searchProject((IProject)iproject.next());
|
||||||
return Status.OK_STATUS;
|
return Status.OK_STATUS;
|
||||||
|
} catch (CoreException e) {
|
||||||
|
return e.getStatus();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void searchProject(IProject project) throws CoreException {
|
||||||
|
if (!CoreModel.hasCNature(project))
|
||||||
|
// Not a CDT project
|
||||||
|
return;
|
||||||
|
|
||||||
|
PDOMDatabase pdom = (PDOMDatabase)PDOM.getPDOM(project);
|
||||||
|
PDOMBinding[] bindings = pdom.findBindings(pattern);
|
||||||
|
|
||||||
|
for (int i = 0; i < bindings.length; ++i) {
|
||||||
|
createMatches(bindings[i]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getLabel() {
|
public String getLabel() {
|
||||||
|
|
|
@ -11,6 +11,10 @@
|
||||||
|
|
||||||
package org.eclipse.cdt.internal.ui.search;
|
package org.eclipse.cdt.internal.ui.search;
|
||||||
|
|
||||||
|
import org.eclipse.cdt.core.dom.ast.IASTFileLocation;
|
||||||
|
import org.eclipse.cdt.internal.core.pdom.dom.PDOMBinding;
|
||||||
|
import org.eclipse.cdt.internal.core.pdom.dom.PDOMName;
|
||||||
|
import org.eclipse.core.runtime.CoreException;
|
||||||
import org.eclipse.search.ui.ISearchQuery;
|
import org.eclipse.search.ui.ISearchQuery;
|
||||||
import org.eclipse.search.ui.ISearchResult;
|
import org.eclipse.search.ui.ISearchResult;
|
||||||
|
|
||||||
|
@ -55,4 +59,37 @@ public abstract class PDOMSearchQuery implements ISearchQuery {
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return true to filter name out of the match list.
|
||||||
|
* Override in a subclass to add scoping.
|
||||||
|
* @param name
|
||||||
|
* @return true to filter name out of the match list
|
||||||
|
*/
|
||||||
|
protected boolean filterName(PDOMName name) {
|
||||||
|
return false; // i.e. keep it
|
||||||
|
}
|
||||||
|
|
||||||
|
private void collectNames(PDOMName name) throws CoreException {
|
||||||
|
while (name != null) {
|
||||||
|
if (!filterName(name)) {
|
||||||
|
IASTFileLocation loc = name.getFileLocation();
|
||||||
|
result.addMatch(new PDOMSearchMatch(name, loc.getNodeOffset(), loc.getNodeLength()));
|
||||||
|
name = name.getNextInBinding();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void createMatches(PDOMBinding binding) throws CoreException {
|
||||||
|
if ((flags & FIND_DECLARATIONS) != 0) {
|
||||||
|
collectNames(binding.getFirstDeclaration());
|
||||||
|
}
|
||||||
|
if ((flags & FIND_DECLARATIONS) != 0) {
|
||||||
|
collectNames(binding.getFirstDefinition());
|
||||||
|
}
|
||||||
|
if ((flags & FIND_REFERENCES) != 0) {
|
||||||
|
collectNames(binding.getFirstReference());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue