@Override public CompletableFuture<CodeLens> resolveCodeLens(CodeLens unresolved) { logInfo(">> codeLens/resolve"); CodeLensHandler handler = new CodeLensHandler(preferenceManager); return computeAsync((monitor) -> { waitForLifecycleJobs(monitor); return handler.resolve(unresolved, monitor); }); }
@Override public CompletableFuture<List<? extends CodeLens>> codeLens(CodeLensParams params) { logInfo(">> document/codeLens"); CodeLensHandler handler = new CodeLensHandler(preferenceManager); return computeAsync((monitor) -> { waitForLifecycleJobs(monitor); return handler.getCodeLensSymbols(params.getTextDocument().getUri(), monitor); }); }
collectCodeLenses(typeRoot, ((IType) element).getChildren(), lenses, monitor); } else if (element.getElementType() == IJavaElement.METHOD) { if (JDTUtils.isHiddenGeneratedElement(element)) { if (parentType != null && overlaps(((ISourceReference) parentType).getNameRange(), ((ISourceReference) element).getNameRange())) { continue; CodeLens lens = getCodeLens(REFERENCES_TYPE, element, typeRoot); if (lens != null) { lenses.add(lens); IType type = (IType) element; if (type.isInterface() || Flags.isAbstract(type.getFlags())) { CodeLens lens = getCodeLens(IMPLEMENTATION_TYPE, element, typeRoot); if (lens != null) { lenses.add(lens);
if (REFERENCES_TYPE.equals(type)) { try { locations = findReferences(element, monitor); } catch (CoreException e) { JavaLanguageServerPlugin.logException(e.getMessage(), e); IDocument document = JsonRpcHelpers.toDocument(typeRoot.getBuffer()); int offset = document.getLineOffset(position.getLine()) + position.getCharacter(); locations = findImplementations((IType) element, offset, monitor); } catch (CoreException | BadLocationException e) { JavaLanguageServerPlugin.logException(e.getMessage(), e);
private List<Location> findReferences(IJavaElement element, IProgressMonitor monitor) throws JavaModelException, CoreException { if (element == null) { return Collections.emptyList(); } SearchPattern pattern = SearchPattern.createPattern(element, IJavaSearchConstants.REFERENCES); final List<Location> result = new ArrayList<>(); SearchEngine engine = new SearchEngine(); engine.search(pattern, new SearchParticipant[] { SearchEngine.getDefaultSearchParticipant() }, createSearchScope(), new SearchRequestor() { @Override public void acceptSearchMatch(SearchMatch match) throws CoreException { Object o = match.getElement(); if (o instanceof IJavaElement) { IJavaElement element = (IJavaElement) o; ICompilationUnit compilationUnit = (ICompilationUnit) element .getAncestor(IJavaElement.COMPILATION_UNIT); if (compilationUnit == null) { return; } Location location = JDTUtils.toLocation(compilationUnit, match.getOffset(), match.getLength()); result.add(location); } } }, monitor); return result; }
public List<CodeLens> getCodeLensSymbols(String uri, IProgressMonitor monitor) { if (!preferenceManager.getPreferences().isCodeLensEnabled()) { return Collections.emptyList(); } final ICompilationUnit unit = JDTUtils.resolveCompilationUnit(uri); IClassFile classFile = null; if (unit == null) { classFile = JDTUtils.resolveClassFile(uri); if (classFile == null) { return Collections.emptyList(); } } else { if (!unit.getResource().exists() || monitor.isCanceled()) { return Collections.emptyList(); } } try { ITypeRoot typeRoot = unit != null ? unit : classFile; IJavaElement[] elements = typeRoot.getChildren(); LinkedHashSet<CodeLens> lenses = new LinkedHashSet<>(elements.length); collectCodeLenses(typeRoot, elements, lenses, monitor); if (monitor.isCanceled()) { lenses.clear(); } return new ArrayList<>(lenses); } catch (JavaModelException e) { JavaLanguageServerPlugin.logException("Problem getting code lenses for" + unit.getElementName(), e); } return Collections.emptyList(); }