mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-09-10 12:03:16 +02:00
Fix NPE opening type-hierarchy from call-hierarchy
This commit is contained in:
parent
509a6ce37e
commit
9051537052
2 changed files with 51 additions and 4 deletions
|
@ -37,6 +37,7 @@ 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;
|
||||||
import org.eclipse.jface.text.IRegion;
|
import org.eclipse.jface.text.IRegion;
|
||||||
|
import org.eclipse.jface.text.Region;
|
||||||
|
|
||||||
abstract class CElementHandle implements ICElementHandle, ISourceReference {
|
abstract class CElementHandle implements ICElementHandle, ISourceReference {
|
||||||
protected static final String[] EMPTY_STRING_ARRAY = new String[0];
|
protected static final String[] EMPTY_STRING_ARRAY = new String[0];
|
||||||
|
@ -60,6 +61,7 @@ abstract class CElementHandle implements ICElementHandle, ISourceReference {
|
||||||
else {
|
else {
|
||||||
fName= name;
|
fName= name;
|
||||||
}
|
}
|
||||||
|
fRegion= new Region(0,0);
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean equals(Object obj) {
|
public boolean equals(Object obj) {
|
||||||
|
|
|
@ -17,6 +17,7 @@ import java.util.ArrayList;
|
||||||
import org.eclipse.core.resources.IFile;
|
import org.eclipse.core.resources.IFile;
|
||||||
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.NullProgressMonitor;
|
||||||
import org.eclipse.core.runtime.Path;
|
import org.eclipse.core.runtime.Path;
|
||||||
import org.eclipse.jface.text.IRegion;
|
import org.eclipse.jface.text.IRegion;
|
||||||
import org.eclipse.jface.text.ITextSelection;
|
import org.eclipse.jface.text.ITextSelection;
|
||||||
|
@ -31,11 +32,17 @@ import org.eclipse.cdt.core.dom.ast.IASTFileLocation;
|
||||||
import org.eclipse.cdt.core.dom.ast.IASTName;
|
import org.eclipse.cdt.core.dom.ast.IASTName;
|
||||||
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
|
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
|
||||||
import org.eclipse.cdt.core.dom.ast.IBinding;
|
import org.eclipse.cdt.core.dom.ast.IBinding;
|
||||||
|
import org.eclipse.cdt.core.dom.ast.ICompositeType;
|
||||||
|
import org.eclipse.cdt.core.dom.ast.IEnumeration;
|
||||||
|
import org.eclipse.cdt.core.dom.ast.ITypedef;
|
||||||
|
import org.eclipse.cdt.core.dom.ast.cpp.ICPPClassType;
|
||||||
|
import org.eclipse.cdt.core.dom.ast.cpp.ICPPNamespace;
|
||||||
import org.eclipse.cdt.core.index.IIndex;
|
import org.eclipse.cdt.core.index.IIndex;
|
||||||
import org.eclipse.cdt.core.index.IIndexBinding;
|
import org.eclipse.cdt.core.index.IIndexBinding;
|
||||||
import org.eclipse.cdt.core.index.IIndexFile;
|
import org.eclipse.cdt.core.index.IIndexFile;
|
||||||
import org.eclipse.cdt.core.index.IIndexFileLocation;
|
import org.eclipse.cdt.core.index.IIndexFileLocation;
|
||||||
import org.eclipse.cdt.core.index.IIndexName;
|
import org.eclipse.cdt.core.index.IIndexName;
|
||||||
|
import org.eclipse.cdt.core.index.IndexFilter;
|
||||||
import org.eclipse.cdt.core.index.IndexLocationFactory;
|
import org.eclipse.cdt.core.index.IndexLocationFactory;
|
||||||
import org.eclipse.cdt.core.model.CModelException;
|
import org.eclipse.cdt.core.model.CModelException;
|
||||||
import org.eclipse.cdt.core.model.ICElement;
|
import org.eclipse.cdt.core.model.ICElement;
|
||||||
|
@ -53,14 +60,52 @@ import org.eclipse.cdt.internal.corext.util.CModelUtil;
|
||||||
public class IndexUI {
|
public class IndexUI {
|
||||||
private static final ICElementHandle[] EMPTY_ELEMENTS = new ICElementHandle[0];
|
private static final ICElementHandle[] EMPTY_ELEMENTS = new ICElementHandle[0];
|
||||||
|
|
||||||
public static IIndexBinding elementToBinding(IIndex index, ICElement element) throws CoreException {
|
public static IIndexBinding elementToBinding(IIndex index, ICElement element) throws CoreException, DOMException {
|
||||||
IIndexName name= elementToName(index, element);
|
if (element instanceof ISourceReference) {
|
||||||
if (name != null) {
|
ISourceReference sf = ((ISourceReference)element);
|
||||||
return index.findBinding(name);
|
ISourceRange range= sf.getSourceRange();
|
||||||
|
if (range.getIdLength() != 0) {
|
||||||
|
IIndexName name= elementToName(index, element);
|
||||||
|
if (name != null) {
|
||||||
|
return index.findBinding(name);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
String name= element.getElementName();
|
||||||
|
name= name.substring(name.lastIndexOf(':')+1);
|
||||||
|
IIndexBinding[] bindings= index.findBindings(name.toCharArray(), IndexFilter.ALL, new NullProgressMonitor());
|
||||||
|
for (int i = 0; i < bindings.length; i++) {
|
||||||
|
IIndexBinding binding = bindings[i];
|
||||||
|
if (checkBinding(binding, element)) {
|
||||||
|
return binding;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static boolean checkBinding(IIndexBinding binding, ICElement element) throws DOMException {
|
||||||
|
switch(element.getElementType()) {
|
||||||
|
case ICElement.C_ENUMERATION:
|
||||||
|
return binding instanceof IEnumeration;
|
||||||
|
case ICElement.C_NAMESPACE:
|
||||||
|
return binding instanceof ICPPNamespace;
|
||||||
|
case ICElement.C_STRUCT:
|
||||||
|
return binding instanceof ICompositeType &&
|
||||||
|
((ICompositeType) binding).getKey() == ICompositeType.k_struct;
|
||||||
|
case ICElement.C_CLASS:
|
||||||
|
return binding instanceof ICPPClassType &&
|
||||||
|
((ICompositeType) binding).getKey() == ICPPClassType.k_class;
|
||||||
|
case ICElement.C_UNION:
|
||||||
|
return binding instanceof ICompositeType &&
|
||||||
|
((ICompositeType) binding).getKey() == ICompositeType.k_union;
|
||||||
|
case ICElement.C_TYPEDEF:
|
||||||
|
return binding instanceof ITypedef;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
public static IIndexName elementToName(IIndex index, ICElement element) throws CoreException {
|
public static IIndexName elementToName(IIndex index, ICElement element) throws CoreException {
|
||||||
if (element instanceof ISourceReference) {
|
if (element instanceof ISourceReference) {
|
||||||
ISourceReference sf = ((ISourceReference)element);
|
ISourceReference sf = ((ISourceReference)element);
|
||||||
|
|
Loading…
Add table
Reference in a new issue