use of org.eclipse.jdt.core.ISourceRange in project eclipse.jdt.ls by eclipse.
the class AssociativeInfixExpressionFragment method getRangeOfOperands.
private static ISourceRange getRangeOfOperands(List<Expression> operands) {
Expression first = operands.get(0);
Expression last = operands.get(operands.size() - 1);
return new SourceRange(first.getStartPosition(), last.getStartPosition() + last.getLength() - first.getStartPosition());
}
use of org.eclipse.jdt.core.ISourceRange in project xtext-eclipse by eclipse.
the class JvmOutlineNodeElementOpener method openEObject.
@Override
protected void openEObject(EObject state) {
try {
if (state instanceof JvmIdentifiableElement && state.eResource() instanceof TypeResource) {
IJavaElement javaElement = javaElementFinder.findElementFor((JvmIdentifiableElement) state);
if (javaElement instanceof IMember) {
IResource resource = javaElement.getResource();
if (resource instanceof IStorage) {
ITrace traceToSource = traceInformation.getTraceToSource((IStorage) resource);
if (traceToSource != null) {
ISourceRange sourceRange = ((IMember) javaElement).getSourceRange();
ILocationInResource sourceInformation = traceToSource.getBestAssociatedLocation(new TextRegion(sourceRange.getOffset(), sourceRange.getLength()));
if (sourceInformation != null) {
globalURIEditorOpener.open(sourceInformation.getAbsoluteResourceURI().getURI(), javaElement, true);
return;
}
}
}
globalURIEditorOpener.open(null, javaElement, true);
return;
}
}
} catch (Exception exc) {
LOG.error("Error opening java editor", exc);
}
super.openEObject(state);
}
use of org.eclipse.jdt.core.ISourceRange in project xtext-eclipse by eclipse.
the class JvmImplementationOpener method openImplementations.
/**
* Main parts of the logic is taken from {@link org.eclipse.jdt.internal.ui.javaeditor.JavaElementImplementationHyperlink}
*
* @param element - Element to show implementations for
* @param textviewer - Viewer to show hierarchy view on
* @param region - Region where to show hierarchy view
*/
public void openImplementations(final IJavaElement element, ITextViewer textviewer, IRegion region) {
if (element instanceof IMethod) {
ITypeRoot typeRoot = ((IMethod) element).getTypeRoot();
CompilationUnit ast = SharedASTProvider.getAST(typeRoot, SharedASTProvider.WAIT_YES, null);
if (ast == null) {
openQuickHierarchy(textviewer, element, region);
return;
}
try {
ISourceRange nameRange = ((IMethod) element).getNameRange();
ASTNode node = NodeFinder.perform(ast, nameRange);
ITypeBinding parentTypeBinding = null;
if (node instanceof SimpleName) {
ASTNode parent = node.getParent();
if (parent instanceof MethodInvocation) {
Expression expression = ((MethodInvocation) parent).getExpression();
if (expression == null) {
parentTypeBinding = Bindings.getBindingOfParentType(node);
} else {
parentTypeBinding = expression.resolveTypeBinding();
}
} else if (parent instanceof SuperMethodInvocation) {
// Directly go to the super method definition
openEditor(element);
return;
} else if (parent instanceof MethodDeclaration) {
parentTypeBinding = Bindings.getBindingOfParentType(node);
}
}
final IType type = parentTypeBinding != null ? (IType) parentTypeBinding.getJavaElement() : null;
if (type == null) {
openQuickHierarchy(textviewer, element, region);
return;
}
final String earlyExitIndicator = "EarlyExitIndicator";
final ArrayList<IJavaElement> links = Lists.newArrayList();
IRunnableWithProgress runnable = new IRunnableWithProgress() {
@Override
public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
if (monitor == null) {
monitor = new NullProgressMonitor();
}
try {
String methodLabel = JavaElementLabels.getElementLabel(element, JavaElementLabels.DEFAULT_QUALIFIED);
monitor.beginTask(Messages.format("Searching for implementors of ''{0}''", methodLabel), 100);
SearchRequestor requestor = new SearchRequestor() {
@Override
public void acceptSearchMatch(SearchMatch match) throws CoreException {
if (match.getAccuracy() == SearchMatch.A_ACCURATE) {
IJavaElement element = (IJavaElement) match.getElement();
if (element instanceof IMethod && !JdtFlags.isAbstract((IMethod) element)) {
links.add(element);
if (links.size() > 1) {
throw new OperationCanceledException(earlyExitIndicator);
}
}
}
}
};
int limitTo = IJavaSearchConstants.DECLARATIONS | IJavaSearchConstants.IGNORE_DECLARING_TYPE | IJavaSearchConstants.IGNORE_RETURN_TYPE;
SearchPattern pattern = SearchPattern.createPattern(element, limitTo);
Assert.isNotNull(pattern);
SearchParticipant[] participants = new SearchParticipant[] { SearchEngine.getDefaultSearchParticipant() };
SearchEngine engine = new SearchEngine();
engine.search(pattern, participants, SearchEngine.createHierarchyScope(type), requestor, SubMonitor.convert(monitor, 100));
if (monitor.isCanceled()) {
throw new InterruptedException();
}
} catch (OperationCanceledException e) {
throw new InterruptedException(e.getMessage());
} catch (CoreException e) {
throw new InvocationTargetException(e);
} finally {
monitor.done();
}
}
};
try {
PlatformUI.getWorkbench().getProgressService().busyCursorWhile(runnable);
} catch (InvocationTargetException e) {
IStatus status = new Status(IStatus.ERROR, JavaPlugin.getPluginId(), IStatus.OK, Messages.format("An error occurred while searching for implementations of method ''{0}''. See error log for details.", element.getElementName()), e.getCause());
JavaPlugin.log(status);
ErrorDialog.openError(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), "Open Implementation", "Problems finding implementations.", status);
} catch (InterruptedException e) {
if (e.getMessage() != earlyExitIndicator) {
return;
}
}
if (links.size() == 1) {
openEditor(links.get(0));
} else {
openQuickHierarchy(textviewer, element, region);
}
} catch (JavaModelException e) {
log.error("An error occurred while searching for implementations", e.getCause());
} catch (PartInitException e) {
log.error("An error occurred while searching for implementations", e.getCause());
}
}
}
use of org.eclipse.jdt.core.ISourceRange in project bndtools by bndtools.
the class BaselineErrorHandler method generatePackageInfoMarkers.
List<MarkerData> generatePackageInfoMarkers(Info baselineInfo, IJavaProject javaProject, String message) throws JavaModelException {
List<MarkerData> markers = new LinkedList<>();
for (IClasspathEntry entry : javaProject.getRawClasspath()) {
if (IClasspathEntry.CPE_SOURCE == entry.getEntryKind()) {
IPath entryPath = entry.getPath();
IPath pkgPath = entryPath.append(baselineInfo.packageName.replace('.', '/'));
// Find in packageinfo file
IPath pkgInfoPath = pkgPath.append(PACKAGEINFO);
IFile pkgInfoFile = javaProject.getProject().getWorkspace().getRoot().getFile(pkgInfoPath);
if (pkgInfoFile != null && pkgInfoFile.exists()) {
Map<String, Object> attribs = new HashMap<String, Object>();
attribs.put(IMarker.MESSAGE, message.trim());
attribs.put(PROP_SUGGESTED_VERSION, baselineInfo.suggestedVersion.toString());
LineLocation lineLoc = findVersionLocation(pkgInfoFile.getLocation().toFile());
if (lineLoc != null) {
attribs.put(IMarker.LINE_NUMBER, lineLoc.lineNum);
attribs.put(IMarker.CHAR_START, lineLoc.start);
attribs.put(IMarker.CHAR_END, lineLoc.end);
}
markers.add(new MarkerData(pkgInfoFile, attribs, true));
}
// Find in package-info.java
IPackageFragment pkg = javaProject.findPackageFragment(pkgPath);
if (pkg != null) {
ICompilationUnit pkgInfoJava = pkg.getCompilationUnit(PACKAGEINFOJAVA);
if (pkgInfoJava != null && pkgInfoJava.exists()) {
ISourceRange range = findPackageInfoJavaVersionLocation(baselineInfo.packageName, pkgInfoJava);
Map<String, Object> attribs = new HashMap<String, Object>();
attribs.put(IMarker.MESSAGE, message.trim());
attribs.put(IJavaModelMarker.ID, 8088);
attribs.put(PROP_SUGGESTED_VERSION, baselineInfo.suggestedVersion.toString());
if (range != null) {
attribs.put(IMarker.CHAR_START, range.getOffset());
attribs.put(IMarker.CHAR_END, range.getOffset() + range.getLength());
markers.add(new MarkerData(pkgInfoJava.getResource(), attribs, true, BndtoolsConstants.MARKER_JAVA_BASELINE));
}
}
}
}
}
return markers;
}
use of org.eclipse.jdt.core.ISourceRange in project flux by eclipse.
the class JavaDocService method getJavadocFromSourceElement.
public String getJavadocFromSourceElement(IMember member) {
JavaDocCommentReader reader = null;
try {
IBuffer buffer = member.getOpenable().getBuffer();
if (buffer == null) {
return null;
}
ISourceRange javadocRange = member.getJavadocRange();
if (javadocRange != null) {
reader = new JavaDocCommentReader(buffer, javadocRange.getOffset(), javadocRange.getOffset() + javadocRange.getLength() - 1);
StringBuffer buf = new StringBuffer();
char[] charBuffer = new char[1024];
int count;
try {
while ((count = reader.read(charBuffer)) != -1) buf.append(charBuffer, 0, count);
} catch (IOException e) {
return null;
}
return buf.toString();
}
} catch (JavaModelException e) {
e.printStackTrace();
} finally {
if (reader != null)
reader.close();
}
return null;
}
Aggregations