use of org.eclipse.jdt.core.ISourceReference in project che by eclipse.
the class JavaNavigation method compilationUnitNavigation.
private OpenDeclarationDescriptor compilationUnitNavigation(ICompilationUnit unit, IJavaElement element) throws JavaModelException {
OpenDeclarationDescriptor dto = DtoFactory.getInstance().createDto(OpenDeclarationDescriptor.class);
String absolutePath = unit.getPath().toOSString();
dto.setPath(absolutePath);
dto.setBinary(false);
if (element instanceof ISourceReference) {
ISourceRange nameRange = ((ISourceReference) element).getNameRange();
dto.setOffset(nameRange.getOffset());
dto.setLength(nameRange.getLength());
}
return dto;
}
use of org.eclipse.jdt.core.ISourceReference in project che by eclipse.
the class JavaNavigation method classFileNavigation.
private OpenDeclarationDescriptor classFileNavigation(IClassFile classFile, IJavaElement element) throws JavaModelException {
OpenDeclarationDescriptor dto = DtoFactory.getInstance().createDto(OpenDeclarationDescriptor.class);
dto.setPath(classFile.getType().getFullyQualifiedName());
dto.setLibId(classFile.getAncestor(IPackageFragmentRoot.PACKAGE_FRAGMENT_ROOT).hashCode());
dto.setBinary(true);
if (classFile.getSourceRange() != null) {
if (element instanceof ISourceReference) {
ISourceRange nameRange = ((ISourceReference) element).getNameRange();
dto.setOffset(nameRange.getOffset());
dto.setLength(nameRange.getLength());
}
}
return dto;
}
use of org.eclipse.jdt.core.ISourceReference in project che by eclipse.
the class SelectionConverter method resolveEnclosingElement.
// public static IJavaElement getElementAtOffset(ITypeRoot input, ITextSelection selection) throws JavaModelException {
// if (input instanceof ICompilationUnit) {
// JavaModelUtil.reconcile((ICompilationUnit)input);
// }
// IJavaElement ref = input.getElementAt(selection.getOffset());
// if (ref == null)
// return input;
// return ref;
// }
//
// public static IJavaElement resolveEnclosingElement(JavaEditor editor, ITextSelection selection) throws JavaModelException {
// ITypeRoot input = getInput(editor);
// if (input != null)
// return resolveEnclosingElement(input, selection);
// return null;
// }
//
public static IJavaElement resolveEnclosingElement(IJavaElement input, ITextSelection selection) throws JavaModelException {
IJavaElement atOffset = null;
if (input instanceof ICompilationUnit) {
ICompilationUnit cunit = (ICompilationUnit) input;
JavaModelUtil.reconcile(cunit);
atOffset = cunit.getElementAt(selection.getOffset());
} else if (input instanceof IClassFile) {
IClassFile cfile = (IClassFile) input;
atOffset = cfile.getElementAt(selection.getOffset());
} else {
return null;
}
if (atOffset == null) {
return input;
} else {
int selectionEnd = selection.getOffset() + selection.getLength();
IJavaElement result = atOffset;
if (atOffset instanceof ISourceReference) {
ISourceRange range = ((ISourceReference) atOffset).getSourceRange();
while (range.getOffset() + range.getLength() < selectionEnd) {
result = result.getParent();
if (!(result instanceof ISourceReference)) {
result = input;
break;
}
range = ((ISourceReference) result).getSourceRange();
}
}
return result;
}
}
use of org.eclipse.jdt.core.ISourceReference in project che by eclipse.
the class RenameAnalyzeUtil method getOldSourceRange.
private static ISourceRange getOldSourceRange(SearchMatch newMatch) {
// cannot transfom offset in preview to offset in original -> just show enclosing method
IJavaElement newMatchElement = (IJavaElement) newMatch.getElement();
IJavaElement primaryElement = newMatchElement.getPrimaryElement();
ISourceRange range = null;
if (primaryElement.exists() && primaryElement instanceof ISourceReference) {
try {
range = ((ISourceReference) primaryElement).getSourceRange();
} catch (JavaModelException e) {
// can live without source range
}
}
return range;
}
use of org.eclipse.jdt.core.ISourceReference in project che by eclipse.
the class CompilationUnitChangeNode method getModifiedJavaElement.
private IJavaElement getModifiedJavaElement(TextEditBasedChangeGroup edit, ICompilationUnit cunit) throws JavaModelException {
IRegion range = edit.getRegion();
if (range.getOffset() == 0 && range.getLength() == 0)
return cunit;
IJavaElement result = cunit.getElementAt(range.getOffset());
if (result == null)
return cunit;
try {
while (true) {
ISourceReference ref = (ISourceReference) result;
IRegion sRange = new Region(ref.getSourceRange().getOffset(), ref.getSourceRange().getLength());
if (result.getElementType() == IJavaElement.COMPILATION_UNIT || result.getParent() == null || coveredBy(edit, sRange))
break;
result = result.getParent();
}
} catch (JavaModelException e) {
// Do nothing, use old value.
} catch (ClassCastException e) {
// Do nothing, use old value.
}
return result;
}
Aggregations