1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-04-29 19:45:01 +02:00

Bug 196479: Search works with project granularity, only.

This commit is contained in:
Markus Schorn 2011-04-11 15:03:58 +00:00
parent 6fffb452a8
commit f3dc616c97
4 changed files with 119 additions and 42 deletions

View file

@ -1,5 +1,5 @@
############################################################################### ###############################################################################
# Copyright (c) 2000, 2009 IBM Corporation and others. # Copyright (c) 2000, 2011 IBM Corporation and others.
# All rights reserved. This program and the accompanying materials # All rights reserved. This program and the accompanying materials
# are made available under the terms of the Eclipse Public License v1.0 # are made available under the terms of the Eclipse Public License v1.0
# which accompanies this distribution, and is available at # which accompanies this distribution, and is available at
@ -62,10 +62,10 @@ CSearch_FindReferencesInWorkingSetAction_tooltip= Search for References to the S
CSearchOperation_operationUnavailable_message= The operation is unavailable on the current selection. CSearchOperation_operationUnavailable_message= The operation is unavailable on the current selection.
WorkspaceScope= Workspace WorkspaceScope= workspace
WorkingSetScope= Working Set - {0} WorkingSetScope= working set - {0}
SelectionScope= Selection SelectionScope= selection
ProjectScope = Project ProjectScope = project
PDOMSearchQuery_refs_label = Search references PDOMSearchQuery_refs_label = Search references
PDOMSearchQuery_defs_label = Search definitions PDOMSearchQuery_defs_label = Search definitions

View file

@ -1,5 +1,5 @@
/******************************************************************************* /*******************************************************************************
* Copyright (c) 2006, 2010 QNX Software Systems and others. * Copyright (c) 2006, 2011 QNX Software Systems and others.
* All rights reserved. This program and the accompanying materials * All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0 * are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at * which accompanies this distribution, and is available at
@ -8,16 +8,17 @@
* Contributors: * Contributors:
* QNX - Initial API and implementation * QNX - Initial API and implementation
* IBM Corporation * IBM Corporation
* Markus Schorn (Wind River Systems)
*******************************************************************************/ *******************************************************************************/
package org.eclipse.cdt.internal.ui.search; package org.eclipse.cdt.internal.ui.search;
import java.util.ArrayList; import java.util.HashSet;
import java.util.Iterator; import java.util.Set;
import java.util.List;
import java.util.regex.PatternSyntaxException; import java.util.regex.PatternSyntaxException;
import org.eclipse.core.resources.IResource; import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.IAdaptable; import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.jface.action.IStatusLineManager; import org.eclipse.jface.action.IStatusLineManager;
import org.eclipse.jface.dialogs.Dialog; import org.eclipse.jface.dialogs.Dialog;
@ -55,8 +56,11 @@ import org.eclipse.ui.IWorkingSet;
import org.eclipse.ui.PlatformUI; import org.eclipse.ui.PlatformUI;
import org.eclipse.cdt.core.model.CoreModel; import org.eclipse.cdt.core.model.CoreModel;
import org.eclipse.cdt.core.model.ICContainer;
import org.eclipse.cdt.core.model.ICElement; import org.eclipse.cdt.core.model.ICElement;
import org.eclipse.cdt.core.model.ICProject; import org.eclipse.cdt.core.model.ICProject;
import org.eclipse.cdt.core.model.ISourceReference;
import org.eclipse.cdt.core.model.ITranslationUnit;
import org.eclipse.cdt.core.parser.Keywords; import org.eclipse.cdt.core.parser.Keywords;
import org.eclipse.cdt.ui.CUIPlugin; import org.eclipse.cdt.ui.CUIPlugin;
@ -150,16 +154,27 @@ public class PDOMSearchPage extends DialogPage implements ISearchPage {
private IStatusLineManager fLineManager; private IStatusLineManager fLineManager;
private static ICProject getProject(Object object) { private static ICProject getProject(String name) {
if (object instanceof ICElement) { return CoreModel.getDefault().create(ResourcesPlugin.getWorkspace().getRoot().getProject(name));
return ((ICElement) object).getCProject();
} else if (object instanceof IResource) {
return CoreModel.getDefault().create(((IResource) object).getProject());
} else {
return null;
}
} }
private ICElement getElement(Object obj) {
if (obj instanceof IResource) {
return CoreModel.getDefault().create((IResource)obj);
}
if (obj instanceof ICElement) {
ICElement elem= (ICElement) obj;
if (elem instanceof ISourceReference)
return ((ISourceReference) elem).getTranslationUnit();
if (elem instanceof ITranslationUnit || elem instanceof ICContainer || elem instanceof ICProject)
return elem;
return elem.getCProject();
}
return null;
}
public boolean performAction() { public boolean performAction() {
fLineManager.setErrorMessage(null); fLineManager.setErrorMessage(null);
@ -184,28 +199,54 @@ public class PDOMSearchPage extends DialogPage implements ISearchPage {
} }
// get the list of elements for the scope // get the list of elements for the scope
List<Object> elements = new ArrayList<Object>(); Set<ICElement> elements = new HashSet<ICElement>();
String scopeDescription = ""; //$NON-NLS-1$ String scopeDescription = ""; //$NON-NLS-1$
switch (getContainer().getSelectedScope()) { switch (getContainer().getSelectedScope()) {
case ISearchPageContainer.SELECTED_PROJECTS_SCOPE: case ISearchPageContainer.SELECTED_PROJECTS_SCOPE:
if (structuredSelection != null) { final String[] prjNames = getContainer().getSelectedProjectNames();
scopeDescription = CSearchMessages.ProjectScope; scopeDescription= CSearchMessages.ProjectScope;
for (Iterator<?> i = structuredSelection.iterator(); i.hasNext();) { int ip= 0;
ICProject project = getProject(i.next()); for (String prjName: prjNames) {
if (project != null) ICProject project = getProject(prjName);
elements.add(project); if (project != null) {
elements.add(project);
switch(ip++) {
case 0:
scopeDescription+= " '" + prjName + "'"; //$NON-NLS-1$//$NON-NLS-2$
break;
case 1:
scopeDescription= scopeDescription + ", '" + prjName + "'"; //$NON-NLS-1$ //$NON-NLS-2$
break;
case 2:
scopeDescription+= ", ..."; //$NON-NLS-1$
break;
default:
break;
}
} }
} }
break; break;
case ISearchPageContainer.SELECTION_SCOPE: case ISearchPageContainer.SELECTION_SCOPE:
if (structuredSelection != null) { if (structuredSelection != null) {
scopeDescription = CSearchMessages.SelectionScope; scopeDescription = CSearchMessages.SelectionScope;
for (Iterator<?> i = structuredSelection.iterator(); i.hasNext();) { int ie= 0;
Object obj = i.next(); for (Object sel : structuredSelection.toList()) {
if (obj instanceof IResource) { ICElement elem= getElement(sel);
elements.add(CoreModel.getDefault().create((IResource)obj)); if (elem != null) {
} else if (obj instanceof ICElement) { elements.add(elem);
elements.add(obj); switch(ie++) {
case 0:
scopeDescription= " '" + elem.toString() + "'"; //$NON-NLS-1$//$NON-NLS-2$
break;
case 1:
scopeDescription= scopeDescription + ", '" + elem.toString() + "'"; //$NON-NLS-1$ //$NON-NLS-2$
break;
case 2:
scopeDescription+= ", ..."; //$NON-NLS-1$
break;
default:
break;
}
} }
} }
break; break;
@ -221,9 +262,9 @@ public class PDOMSearchPage extends DialogPage implements ISearchPage {
for (int i = 0; i < workingSets.length; ++i) { for (int i = 0; i < workingSets.length; ++i) {
IAdaptable[] wsElements = workingSets[i].getElements(); IAdaptable[] wsElements = workingSets[i].getElements();
for (int j = 0; j < wsElements.length; ++j) { for (int j = 0; j < wsElements.length; ++j) {
ICProject project = getProject(wsElements[j]); ICElement elem = getElement(wsElements[j]);
if (project != null) if (elem != null)
elements.add(project); elements.add(elem);
} }
} }
break; break;

View file

@ -1,5 +1,5 @@
/******************************************************************************* /*******************************************************************************
* Copyright (c) 2006, 2010 QNX Software Systems and others. * Copyright (c) 2006, 2011 QNX Software Systems and others.
* All rights reserved. This program and the accompanying materials * All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0 * are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at * which accompanies this distribution, and is available at
@ -23,7 +23,6 @@ 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;
import org.eclipse.core.runtime.Status; import org.eclipse.core.runtime.Status;
import org.eclipse.osgi.util.NLS;
import org.eclipse.cdt.core.dom.ast.ICompositeType; import org.eclipse.cdt.core.dom.ast.ICompositeType;
import org.eclipse.cdt.core.dom.ast.IEnumeration; import org.eclipse.cdt.core.dom.ast.IEnumeration;
@ -197,8 +196,6 @@ public class PDOMSearchPatternQuery extends PDOMSearchQuery {
@Override @Override
public String getResultLabel(int numMatches) { public String getResultLabel(int numMatches) {
String patternInScope = NLS.bind( return getResultLabel(patternStr, scopeDesc, numMatches);
CSearchMessages.PDOMSearchPatternQuery_PatternQuery_labelPatternInScope, patternStr, scopeDesc);
return getResultLabel(patternInScope, numMatches);
} }
} }

View file

@ -1,5 +1,5 @@
/******************************************************************************* /*******************************************************************************
* Copyright (c) 2006, 2010 QNX Software Systems and others. * Copyright (c) 2006, 2011 QNX Software Systems and others.
* All rights reserved. This program and the accompanying materials * All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0 * are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at * which accompanies this distribution, and is available at
@ -24,6 +24,8 @@ import java.util.Map;
import java.util.Map.Entry; import java.util.Map.Entry;
import java.util.Set; import java.util.Set;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath; import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor; import org.eclipse.core.runtime.IProgressMonitor;
@ -89,6 +91,7 @@ public abstract class PDOMSearchQuery implements ISearchQuery {
protected ICElement[] scope; protected ICElement[] scope;
protected ICProject[] projects; protected ICProject[] projects;
private Set<String> fullPathFilter;
protected PDOMSearchQuery(ICElement[] scope, int flags) { protected PDOMSearchQuery(ICElement[] scope, int flags) {
result = new PDOMSearchResult(this); result = new PDOMSearchResult(this);
@ -110,15 +113,24 @@ public abstract class PDOMSearchQuery implements ISearchQuery {
projects = (ICProject[]) ArrayUtil.removeNulls(ICProject.class, allProjects); projects = (ICProject[]) ArrayUtil.removeNulls(ICProject.class, allProjects);
} else { } else {
Map<String, ICProject> projectMap = new HashMap<String, ICProject>(); Map<String, ICProject> projectMap = new HashMap<String, ICProject>();
Set<String> pathFilter = new HashSet<String>();
boolean needFilter= false;
for (int i = 0; i < scope.length; ++i) { for (int i = 0; i < scope.length; ++i) {
ICProject project = scope[i].getCProject(); ICProject project = scope[i].getCProject();
if (project != null && project.getProject().isOpen()) { if (project != null && project.getProject().isOpen()) {
IResource res= scope[i].getResource();
if (res != null) {
pathFilter.add(res.getFullPath().toString());
needFilter= needFilter || !(res instanceof IProject);
}
projectMap.put(project.getElementName(), project); projectMap.put(project.getElementName(), project);
} }
} }
projects = projectMap.values().toArray(new ICProject[projectMap.size()]); projects = projectMap.values().toArray(new ICProject[projectMap.size()]);
if (needFilter) {
fullPathFilter= pathFilter;
}
} }
} catch (CoreException e) { } catch (CoreException e) {
CUIPlugin.log(e); CUIPlugin.log(e);
@ -149,8 +161,12 @@ public abstract class PDOMSearchQuery implements ISearchQuery {
} }
public abstract String getResultLabel(int matchCount); public abstract String getResultLabel(int matchCount);
public String getResultLabel(String pattern, int matchCount) { public String getResultLabel(String pattern, int matchCount) {
return getResultLabel(pattern, null, matchCount);
}
public String getResultLabel(String pattern, String scope, int matchCount) {
// Report pattern and number of matches // Report pattern and number of matches
String label; String label;
final int kindFlags= flags & FIND_ALL_OCCURRENCES; final int kindFlags= flags & FIND_ALL_OCCURRENCES;
@ -171,6 +187,10 @@ public abstract class PDOMSearchQuery implements ISearchQuery {
label = NLS.bind(CSearchMessages.PDOMSearchQuery_occurrences_result_label, pattern); label = NLS.bind(CSearchMessages.PDOMSearchQuery_occurrences_result_label, pattern);
break; break;
} }
if (scope != null)
label= NLS.bind(CSearchMessages.PDOMSearchPatternQuery_PatternQuery_labelPatternInScope, label, scope);
String countLabel = Messages.format(CSearchMessages.CSearchResultCollector_matches, new Integer( String countLabel = Messages.format(CSearchMessages.CSearchResultCollector_matches, new Integer(
matchCount)); matchCount));
return label + " " + countLabel; //$NON-NLS-1$ return label + " " + countLabel; //$NON-NLS-1$
@ -355,7 +375,26 @@ public abstract class PDOMSearchQuery implements ISearchQuery {
private void createMatches1(IIndex index, IBinding binding, List<IIndexName> names) throws CoreException { private void createMatches1(IIndex index, IBinding binding, List<IIndexName> names) throws CoreException {
IIndexName[] bindingNames= index.findNames(binding, flags); IIndexName[] bindingNames= index.findNames(binding, flags);
names.addAll(Arrays.asList(bindingNames)); if (fullPathFilter == null) {
names.addAll(Arrays.asList(bindingNames));
} else {
for (IIndexName name : bindingNames) {
String fullPath= name.getFile().getLocation().getFullPath();
if (accept(fullPath))
names.add(name);
}
}
}
private boolean accept(String fullPath) {
for(;;) {
if (fullPathFilter.contains(fullPath))
return true;
int idx= fullPath.lastIndexOf('/');
if (idx < 0)
return false;
fullPath= fullPath.substring(0, idx);
}
} }
protected void createLocalMatches(IASTTranslationUnit ast, IBinding binding) throws CoreException { protected void createLocalMatches(IASTTranslationUnit ast, IBinding binding) throws CoreException {