List<PsiLambdaExpression> (@NotNull SourcePosition position, final boolean onlyOnTheLine) { ApplicationManager.getApplication().assertReadAccessAllowed(); PsiFile file = position.getFile(); final int line = position.getLine(); final Document document = file.getViewProvider().getDocument(); if (document == null || line < 0 || line >= document.getLineCount()) { return Collections.emptyList(); } TextRange lineRange = DocumentUtil.getLineTextRange(document, line); // always start from the beginning of the line for consistency PsiElement element = file.findElementAt(lineRange.getStartOffset()); if (element == null) { return Collections.emptyList(); } final List<PsiLambdaExpression> lambdas = new SmartList<>(); final PsiElementVisitor lambdaCollector = new JavaRecursiveElementVisitor() { @Override public void visitLambdaExpression(@NotNull PsiLambdaExpression expression) { super.visitLambdaExpression(expression); if (!onlyOnTheLine || getFirstElementOnTheLine(expression, document, line) != null) { lambdas.add(expression); } } }; element.accept(lambdaCollector); for (PsiElement sibling = getNextElement(element); sibling != null; sibling = getNextElement(sibling)) { if (!intersects(lineRange, sibling)) { break; } sibling.accept(lambdaCollector); } // add initial lambda if we're inside already PsiElement method = getContainingMethod(element); if (method instanceof PsiLambdaExpression && !lambdas.contains(method)) { lambdas.add((PsiLambdaExpression)method); } return lambdas; }