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
# are made available under the terms of the Eclipse Public License v1.0
# 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.
WorkspaceScope= Workspace
WorkingSetScope= Working Set - {0}
SelectionScope= Selection
ProjectScope = Project
WorkspaceScope= workspace
WorkingSetScope= working set - {0}
SelectionScope= selection
ProjectScope = project
PDOMSearchQuery_refs_label = Search references
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
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
@ -8,16 +8,17 @@
* Contributors:
* QNX - Initial API and implementation
* IBM Corporation
* Markus Schorn (Wind River Systems)
*******************************************************************************/
package org.eclipse.cdt.internal.ui.search;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.HashSet;
import java.util.Set;
import java.util.regex.PatternSyntaxException;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.jface.action.IStatusLineManager;
import org.eclipse.jface.dialogs.Dialog;
@ -55,8 +56,11 @@ import org.eclipse.ui.IWorkingSet;
import org.eclipse.ui.PlatformUI;
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.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.ui.CUIPlugin;
@ -150,15 +154,26 @@ public class PDOMSearchPage extends DialogPage implements ISearchPage {
private IStatusLineManager fLineManager;
private static ICProject getProject(Object object) {
if (object instanceof ICElement) {
return ((ICElement) object).getCProject();
} else if (object instanceof IResource) {
return CoreModel.getDefault().create(((IResource) object).getProject());
} else {
private static ICProject getProject(String name) {
return CoreModel.getDefault().create(ResourcesPlugin.getWorkspace().getRoot().getProject(name));
}
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() {
fLineManager.setErrorMessage(null);
@ -184,28 +199,54 @@ public class PDOMSearchPage extends DialogPage implements ISearchPage {
}
// 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$
switch (getContainer().getSelectedScope()) {
case ISearchPageContainer.SELECTED_PROJECTS_SCOPE:
if (structuredSelection != null) {
final String[] prjNames = getContainer().getSelectedProjectNames();
scopeDescription= CSearchMessages.ProjectScope;
for (Iterator<?> i = structuredSelection.iterator(); i.hasNext();) {
ICProject project = getProject(i.next());
if (project != null)
int ip= 0;
for (String prjName: prjNames) {
ICProject project = getProject(prjName);
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;
case ISearchPageContainer.SELECTION_SCOPE:
if (structuredSelection != null) {
scopeDescription = CSearchMessages.SelectionScope;
for (Iterator<?> i = structuredSelection.iterator(); i.hasNext();) {
Object obj = i.next();
if (obj instanceof IResource) {
elements.add(CoreModel.getDefault().create((IResource)obj));
} else if (obj instanceof ICElement) {
elements.add(obj);
int ie= 0;
for (Object sel : structuredSelection.toList()) {
ICElement elem= getElement(sel);
if (elem != null) {
elements.add(elem);
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;
@ -221,9 +262,9 @@ public class PDOMSearchPage extends DialogPage implements ISearchPage {
for (int i = 0; i < workingSets.length; ++i) {
IAdaptable[] wsElements = workingSets[i].getElements();
for (int j = 0; j < wsElements.length; ++j) {
ICProject project = getProject(wsElements[j]);
if (project != null)
elements.add(project);
ICElement elem = getElement(wsElements[j]);
if (elem != null)
elements.add(elem);
}
}
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
* are made available under the terms of the Eclipse Public License v1.0
* 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.OperationCanceledException;
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.IEnumeration;
@ -197,8 +196,6 @@ public class PDOMSearchPatternQuery extends PDOMSearchQuery {
@Override
public String getResultLabel(int numMatches) {
String patternInScope = NLS.bind(
CSearchMessages.PDOMSearchPatternQuery_PatternQuery_labelPatternInScope, patternStr, scopeDesc);
return getResultLabel(patternInScope, numMatches);
return getResultLabel(patternStr, scopeDesc, 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
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
@ -24,6 +24,8 @@ import java.util.Map;
import java.util.Map.Entry;
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.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
@ -89,6 +91,7 @@ public abstract class PDOMSearchQuery implements ISearchQuery {
protected ICElement[] scope;
protected ICProject[] projects;
private Set<String> fullPathFilter;
protected PDOMSearchQuery(ICElement[] scope, int flags) {
result = new PDOMSearchResult(this);
@ -110,15 +113,24 @@ public abstract class PDOMSearchQuery implements ISearchQuery {
projects = (ICProject[]) ArrayUtil.removeNulls(ICProject.class, allProjects);
} else {
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) {
ICProject project = scope[i].getCProject();
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);
}
}
projects = projectMap.values().toArray(new ICProject[projectMap.size()]);
if (needFilter) {
fullPathFilter= pathFilter;
}
}
} catch (CoreException e) {
CUIPlugin.log(e);
@ -151,6 +163,10 @@ public abstract class PDOMSearchQuery implements ISearchQuery {
public abstract String getResultLabel(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
String label;
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);
break;
}
if (scope != null)
label= NLS.bind(CSearchMessages.PDOMSearchPatternQuery_PatternQuery_labelPatternInScope, label, scope);
String countLabel = Messages.format(CSearchMessages.CSearchResultCollector_matches, new Integer(
matchCount));
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 {
IIndexName[] bindingNames= index.findNames(binding, flags);
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 {