mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-07-23 08:55:25 +02:00
79193: fix rendering of external paths in C++ search page
This commit is contained in:
parent
849223972d
commit
212cb73714
8 changed files with 164 additions and 33 deletions
|
@ -11,6 +11,9 @@
|
|||
package org.eclipse.cdt.ui.tests.search;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.File;
|
||||
import java.io.FileWriter;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
|
||||
import junit.framework.TestSuite;
|
||||
|
||||
|
@ -18,6 +21,17 @@ import org.eclipse.core.resources.IFile;
|
|||
import org.eclipse.core.resources.IResource;
|
||||
import org.eclipse.core.runtime.NullProgressMonitor;
|
||||
import org.eclipse.core.runtime.Path;
|
||||
import org.eclipse.jface.operation.IRunnableContext;
|
||||
import org.eclipse.jface.operation.IRunnableWithProgress;
|
||||
import org.eclipse.jface.viewers.ILabelProvider;
|
||||
import org.eclipse.jface.viewers.IStructuredContentProvider;
|
||||
import org.eclipse.jface.viewers.StructuredViewer;
|
||||
import org.eclipse.search.ui.IQueryListener;
|
||||
import org.eclipse.search.ui.ISearchQuery;
|
||||
import org.eclipse.search.ui.ISearchResult;
|
||||
import org.eclipse.search.ui.ISearchResultPage;
|
||||
import org.eclipse.search.ui.ISearchResultViewPart;
|
||||
import org.eclipse.search.ui.NewSearchUI;
|
||||
import org.osgi.framework.Bundle;
|
||||
|
||||
import org.eclipse.cdt.core.CCorePlugin;
|
||||
|
@ -25,15 +39,17 @@ import org.eclipse.cdt.core.dom.IPDOMManager;
|
|||
import org.eclipse.cdt.core.model.ICElement;
|
||||
import org.eclipse.cdt.core.model.ICProject;
|
||||
import org.eclipse.cdt.core.testplugin.CProjectHelper;
|
||||
import org.eclipse.cdt.core.testplugin.util.BaseTestCase;
|
||||
import org.eclipse.cdt.core.testplugin.TestScannerProvider;
|
||||
import org.eclipse.cdt.core.testplugin.util.TestSourceReader;
|
||||
import org.eclipse.cdt.ui.testplugin.CTestPlugin;
|
||||
import org.eclipse.cdt.ui.tests.BaseUITestCase;
|
||||
|
||||
import org.eclipse.cdt.internal.ui.search.PDOMSearchPatternQuery;
|
||||
import org.eclipse.cdt.internal.ui.search.PDOMSearchQuery;
|
||||
import org.eclipse.cdt.internal.ui.search.PDOMSearchResult;
|
||||
import org.eclipse.cdt.internal.ui.search.PDOMSearchViewPage;
|
||||
|
||||
public class BasicSearchTest extends BaseTestCase {
|
||||
public class BasicSearchTest extends BaseUITestCase {
|
||||
ICProject fCProject;
|
||||
StringBuffer[] testData;
|
||||
|
||||
|
@ -60,6 +76,79 @@ public class BasicSearchTest extends BaseTestCase {
|
|||
}
|
||||
}
|
||||
|
||||
// // empty
|
||||
|
||||
// #include "extHead.h"
|
||||
// void bar() {
|
||||
// foo();
|
||||
// }
|
||||
public void testExternalPathRenderedCorrectly_79193() throws Exception {
|
||||
// make an external file
|
||||
File dir= CProjectHelper.freshDir();
|
||||
File externalFile= new File(dir, "extHead.h");
|
||||
externalFile.deleteOnExit();
|
||||
FileWriter fw= new FileWriter(externalFile);
|
||||
fw.write("void foo() {}\n");
|
||||
fw.close();
|
||||
|
||||
// rebuild the index
|
||||
TestScannerProvider.sIncludes= new String[] {dir.getAbsolutePath()};
|
||||
CCorePlugin.getIndexManager().reindex(fCProject);
|
||||
assertTrue(CCorePlugin.getIndexManager().joinIndexer(360000, new NullProgressMonitor()));
|
||||
|
||||
// open a query
|
||||
PDOMSearchQuery query= makeProjectQuery("foo");
|
||||
PDOMSearchResult result= runQuery(query);
|
||||
assertEquals(2, result.getElements().length);
|
||||
|
||||
ISearchResultViewPart vp= NewSearchUI.getSearchResultView();
|
||||
ISearchResultPage page= vp.getActivePage();
|
||||
assertTrue(""+page, page instanceof PDOMSearchViewPage);
|
||||
|
||||
PDOMSearchViewPage pdomsvp= (PDOMSearchViewPage) page;
|
||||
StructuredViewer viewer= pdomsvp.getViewer();
|
||||
ILabelProvider labpv= (ILabelProvider) viewer.getLabelProvider();
|
||||
IStructuredContentProvider scp= (IStructuredContentProvider) viewer.getContentProvider();
|
||||
|
||||
Object result0= result.getElements()[0];
|
||||
Object result1= result.getElements()[1];
|
||||
|
||||
// check the results are rendered
|
||||
String expected0= fCProject.getProject().getName();
|
||||
String expected1= new Path(externalFile.getAbsolutePath()).toString();
|
||||
assertEquals(expected0,labpv.getText(scp.getElements(result)[0]));
|
||||
assertEquals(expected1,labpv.getText(scp.getElements(result)[1]));
|
||||
}
|
||||
|
||||
/**
|
||||
* Run the specified query, and return the result. When this method returns the
|
||||
* search page will have been opened.
|
||||
* @param query
|
||||
* @return
|
||||
*/
|
||||
protected PDOMSearchResult runQuery(PDOMSearchQuery query) {
|
||||
final ISearchResult result[]= new ISearchResult[1];
|
||||
IQueryListener listener= new IQueryListener() {
|
||||
public void queryAdded(ISearchQuery query) {}
|
||||
public void queryFinished(ISearchQuery query) {
|
||||
result[0]= query.getSearchResult();
|
||||
}
|
||||
public void queryRemoved(ISearchQuery query) {}
|
||||
public void queryStarting(ISearchQuery query) {}
|
||||
};
|
||||
NewSearchUI.addQueryListener(listener);
|
||||
NewSearchUI.runQueryInForeground(new IRunnableContext() {
|
||||
public void run(boolean fork, boolean cancelable,
|
||||
IRunnableWithProgress runnable)
|
||||
throws InvocationTargetException, InterruptedException {
|
||||
runnable.run(NPM);
|
||||
}
|
||||
}, query);
|
||||
assertTrue(result[0] instanceof PDOMSearchResult);
|
||||
runEventQueue(500);
|
||||
return (PDOMSearchResult) result[0];
|
||||
}
|
||||
|
||||
// void foo() {}
|
||||
|
||||
// void bar() {
|
||||
|
|
|
@ -13,12 +13,12 @@
|
|||
package org.eclipse.cdt.internal.ui.search;
|
||||
|
||||
import org.eclipse.core.runtime.CoreException;
|
||||
import org.eclipse.core.runtime.Path;
|
||||
|
||||
import org.eclipse.cdt.core.browser.ITypeInfo;
|
||||
import org.eclipse.cdt.core.browser.IndexTypeInfo;
|
||||
import org.eclipse.cdt.core.index.IIndex;
|
||||
import org.eclipse.cdt.core.index.IIndexBinding;
|
||||
import org.eclipse.cdt.core.index.IIndexFileLocation;
|
||||
import org.eclipse.cdt.core.index.IIndexName;
|
||||
|
||||
/**
|
||||
|
@ -29,15 +29,15 @@ import org.eclipse.cdt.core.index.IIndexName;
|
|||
public class PDOMSearchElement {
|
||||
|
||||
private final ITypeInfo typeInfo;
|
||||
private final String filename;
|
||||
private final IIndexFileLocation location;
|
||||
|
||||
public PDOMSearchElement(IIndex index, IIndexName name, IIndexBinding binding) throws CoreException {
|
||||
this.typeInfo= IndexTypeInfo.create(index, binding);
|
||||
filename = new Path(name.getFileLocation().getFileName()).toOSString();
|
||||
this.location= name.getFile().getLocation();
|
||||
}
|
||||
|
||||
public int hashCode() {
|
||||
return (typeInfo.getCElementType() *31 + typeInfo.getName().hashCode())*31 + filename.hashCode();
|
||||
return (typeInfo.getCElementType() *31 + typeInfo.getName().hashCode())*31 + location.hashCode();
|
||||
}
|
||||
|
||||
public boolean equals(Object obj) {
|
||||
|
@ -48,14 +48,14 @@ public class PDOMSearchElement {
|
|||
PDOMSearchElement other = (PDOMSearchElement)obj;
|
||||
return typeInfo.getCElementType() == other.typeInfo.getCElementType()
|
||||
&& typeInfo.getName().equals(other.typeInfo.getName())
|
||||
&& filename.equals(other.filename);
|
||||
&& location.equals(other.location);
|
||||
}
|
||||
|
||||
public ITypeInfo getTypeInfo() {
|
||||
return typeInfo;
|
||||
}
|
||||
|
||||
public String getFileName() {
|
||||
return filename;
|
||||
IIndexFileLocation getLocation() {
|
||||
return location;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -11,12 +11,16 @@
|
|||
|
||||
package org.eclipse.cdt.internal.ui.search;
|
||||
|
||||
import org.eclipse.core.runtime.IPath;
|
||||
import org.eclipse.jface.viewers.LabelProvider;
|
||||
import org.eclipse.search.ui.text.AbstractTextSearchViewPage;
|
||||
import org.eclipse.swt.graphics.Image;
|
||||
|
||||
import org.eclipse.cdt.core.index.IIndexFileLocation;
|
||||
import org.eclipse.cdt.core.index.IndexLocationFactory;
|
||||
import org.eclipse.cdt.ui.browser.typeinfo.TypeInfoLabelProvider;
|
||||
|
||||
import org.eclipse.cdt.internal.ui.CPluginImages;
|
||||
import org.eclipse.cdt.internal.ui.viewsupport.CElementImageProvider;
|
||||
import org.eclipse.cdt.internal.ui.viewsupport.CUILabelProvider;
|
||||
|
||||
|
@ -40,13 +44,25 @@ public class PDOMSearchLabelProvider extends LabelProvider {
|
|||
if (element instanceof PDOMSearchElement)
|
||||
return fTypeInfoLabelProvider.getImage(((PDOMSearchElement)element).getTypeInfo());
|
||||
|
||||
if (element instanceof IIndexFileLocation) {
|
||||
return CPluginImages.get(CPluginImages.IMG_OBJS_INCLUDE);
|
||||
}
|
||||
|
||||
return fCElementLabelProvider.getImage(element);
|
||||
}
|
||||
|
||||
public String getText(Object element) {
|
||||
if (element instanceof PDOMSearchElement) {
|
||||
return fTypeInfoLabelProvider.getText(((PDOMSearchElement)element).getTypeInfo());
|
||||
}
|
||||
}
|
||||
|
||||
if (element instanceof IIndexFileLocation) {
|
||||
IPath path= IndexLocationFactory.getPath((IIndexFileLocation)element);
|
||||
if(path!=null) {
|
||||
return path.toString();
|
||||
}
|
||||
}
|
||||
|
||||
return fCElementLabelProvider.getText(element);
|
||||
}
|
||||
|
||||
|
|
|
@ -12,8 +12,12 @@
|
|||
|
||||
package org.eclipse.cdt.internal.ui.search;
|
||||
|
||||
import org.eclipse.core.runtime.IPath;
|
||||
import org.eclipse.search.ui.text.AbstractTextSearchViewPage;
|
||||
|
||||
import org.eclipse.cdt.core.index.IIndexFileLocation;
|
||||
import org.eclipse.cdt.core.index.IndexLocationFactory;
|
||||
|
||||
/**
|
||||
* @author Doug Schaefer
|
||||
*
|
||||
|
@ -26,13 +30,22 @@ public class PDOMSearchListLabelProvider extends PDOMSearchLabelProvider {
|
|||
|
||||
public String getText(Object element) {
|
||||
final String text= super.getText(element);
|
||||
|
||||
if (element instanceof PDOMSearchElement) {
|
||||
PDOMSearchElement searchElement = (PDOMSearchElement)element;
|
||||
final int count= getMatchCount(element);
|
||||
final String filename = " - " + searchElement.getFileName(); //$NON-NLS-1$
|
||||
final String filename = " - " + IndexLocationFactory.getPath(searchElement.getLocation()); //$NON-NLS-1$
|
||||
return text + filename + " " //$NON-NLS-1$
|
||||
+ CSearchMessages.getFormattedString("CSearchResultCollector.matches", new Integer(count)); //$NON-NLS-1$
|
||||
}
|
||||
|
||||
if (element instanceof IIndexFileLocation) {
|
||||
IPath path= IndexLocationFactory.getPath((IIndexFileLocation)element);
|
||||
if(path!=null) {
|
||||
return path.toString();
|
||||
}
|
||||
}
|
||||
|
||||
return text;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -17,6 +17,7 @@ import org.eclipse.search.ui.text.Match;
|
|||
|
||||
import org.eclipse.cdt.core.index.IIndex;
|
||||
import org.eclipse.cdt.core.index.IIndexBinding;
|
||||
import org.eclipse.cdt.core.index.IIndexFileLocation;
|
||||
import org.eclipse.cdt.core.index.IIndexName;
|
||||
|
||||
/**
|
||||
|
@ -29,8 +30,8 @@ public class PDOMSearchMatch extends Match {
|
|||
super(new PDOMSearchElement(index, name, binding), offset, length);
|
||||
}
|
||||
|
||||
public String getFileName() throws CoreException {
|
||||
return ((PDOMSearchElement)getElement()).getFileName();
|
||||
IIndexFileLocation getLocation() {
|
||||
return ((PDOMSearchElement)getElement()).getLocation();
|
||||
}
|
||||
|
||||
public boolean equals(Object obj) {
|
||||
|
|
|
@ -36,6 +36,7 @@ import org.eclipse.ui.part.FileEditorInput;
|
|||
|
||||
import org.eclipse.cdt.core.index.IIndexFileLocation;
|
||||
import org.eclipse.cdt.core.index.IIndexName;
|
||||
import org.eclipse.cdt.core.index.IndexLocationFactory;
|
||||
import org.eclipse.cdt.ui.CUIPlugin;
|
||||
|
||||
import org.eclipse.cdt.internal.ui.util.ExternalEditorInput;
|
||||
|
@ -91,22 +92,18 @@ public class PDOMSearchResult extends AbstractTextSearchResult implements IEdito
|
|||
}
|
||||
|
||||
public boolean isShownInEditor(Match match, IEditorPart editor) {
|
||||
try {
|
||||
String filename = getFileName(editor);
|
||||
if (filename != null && match instanceof PDOMSearchMatch)
|
||||
return filename.equals(((PDOMSearchMatch)match).getFileName());
|
||||
} catch (CoreException e) {
|
||||
CUIPlugin.getDefault().log(e);
|
||||
}
|
||||
IPath filename = new Path(getFileName(editor));
|
||||
if (filename != null && match instanceof PDOMSearchMatch)
|
||||
return filename.equals(IndexLocationFactory.getAbsolutePath(((PDOMSearchMatch)match).getLocation()));
|
||||
return false;
|
||||
}
|
||||
|
||||
private Match[] computeContainedMatches(AbstractTextSearchResult result, String filename) throws CoreException {
|
||||
IPath pfilename= new Path(filename);
|
||||
List list = new ArrayList();
|
||||
Object[] elements = result.getElements();
|
||||
for (int i = 0; i < elements.length; ++i) {
|
||||
if (((PDOMSearchElement) elements[i]).getFileName()
|
||||
.equals(filename)) {
|
||||
if (pfilename.equals(IndexLocationFactory.getAbsolutePath(((PDOMSearchElement)elements[i]).getLocation()))) {
|
||||
Match[] matches = result.getMatches(elements[i]);
|
||||
for (int j = 0; j < matches.length; ++j) {
|
||||
if (matches[j] instanceof PDOMSearchMatch) {
|
||||
|
|
|
@ -18,10 +18,6 @@ import java.util.Iterator;
|
|||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import org.eclipse.cdt.core.model.CoreModel;
|
||||
import org.eclipse.cdt.core.model.ICElement;
|
||||
import org.eclipse.cdt.core.model.ICProject;
|
||||
import org.eclipse.cdt.core.model.ISourceRoot;
|
||||
import org.eclipse.core.resources.IFile;
|
||||
import org.eclipse.core.resources.IProject;
|
||||
import org.eclipse.core.resources.ResourcesPlugin;
|
||||
|
@ -33,6 +29,13 @@ import org.eclipse.jface.viewers.Viewer;
|
|||
import org.eclipse.swt.widgets.Display;
|
||||
import org.eclipse.ui.PlatformUI;
|
||||
|
||||
import org.eclipse.cdt.core.index.IIndexFileLocation;
|
||||
import org.eclipse.cdt.core.index.IndexLocationFactory;
|
||||
import org.eclipse.cdt.core.model.CoreModel;
|
||||
import org.eclipse.cdt.core.model.ICElement;
|
||||
import org.eclipse.cdt.core.model.ICProject;
|
||||
import org.eclipse.cdt.core.model.ISourceRoot;
|
||||
|
||||
/**
|
||||
* @author Doug Schaefer
|
||||
*
|
||||
|
@ -94,8 +97,14 @@ public class PDOMSearchTreeContentProvider implements ITreeContentProvider, IPDO
|
|||
}
|
||||
|
||||
private void insertSearchElement(PDOMSearchElement element) {
|
||||
IPath path = new Path(element.getFileName());
|
||||
IFile[] files = ResourcesPlugin.getWorkspace().getRoot().findFilesForLocation(path);
|
||||
IIndexFileLocation location= element.getLocation();
|
||||
IFile[] files;
|
||||
if(location.getFullPath()!=null) {
|
||||
files= new IFile[] {ResourcesPlugin.getWorkspace().getRoot().getFile(new Path(location.getFullPath()))};
|
||||
} else {
|
||||
IPath path= IndexLocationFactory.getAbsolutePath(element.getLocation());
|
||||
files= ResourcesPlugin.getWorkspace().getRoot().findFilesForLocation(path);
|
||||
}
|
||||
boolean handled= false;
|
||||
if (files.length > 0) {
|
||||
for (int j = 0; j < files.length; ++j) {
|
||||
|
@ -108,9 +117,8 @@ public class PDOMSearchTreeContentProvider implements ITreeContentProvider, IPDO
|
|||
}
|
||||
}
|
||||
if (!handled) {
|
||||
String pathName = path.toOSString();
|
||||
insertChild(pathName, element);
|
||||
insertChild(result, pathName);
|
||||
insertChild(element.getLocation(), element);
|
||||
insertChild(result, element.getLocation());
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -13,7 +13,7 @@ package org.eclipse.cdt.internal.ui.search;
|
|||
|
||||
import org.eclipse.core.runtime.CoreException;
|
||||
import org.eclipse.core.runtime.IPath;
|
||||
import org.eclipse.core.runtime.Path;
|
||||
import org.eclipse.jface.viewers.StructuredViewer;
|
||||
import org.eclipse.jface.viewers.TableViewer;
|
||||
import org.eclipse.jface.viewers.TreeViewer;
|
||||
import org.eclipse.search.ui.text.AbstractTextSearchViewPage;
|
||||
|
@ -22,6 +22,8 @@ import org.eclipse.ui.IEditorPart;
|
|||
import org.eclipse.ui.PartInitException;
|
||||
import org.eclipse.ui.texteditor.ITextEditor;
|
||||
|
||||
import org.eclipse.cdt.core.index.IIndexFileLocation;
|
||||
import org.eclipse.cdt.core.index.IndexLocationFactory;
|
||||
import org.eclipse.cdt.ui.CUIPlugin;
|
||||
|
||||
import org.eclipse.cdt.internal.ui.util.EditorUtility;
|
||||
|
@ -69,7 +71,9 @@ public class PDOMSearchViewPage extends AbstractTextSearchViewPage {
|
|||
return;
|
||||
|
||||
try {
|
||||
IPath path = new Path(((PDOMSearchMatch)match).getFileName());
|
||||
Object element= ((PDOMSearchMatch)match).getElement();
|
||||
IIndexFileLocation ifl= ((PDOMSearchElement)element).getLocation();
|
||||
IPath path = IndexLocationFactory.getPath(ifl);
|
||||
IEditorPart editor = EditorUtility.openInEditor(path, null);
|
||||
if (editor instanceof ITextEditor) {
|
||||
ITextEditor textEditor = (ITextEditor)editor;
|
||||
|
@ -80,4 +84,7 @@ public class PDOMSearchViewPage extends AbstractTextSearchViewPage {
|
|||
}
|
||||
}
|
||||
|
||||
public StructuredViewer getViewer() {
|
||||
return super.getViewer();
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue