use of org.eclipse.jdt.core.ISourceRange in project che by eclipse.
the class SourceTypeConverter method convertAnnotations.
private Annotation[] convertAnnotations(IAnnotatable element) throws JavaModelException {
IAnnotation[] annotations = element.getAnnotations();
int length = annotations.length;
Annotation[] astAnnotations = new Annotation[length];
if (length > 0) {
char[] cuSource = getSource();
int recordedAnnotations = 0;
for (int i = 0; i < length; i++) {
ISourceRange positions = annotations[i].getSourceRange();
int start = positions.getOffset();
int end = start + positions.getLength();
char[] annotationSource = CharOperation.subarray(cuSource, start, end);
if (annotationSource != null) {
Expression expression = parseMemberValue(annotationSource);
/*
* expression can be null or not an annotation if the source has changed between
* the moment where the annotation source positions have been retrieved and the moment were
* this parsing occurred.
* See https://bugs.eclipse.org/bugs/show_bug.cgi?id=90916
*/
if (expression instanceof Annotation) {
astAnnotations[recordedAnnotations++] = (Annotation) expression;
}
}
}
if (length != recordedAnnotations) {
// resize to remove null annotations
System.arraycopy(astAnnotations, 0, (astAnnotations = new Annotation[recordedAnnotations]), 0, recordedAnnotations);
}
}
return astAnnotations;
}
use of org.eclipse.jdt.core.ISourceRange in project tdi-studio-se by Talend.
the class OpenDeclarationAction method highlightMethod.
/**
* Highlights the method on editor.
*
* @param editorInput The editor input
* @param editorPart The editor part
* @throws CoreException
*/
private void highlightMethod(IEditorInput editorInput, IEditorPart editorPart) throws CoreException {
if (!(editorPart instanceof ITextEditor) || methodName == null || parameters == null) {
return;
}
ITextEditor textEditor = (ITextEditor) editorPart;
IDocumentProvider provider = textEditor.getDocumentProvider();
provider.connect(editorInput);
int offset = 0;
int length = 0;
ISourceRange range = getMethodNameRange(editorInput);
if (range != null) {
offset = range.getOffset();
length = range.getLength();
}
textEditor.selectAndReveal(offset, length);
provider.disconnect(editorInput);
}
use of org.eclipse.jdt.core.ISourceRange in project webtools.sourceediting by eclipse.
the class JSPJavaHyperlinkDetector method createHyperlink.
private IHyperlink createHyperlink(IJavaElement element, IRegion region, IDocument document) {
IHyperlink link = null;
if (region != null) {
// open local variable in the JSP file...
boolean isInTranslationCU = false;
if (element instanceof ISourceReference) {
IFile file = null;
int jspOffset = 0;
// try to locate the file in the workspace
ITextFileBuffer textFileBuffer = FileBuffers.getTextFileBufferManager().getTextFileBuffer(document);
if (textFileBuffer != null && textFileBuffer.getLocation() != null) {
file = getFile(textFileBuffer.getLocation().toString());
}
// get Java range and translate to JSP range
try {
ISourceRange range = null;
IJSPTranslation jspTranslation = getJSPTranslation(document);
if (jspTranslation != null) {
// link to local variable definitions
if (element instanceof ILocalVariable) {
range = ((ILocalVariable) element).getNameRange();
Object cu = ((ILocalVariable) element).getAncestor(IJavaElement.COMPILATION_UNIT);
if (cu != null && cu.equals(jspTranslation.getCompilationUnit()))
isInTranslationCU = true;
} else // linking to fields of the same compilation unit
if (element.getElementType() == IJavaElement.FIELD) {
Object cu = ((IField) element).getCompilationUnit();
if (cu != null && cu.equals(jspTranslation.getCompilationUnit())) {
range = ((ISourceReference) element).getSourceRange();
isInTranslationCU = true;
}
} else // linking to methods of the same compilation unit
if (element.getElementType() == IJavaElement.METHOD) {
Object cu = ((IMethod) element).getCompilationUnit();
if (cu != null && cu.equals(jspTranslation.getCompilationUnit())) {
range = ((ISourceReference) element).getSourceRange();
isInTranslationCU = true;
}
}
}
if (jspTranslation != null && range != null && file != null) {
jspOffset = jspTranslation.getJspOffset(range.getOffset());
if (jspOffset >= 0) {
link = new WorkspaceFileHyperlink(region, file, new Region(jspOffset, range.getLength()));
}
}
} catch (JavaModelException jme) {
Logger.log(Logger.WARNING_DEBUG, jme.getMessage(), jme);
}
}
if (link == null && !isInTranslationCU) {
// Don't try to open the translation CU
link = new JSPJavaHyperlink(region, element);
}
}
return link;
}
use of org.eclipse.jdt.core.ISourceRange in project eclipse.jdt.ls by eclipse.
the class JavadocContentAccess method internalGetContentReader.
/**
* Gets a reader for an IMember's Javadoc comment content from the source attachment.
* The content does contain only the text from the comment without the Javadoc leading star characters.
* Returns <code>null</code> if the member does not contain a Javadoc comment or if no source is available.
* @param member The member to get the Javadoc of.
* @return Returns a reader for the Javadoc comment content or <code>null</code> if the member
* does not contain a Javadoc comment or if no source is available
* @throws JavaModelException is thrown when the elements javadoc can not be accessed
* @since 3.4
*/
private static Reader internalGetContentReader(IMember member) throws JavaModelException {
IBuffer buf = member.getOpenable().getBuffer();
if (buf == null) {
// no source attachment found
return null;
}
ISourceRange javadocRange = member.getJavadocRange();
if (javadocRange != null) {
JavaDocCommentReader reader = new JavaDocCommentReader(buf, javadocRange.getOffset(), javadocRange.getOffset() + javadocRange.getLength() - 1);
if (!containsOnlyInheritDoc(reader, javadocRange.getLength())) {
reader.reset();
return reader;
}
}
return null;
}
use of org.eclipse.jdt.core.ISourceRange in project eclipse.jdt.ls by eclipse.
the class CorrectPackageDeclarationProposal method addEdits.
@Override
protected void addEdits(IDocument doc, TextEdit root) throws CoreException {
super.addEdits(doc, root);
ICompilationUnit cu = getCompilationUnit();
IPackageFragment parentPack = (IPackageFragment) cu.getParent();
IPackageDeclaration[] decls = cu.getPackageDeclarations();
if (parentPack.isDefaultPackage() && decls.length > 0) {
for (int i = 0; i < decls.length; i++) {
ISourceRange range = decls[i].getSourceRange();
root.addChild(new DeleteEdit(range.getOffset(), range.getLength()));
}
return;
}
if (!parentPack.isDefaultPackage() && decls.length == 0) {
String lineDelim = "\n";
// $NON-NLS-1$
String str = "package " + parentPack.getElementName() + ';' + lineDelim + lineDelim;
root.addChild(new InsertEdit(0, str));
return;
}
root.addChild(new ReplaceEdit(fLocation.getOffset(), fLocation.getLength(), parentPack.getElementName()));
}
Aggregations