mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-07-31 12:55:40 +02:00
Patch for Devin Steffler.
Fixed Bug 103323 NPE caused by DOMSearchUtil#getSearchPattern() when IASTName is null
This commit is contained in:
parent
090b00143d
commit
0caa2a5bd5
4 changed files with 110 additions and 48 deletions
|
@ -64,7 +64,8 @@ import org.eclipse.core.runtime.content.IContentType;
|
||||||
* @author dsteffle
|
* @author dsteffle
|
||||||
*/
|
*/
|
||||||
public class DOMSearchUtil {
|
public class DOMSearchUtil {
|
||||||
private static final IASTName[] BLANK_NAME_ARRAY = new IASTName[0];
|
private static final String BLANK_STRING = ""; //$NON-NLS-1$
|
||||||
|
private static final IASTName[] BLANK_NAME_ARRAY = new IASTName[0];
|
||||||
private static final IASTName[] EMPTY_NAME_LIST = BLANK_NAME_ARRAY;
|
private static final IASTName[] EMPTY_NAME_LIST = BLANK_NAME_ARRAY;
|
||||||
private static final Set EMPTY_MATCHES = new HashSet(0);
|
private static final Set EMPTY_MATCHES = new HashSet(0);
|
||||||
|
|
||||||
|
@ -468,6 +469,9 @@ public class DOMSearchUtil {
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
public static String getSearchPattern(IASTName name) {
|
public static String getSearchPattern(IASTName name) {
|
||||||
|
if (name == null)
|
||||||
|
return BLANK_STRING;
|
||||||
|
|
||||||
StringBuffer buffer = new StringBuffer();
|
StringBuffer buffer = new StringBuffer();
|
||||||
// first option - use binding to get qualified name
|
// first option - use binding to get qualified name
|
||||||
IBinding binding = name.resolveBinding();
|
IBinding binding = name.resolveBinding();
|
||||||
|
|
|
@ -23,6 +23,7 @@ import org.eclipse.cdt.core.index.IndexChangeEvent;
|
||||||
import org.eclipse.cdt.core.search.DOMSearchUtil;
|
import org.eclipse.cdt.core.search.DOMSearchUtil;
|
||||||
import org.eclipse.cdt.core.testplugin.FileManager;
|
import org.eclipse.cdt.core.testplugin.FileManager;
|
||||||
import org.eclipse.cdt.internal.core.parser.ParserException;
|
import org.eclipse.cdt.internal.core.parser.ParserException;
|
||||||
|
import org.eclipse.cdt.internal.ui.editor.ICEditorActionDefinitionIds;
|
||||||
import org.eclipse.core.resources.IFile;
|
import org.eclipse.core.resources.IFile;
|
||||||
import org.eclipse.core.resources.IFolder;
|
import org.eclipse.core.resources.IFolder;
|
||||||
import org.eclipse.core.resources.IProject;
|
import org.eclipse.core.resources.IProject;
|
||||||
|
@ -32,9 +33,14 @@ import org.eclipse.core.runtime.IStatus;
|
||||||
import org.eclipse.core.runtime.NullProgressMonitor;
|
import org.eclipse.core.runtime.NullProgressMonitor;
|
||||||
import org.eclipse.jface.action.IAction;
|
import org.eclipse.jface.action.IAction;
|
||||||
import org.eclipse.jface.text.TextSelection;
|
import org.eclipse.jface.text.TextSelection;
|
||||||
|
import org.eclipse.jface.util.IPropertyChangeListener;
|
||||||
|
import org.eclipse.jface.util.PropertyChangeEvent;
|
||||||
import org.eclipse.jface.viewers.ISelection;
|
import org.eclipse.jface.viewers.ISelection;
|
||||||
|
import org.eclipse.search2.internal.ui.SearchView;
|
||||||
import org.eclipse.ui.IEditorInput;
|
import org.eclipse.ui.IEditorInput;
|
||||||
import org.eclipse.ui.IEditorPart;
|
import org.eclipse.ui.IEditorPart;
|
||||||
|
import org.eclipse.ui.IViewPart;
|
||||||
|
import org.eclipse.ui.IViewReference;
|
||||||
import org.eclipse.ui.IWorkbenchPage;
|
import org.eclipse.ui.IWorkbenchPage;
|
||||||
import org.eclipse.ui.PartInitException;
|
import org.eclipse.ui.PartInitException;
|
||||||
import org.eclipse.ui.PlatformUI;
|
import org.eclipse.ui.PlatformUI;
|
||||||
|
@ -292,6 +298,34 @@ public class BaseSelectionTestsIndexer extends TestCase {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected void testSimple_Ctrl_G_Selection(IFile file, int offset, int length, int numOccurrences) throws ParserException {
|
||||||
|
if (offset < 0)
|
||||||
|
throw new ParserException("offset can not be less than 0 and was " + offset); //$NON-NLS-1$
|
||||||
|
|
||||||
|
IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
|
||||||
|
IEditorPart part = null;
|
||||||
|
try {
|
||||||
|
part = page.openEditor(new FileEditorInput(file), "org.eclipse.cdt.ui.editor.CEditor"); //$NON-NLS-1$
|
||||||
|
} catch (PartInitException e) {
|
||||||
|
assertFalse(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (part instanceof AbstractTextEditor) {
|
||||||
|
((AbstractTextEditor)part).getSelectionProvider().setSelection(new TextSelection(offset,length));
|
||||||
|
|
||||||
|
final IAction action = ((AbstractTextEditor)part).getAction(ICEditorActionDefinitionIds.FIND_DECL);
|
||||||
|
|
||||||
|
action.run();
|
||||||
|
|
||||||
|
// update the file/part to point to the newly opened IFile/IEditorPart
|
||||||
|
// IViewPart view = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().findView("org.eclipse.search.ui.views.SearchView");
|
||||||
|
|
||||||
|
// String title = view.getTitle();
|
||||||
|
|
||||||
|
// assertTrue( title.indexOf(numOccurrences + " Occurrences") >= 0 ); //$NON-NLS-1$
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public void resetIndexer(final String indexerId){
|
public void resetIndexer(final String indexerId){
|
||||||
if ( project != null) {
|
if ( project != null) {
|
||||||
ICDescriptorOperation op = new ICDescriptorOperation() {
|
ICDescriptorOperation op = new ICDescriptorOperation() {
|
||||||
|
|
|
@ -126,6 +126,7 @@ public class CPPSelectionTestsDOMIndexer extends BaseSelectionTestsIndexer imple
|
||||||
suite.addTest(new CPPSelectionTestsDOMIndexer("testBug95229")); //$NON-NLS-1$
|
suite.addTest(new CPPSelectionTestsDOMIndexer("testBug95229")); //$NON-NLS-1$
|
||||||
suite.addTest(new CPPSelectionTestsDOMIndexer("testBug101287")); //$NON-NLS-1$
|
suite.addTest(new CPPSelectionTestsDOMIndexer("testBug101287")); //$NON-NLS-1$
|
||||||
suite.addTest(new CPPSelectionTestsDOMIndexer("testBug102258")); //$NON-NLS-1$
|
suite.addTest(new CPPSelectionTestsDOMIndexer("testBug102258")); //$NON-NLS-1$
|
||||||
|
suite.addTest(new CPPSelectionTestsDOMIndexer("testBug103323")); //$NON-NLS-1$
|
||||||
|
|
||||||
return suite;
|
return suite;
|
||||||
}
|
}
|
||||||
|
@ -1046,6 +1047,27 @@ public class CPPSelectionTestsDOMIndexer extends BaseSelectionTestsIndexer imple
|
||||||
assertEquals(((ASTNode)decl).getOffset(), 7);
|
assertEquals(((ASTNode)decl).getOffset(), 7);
|
||||||
assertEquals(((ASTNode)decl).getLength(), 12);
|
assertEquals(((ASTNode)decl).getLength(), 12);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void testBug103323() throws Exception {
|
||||||
|
StringBuffer buffer = new StringBuffer();
|
||||||
|
buffer.append("namespace foo {\n"); //$NON-NLS-1$
|
||||||
|
buffer.append("int g() {\n"); //$NON-NLS-1$
|
||||||
|
buffer.append("return 0;\n"); //$NON-NLS-1$
|
||||||
|
buffer.append("}\n"); //$NON-NLS-1$
|
||||||
|
buffer.append("}\n"); //$NON-NLS-1$
|
||||||
|
buffer.append("int f() {\n"); //$NON-NLS-1$
|
||||||
|
buffer.append("return foo::g();\n"); //$NON-NLS-1$
|
||||||
|
buffer.append("}\n"); //$NON-NLS-1$
|
||||||
|
|
||||||
|
String code = buffer.toString();
|
||||||
|
IFile file = importFile("testBug103323.cpp", code); //$NON-NLS-1$
|
||||||
|
|
||||||
|
int offset = code.indexOf("g();\n"); //$NON-NLS-1$
|
||||||
|
int length = "g()".length(); //$NON-NLS-1$
|
||||||
|
|
||||||
|
testSimple_Ctrl_G_Selection(file, offset, length, 1);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
// REMINDER: see CPPSelectionTestsDomIndexer#suite() when appending new tests to this suite
|
// REMINDER: see CPPSelectionTestsDomIndexer#suite() when appending new tests to this suite
|
||||||
}
|
}
|
||||||
|
|
|
@ -94,54 +94,56 @@ public class DOMQuery extends CSearchQuery implements ISearchQuery {
|
||||||
// fix for 43128
|
// fix for 43128
|
||||||
Set matches=null;
|
Set matches=null;
|
||||||
IASTName[] foundNames=null;
|
IASTName[] foundNames=null;
|
||||||
if (!isLocal())
|
if (searchName != null) {
|
||||||
matches = DOMSearchUtil.getMatchesFromSearchEngine(scope, searchName, limitTo);
|
if (!isLocal())
|
||||||
|
matches = DOMSearchUtil.getMatchesFromSearchEngine(scope, searchName, limitTo);
|
||||||
|
|
||||||
|
if (matches != null && matches.size() > 0) {
|
||||||
|
Iterator itr = matches.iterator();
|
||||||
|
while(itr.hasNext()) {
|
||||||
|
Object next = itr.next();
|
||||||
|
if (next instanceof IMatch) {
|
||||||
|
try {
|
||||||
|
collector.acceptMatch((IMatch)next);
|
||||||
|
} catch (CoreException e) {
|
||||||
|
// don't do anything if the match wasn't accepted
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else { // only search against the DOM if the index failed to get results... i.e. don't want duplicates
|
||||||
|
foundNames = DOMSearchUtil.getNamesFromDOM(searchName, limitTo);
|
||||||
|
|
||||||
|
for (int i=0; i<foundNames.length; i++) {
|
||||||
|
try {
|
||||||
|
String fileName = null;
|
||||||
|
IPath path = null;
|
||||||
|
int start = 0;
|
||||||
|
int end = 0;
|
||||||
|
|
||||||
|
if ( foundNames[i].getTranslationUnit() != null ) {
|
||||||
|
IASTFileLocation location = foundNames[i].getFileLocation();
|
||||||
|
fileName = location.getFileName();
|
||||||
|
start = location.getNodeOffset();
|
||||||
|
end = location.getNodeOffset() + location.getNodeLength();
|
||||||
|
}
|
||||||
|
|
||||||
|
path = new Path(fileName);
|
||||||
|
Object fileResource=null;
|
||||||
|
IResource res = ParserUtil.getResourceForFilename(fileName);
|
||||||
|
if (res != null)
|
||||||
|
fileResource = res;
|
||||||
|
else {
|
||||||
|
fileResource = PathUtil.getWorkspaceRelativePath(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
|
collector.acceptMatch( createMatch(fileResource, start, end, foundNames[i], path ) );
|
||||||
|
|
||||||
|
} catch (CoreException ce) {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (matches != null && matches.size() > 0) {
|
if ((searchPattern != null || matches == null || matches.size() == 0) && (foundNames == null || foundNames.length == 0)) {
|
||||||
Iterator itr = matches.iterator();
|
|
||||||
while(itr.hasNext()) {
|
|
||||||
Object next = itr.next();
|
|
||||||
if (next instanceof IMatch) {
|
|
||||||
try {
|
|
||||||
collector.acceptMatch((IMatch)next);
|
|
||||||
} catch (CoreException e) {
|
|
||||||
// don't do anything if the match wasn't accepted
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else { // only search against the DOM if the index failed to get results... i.e. don't want duplicates
|
|
||||||
foundNames = DOMSearchUtil.getNamesFromDOM(searchName, limitTo);
|
|
||||||
|
|
||||||
for (int i=0; i<foundNames.length; i++) {
|
|
||||||
try {
|
|
||||||
String fileName = null;
|
|
||||||
IPath path = null;
|
|
||||||
int start = 0;
|
|
||||||
int end = 0;
|
|
||||||
|
|
||||||
if ( foundNames[i].getTranslationUnit() != null ) {
|
|
||||||
IASTFileLocation location = foundNames[i].getFileLocation();
|
|
||||||
fileName = location.getFileName();
|
|
||||||
start = location.getNodeOffset();
|
|
||||||
end = location.getNodeOffset() + location.getNodeLength();
|
|
||||||
}
|
|
||||||
|
|
||||||
path = new Path(fileName);
|
|
||||||
Object fileResource=null;
|
|
||||||
IResource res = ParserUtil.getResourceForFilename(fileName);
|
|
||||||
if (res != null)
|
|
||||||
fileResource = res;
|
|
||||||
else {
|
|
||||||
fileResource = PathUtil.getWorkspaceRelativePath(fileName);
|
|
||||||
}
|
|
||||||
|
|
||||||
collector.acceptMatch( createMatch(fileResource, start, end, foundNames[i], path ) );
|
|
||||||
|
|
||||||
} catch (CoreException ce) {}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (searchPattern != null && matches.size() == 0 && (foundNames == null || foundNames.length == 0)) {
|
|
||||||
// last try: search the index for the selected string, even if no name was found for that selection
|
// last try: search the index for the selected string, even if no name was found for that selection
|
||||||
matches = DOMSearchUtil.getMatchesFromSearchEngine( scope, searchPattern, limitTo );
|
matches = DOMSearchUtil.getMatchesFromSearchEngine( scope, searchPattern, limitTo );
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue