PsiExpression (PsiFile file, int startOffset, int endOffset) { if (!file.getViewProvider().getLanguages().contains(JavaLanguage.INSTANCE)) return null; PsiExpression expression = findElementInRange(file, startOffset, endOffset, PsiExpression.class); if (expression == null && findStatementsInRange(file, startOffset, endOffset).length == 0) { PsiElement element2 = file.getViewProvider().findElementAt(endOffset - 1, JavaLanguage.INSTANCE); if (element2 instanceof PsiJavaToken token) { final IElementType tokenType = token.getTokenType(); if (tokenType.equals(JavaTokenType.SEMICOLON) || element2.getParent() instanceof PsiErrorElement) { expression = findElementInRange(file, startOffset, element2.getTextRange().getStartOffset(), PsiExpression.class); } } } if (expression == null && findStatementsInRange(file, startOffset, endOffset).length == 0) { PsiElement element = PsiTreeUtil.skipWhitespacesBackward(file.findElementAt(endOffset)); if (element != null) { element = PsiTreeUtil.skipWhitespacesAndCommentsBackward(element.getLastChild()); if (element != null) { final int newEndOffset = element.getTextRange().getEndOffset(); if (newEndOffset < endOffset) { expression = findExpressionInRange(file, startOffset, newEndOffset); } } } } if (expression instanceof PsiReferenceExpression && expression.getParent() instanceof PsiMethodCallExpression) return null; return expression; }