PsiPatternVariable (@NotNull PsiPatternVariable variable) { PsiPattern pattern = variable.getPattern(); PatternResolveState hint = PatternResolveState.WHEN_TRUE; VariablesNotProcessor proc = new VariablesNotProcessor(variable, false) { @Override protected boolean check(PsiVariable var, ResolveState state) { return var instanceof PsiPatternVariable && super.check(var, state); } }; PsiElement lastParent = pattern; for (PsiElement parent = lastParent.getParent(); parent != null; lastParent = parent, parent = parent.getParent()) { if (parent instanceof PsiInstanceOfExpression || parent instanceof PsiParenthesizedExpression) continue; if (parent instanceof PsiPrefixExpression && ((PsiPrefixExpression)parent).getOperationTokenType().equals(JavaTokenType.EXCL)) { hint = hint.invert(); continue; } if (parent instanceof PsiPolyadicExpression) { IElementType tokenType = ((PsiPolyadicExpression)parent).getOperationTokenType(); if (tokenType.equals(JavaTokenType.ANDAND) || tokenType.equals(JavaTokenType.OROR)) { PatternResolveState targetHint = PatternResolveState.fromBoolean(tokenType.equals(JavaTokenType.OROR)); if (hint == targetHint) { for (PsiExpression operand : ((PsiPolyadicExpression)parent).getOperands()) { if (operand == lastParent) break; operand.processDeclarations(proc, hint.putInto(ResolveState.initial()), null, pattern); } } continue; } } if (parent instanceof PsiConditionalExpression conditional) { PsiExpression thenExpression = conditional.getThenExpression(); if (lastParent == thenExpression) { conditional.getCondition() .processDeclarations(proc, PatternResolveState.WHEN_FALSE.putInto(ResolveState.initial()), null, pattern); } else if (lastParent == conditional.getElseExpression()) { conditional.getCondition() .processDeclarations(proc, PatternResolveState.WHEN_TRUE.putInto(ResolveState.initial()), null, pattern); if (thenExpression != null) { thenExpression.processDeclarations(proc, hint.putInto(ResolveState.initial()), null, pattern); } } } break; } return proc.size() > 0 ? (PsiPatternVariable)proc.getResult(0) : null; }