private IJavaElement getJavaElement(IJavaProject project, String fqn, int offset) throws JavaModelException { IJavaElement originalElement = null; IType type = project.findType(fqn); ICodeAssist codeAssist; if (type.isBinary()) { codeAssist = type.getClassFile(); } else { codeAssist = type.getCompilationUnit(); } IJavaElement[] elements = null; if (codeAssist != null) { elements = codeAssist.codeSelect(offset, 0); } if (elements != null && elements.length > 0) { originalElement = elements[0]; } return originalElement; }
/** * Returns the Java elements at the given hover region. * * @param textViewer the text viewer * @param hoverRegion the hover region * @return the array with the Java elements or <code>null</code> * @since 3.4 */ protected IJavaElement[] getJavaElementsAt(ITextViewer textViewer, IRegion hoverRegion) { /* * The region should be a word region an not of length 0. * This check is needed because codeSelect(...) also finds * the Java element if the offset is behind the word. */ if (hoverRegion.getLength() == 0) return null; IDocument document= textViewer.getDocument(); if (document != null && isInheritDoc(document, hoverRegion)) return null; ICodeAssist resolve= getCodeAssist(); if (resolve != null) { try { return resolve.codeSelect(hoverRegion.getOffset(), hoverRegion.getLength()); } catch (JavaModelException x) { return null; } } return null; }
public OpenDeclarationDescriptor findDeclaration(IJavaProject project, String fqn, int offset) throws JavaModelException { IJavaElement originalElement = null; IType type = project.findType(fqn); ICodeAssist codeAssist; if (type.isBinary()) { codeAssist = type.getClassFile(); } else { codeAssist = type.getCompilationUnit(); } IJavaElement[] elements = null; if (codeAssist != null) { elements = codeAssist.codeSelect(offset, 0); } if (elements != null && elements.length > 0) { originalElement = elements[0]; } IJavaElement element = originalElement; while (element != null) { if (element instanceof ICompilationUnit) { ICompilationUnit unit = ((ICompilationUnit) element).getPrimary(); return compilationUnitNavigation(unit, originalElement); } if (element instanceof IClassFile) { return classFileNavigation((IClassFile) element, originalElement); } element = element.getParent(); } return null; }
public static IJavaElement[] codeResolve(IJavaElement input, ITextSelection selection) throws JavaModelException { if (input instanceof ICodeAssist) { if (input instanceof ICompilationUnit) { JavaModelUtil.reconcile((ICompilationUnit) input); } IJavaElement[] elements = ((ICodeAssist) input).codeSelect(selection.getOffset() + selection.getLength(), 0); if (elements.length > 0) { return elements; } } return EMPTY_RESULT; }
public String findJavadoc(IJavaProject project, String fqn, int offset) throws JavaModelException { IMember member = null; IType type = project.findType(fqn); ICodeAssist codeAssist; if (type.isBinary()) { codeAssist = type.getClassFile(); } else { codeAssist = type.getCompilationUnit(); } IJavaElement[] elements = null; if (codeAssist != null) { elements = codeAssist.codeSelect(/*region.getOffset(), region.getLength()*/ offset, 0); } IJavaElement element = null; if (elements != null && elements.length > 0) { element = elements[0]; } if (element != null && element instanceof IMember) { member = ((IMember) element); } if (member == null) { return null; } return getJavadoc(member); }
/** * Converts the given selection to a structured selection * containing Java elements. * * @param selection the selection * @return a structured selection with Java elements */ private IStructuredSelection convertToJavaElementSelection(ISelection selection) { if (!(selection instanceof ITextSelection && fCurrentViewInput instanceof ISourceReference)) return StructuredSelection.EMPTY; ITextSelection textSelection= (ITextSelection)selection; Object codeAssist= fCurrentViewInput.getAncestor(IJavaElement.COMPILATION_UNIT); if (codeAssist == null) codeAssist= fCurrentViewInput.getAncestor(IJavaElement.CLASS_FILE); if (codeAssist instanceof ICodeAssist) { IJavaElement[] elements= null; try { ISourceRange range= ((ISourceReference)fCurrentViewInput).getSourceRange(); elements= ((ICodeAssist)codeAssist).codeSelect(range.getOffset() + getOffsetInUnclippedDocument(textSelection), textSelection.getLength()); } catch (JavaModelException e) { return StructuredSelection.EMPTY; } if (elements != null && elements.length > 0) { return new StructuredSelection(elements[0]); } else return StructuredSelection.EMPTY; } return StructuredSelection.EMPTY; }
private static IJavaElement[] codeResolve(IJavaElement input, ITextSelection selection) throws JavaModelException { if (input instanceof ICodeAssist) { if (input instanceof ICompilationUnit) { ICompilationUnit cunit= (ICompilationUnit)input; if (cunit.isWorkingCopy()) JavaModelUtil.reconcile(cunit); } IJavaElement[] elements= ((ICodeAssist)input).codeSelect(selection.getOffset(), selection.getLength()); if (elements != null && elements.length > 0) return elements; } return EMPTY_RESULT; }
public static IJavaElement[] codeResolve(IJavaElement input, ITextSelection selection) throws JavaModelException { if (input instanceof ICodeAssist) { if (input instanceof ICompilationUnit) { JavaModelUtil.reconcile((ICompilationUnit) input); } IJavaElement[] elements= ((ICodeAssist)input).codeSelect(selection.getOffset() + selection.getLength(), 0); if (elements.length > 0) { return elements; } } return EMPTY_RESULT; }
protected ICodeAssist getCodeAssist() { if (fEditor != null) { IEditorInput input= fEditor.getEditorInput(); if (input instanceof IClassFileEditorInput) { IClassFileEditorInput cfeInput= (IClassFileEditorInput) input; return cfeInput.getClassFile(); } WorkingCopyManager manager= JavaPlugin.getDefault().getWorkingCopyManager(); return manager.getWorkingCopy(input, false); } return null; }
/** * Tokenizes the supplied {@code ITextSelection} if the selected text corresponds to an * {@code IJavaElement}. */ private void tokenize(final ITextSelection selection) { final IWorkbenchPage activePage = PlatformUI.getWorkbench().getActiveWorkbenchWindow() .getActivePage(); if (activePage != null) { final IEditorPart editor = activePage.getActiveEditor(); final IEditorInput editorInput = editor.getEditorInput(); final IJavaElement element = JavaUI.getEditorInputJavaElement(editorInput); if (element instanceof ICodeAssist) { final ICodeAssist root = (ICodeAssist) element; try { final int offset = selection.getOffset(); final int length = selection.getLength(); final IJavaElement[] elements = root.codeSelect(offset, length); if (elements.length > 0) { tokenize(elements[0]); } } catch (final JavaModelException e) { // do nothing } } } }
public IHyperlink[] detectHyperlinks(ITextViewer textViewer, IRegion region, boolean canShowMultipleHyperlinks) { ITextEditor textEditor= (ITextEditor)getAdapter(ITextEditor.class); if (region == null || !(textEditor instanceof JavaEditor)) return null; IAction openAction= textEditor.getAction("OpenEditor"); //$NON-NLS-1$ if (!(openAction instanceof SelectionDispatchAction)) return null; int offset= region.getOffset(); ITypeRoot input= EditorUtility.getEditorInputJavaElement(textEditor, false); if (input == null) return null; try { IDocumentProvider documentProvider= textEditor.getDocumentProvider(); IEditorInput editorInput= textEditor.getEditorInput(); IDocument document= documentProvider.getDocument(editorInput); IRegion wordRegion= JavaWordFinder.findWord(document, offset); if (wordRegion == null || wordRegion.getLength() == 0) return null; if (isInheritDoc(document, wordRegion) && getClass() != JavaElementHyperlinkDetector.class) return null; if (JavaElementHyperlinkDetector.class == getClass() && findBreakOrContinueTarget(input, region) != null) return new IHyperlink[] { new JavaElementHyperlink(wordRegion, (SelectionDispatchAction)openAction, null, false) }; IJavaElement[] elements; long modStamp= documentProvider.getModificationStamp(editorInput); if (input.equals(fLastInput) && modStamp == fLastModStamp && wordRegion.equals(fLastWordRegion)) { elements= fLastElements; } else { elements= ((ICodeAssist) input).codeSelect(wordRegion.getOffset(), wordRegion.getLength()); elements= selectOpenableElements(elements); fLastInput= input; fLastModStamp= modStamp; fLastWordRegion= wordRegion; fLastElements= elements; } if (elements.length == 0) return null; ArrayList<IHyperlink> links= new ArrayList<IHyperlink>(elements.length); for (int i= 0; i < elements.length; i++) { addHyperlinks(links, wordRegion, (SelectionDispatchAction)openAction, elements[i], elements.length > 1, (JavaEditor)textEditor); } if (links.size() == 0) return null; return CollectionsUtil.toArray(links, IHyperlink.class); } catch (JavaModelException e) { return null; } }
protected IJavaElement[] codeSelect(ISourceReference sourceReference, String selectAt, String selection) throws JavaModelException { String str = sourceReference.getSource(); int start = str.indexOf(selectAt); int length = selection.length(); return ((ICodeAssist)sourceReference).codeSelect(start, length); }