PsiExpression (Editor editor, PsiFile file) { if (myExpression != null) { if (!myExpression.isValid()) return null; final PsiType type = myExpression.getType(); if (type instanceof PsiArrayType || InheritanceUtil.isInheritor(type, CommonClassNames.JAVA_LANG_ITERABLE)) return myExpression; return null; } final SelectionModel selectionModel = editor.getSelectionModel(); if (selectionModel.hasSelection()) { PsiElement elementAtStart = file.findElementAt(selectionModel.getSelectionStart()); PsiElement elementAtEnd = file.findElementAt(selectionModel.getSelectionEnd() - 1); if (elementAtStart == null || elementAtStart instanceof PsiWhiteSpace || elementAtStart instanceof PsiComment) { elementAtStart = PsiTreeUtil.skipWhitespacesAndCommentsForward(elementAtStart); if (elementAtStart == null) return null; } if (elementAtEnd == null || elementAtEnd instanceof PsiWhiteSpace || elementAtEnd instanceof PsiComment) { elementAtEnd = PsiTreeUtil.skipWhitespacesAndCommentsBackward(elementAtEnd); if (elementAtEnd == null) return null; } PsiElement parent = PsiTreeUtil.findCommonParent(elementAtStart, elementAtEnd); if (parent instanceof PsiExpression) { final PsiType type = ((PsiExpression)parent).getType(); return type instanceof PsiArrayType || InheritanceUtil.isInheritor(type, CommonClassNames.JAVA_LANG_ITERABLE) ? (PsiExpression)parent : null; } return null; } PsiElement element = file.findElementAt(editor.getCaretModel().getOffset()); while (element instanceof PsiWhiteSpace) { element = element.getPrevSibling(); } if (element instanceof PsiExpressionStatement) { element = ((PsiExpressionStatement)element).getExpression().getLastChild(); } while ((element = PsiTreeUtil.getParentOfType(element, PsiExpression.class, true)) != null) { final PsiElement parent = element.getParent(); if (parent instanceof PsiMethodCallExpression) continue; if (!(parent instanceof PsiExpressionStatement)) return null; final PsiType type = ((PsiExpression)element).getType(); if (type instanceof PsiArrayType || InheritanceUtil.isInheritor(type, CommonClassNames.JAVA_LANG_ITERABLE)) return (PsiExpression)element; } return null; }