PsiElement (Editor editor, PsiFile psiFile) { PsiElement atCaretElement = super.getStatementAtCaret(editor, psiFile); if (atCaretElement == null) { return null; } ASTNode atCaret = atCaretElement.getNode(); if (BasicJavaAstTreeUtil.isWhiteSpace(atCaret)) return null; if (BasicJavaAstTreeUtil.is(atCaret, JavaTokenType.RBRACE)) { atCaret = atCaret.getTreeParent(); boolean expressionEndingWithBrace = BasicJavaAstTreeUtil.is(atCaret, BASIC_ANONYMOUS_CLASS) || BasicJavaAstTreeUtil.is(atCaret, BASIC_ARRAY_INITIALIZER_EXPRESSION) || BasicJavaAstTreeUtil.is(atCaret, BASIC_CODE_BLOCK) && ( BasicJavaAstTreeUtil.is(atCaret.getTreeParent(), BASIC_LAMBDA_EXPRESSION) || BasicJavaAstTreeUtil.is(atCaret.getTreeParent(), BASIC_SWITCH_EXPRESSION)); if (!expressionEndingWithBrace) return null; } for (ASTNode each : SyntaxTraverser.astApi().parents(atCaret).skip(1)) { PsiElement psiElement = BasicJavaAstTreeUtil.toPsi(each); if (BasicJavaAstTreeUtil.is(each, MEMBER_SET) || isImportStatementBase(psiElement) || BasicJavaAstTreeUtil.is(each, BASIC_PACKAGE_STATEMENT) || BasicJavaAstTreeUtil.is(each, BASIC_ANNOTATION) && psiElement != null && PsiTreeUtil.hasErrorElements(psiElement)) { return each.getPsi(); } if (BasicJavaAstTreeUtil.is(each, BASIC_CODE_BLOCK) || BasicJavaAstTreeUtil.is(each, BASIC_JAVA_COMMENT_BIT_SET)) { return null; } if (BasicJavaAstTreeUtil.is(each, STATEMENT_SET)) { return BasicJavaAstTreeUtil.is(each.getTreeParent(), BASIC_FOR_STATEMENT) && !PsiTreeUtil.hasErrorElements(each.getPsi()) ? each.getPsi().getParent() : each.getPsi(); } if (BasicJavaAstTreeUtil.is(each, BASIC_CONDITIONAL_EXPRESSION) && PsiUtilCore.hasErrorElementChild(each.getPsi())) { return each.getPsi(); } } return null; }