@Override
  public void visitReferenceExpression(@NotNull PsiReferenceExpression reference) {
    visitElement(reference);
    final PsiElement referenceParent = reference.getParent();

    final CompileContext context = myCompilingVisitor.getContext();
    final CompiledPattern pattern = context.getPattern();
    final boolean typedVar = pattern.isRealTypedVar(reference) &&
                             reference.getQualifierExpression() == null &&
                             !(referenceParent instanceof PsiExpressionStatement);

    final MatchingHandler handler = pattern.getHandler(reference);
    if (reference.getParent() instanceof PsiLambdaExpression) {
      GlobalCompilingVisitor.setFilter(handler, element -> true);
    }
    else {
      GlobalCompilingVisitor.setFilter(handler, ExpressionFilter.getInstance());
    }

    // We want to merge qname related to class to find it in any form
    final String referencedName = reference.getReferenceName();

    if (!typedVar && !(handler instanceof SubstitutionHandler)) {
      // When in dumb mode fall back to first character of name is upper case heuristic to identify classes.
      // Which is basically already always used when the referenced class is not in java.lang,
      // because SSR patterns have no imports so resolve will return null.
      final PsiElement resolve = DumbService.isDumb(context.getProject()) ? null : reference.resolve();

      final PsiElement referenceQualifier = reference.getQualifier();
      if (resolve instanceof PsiClass ||
          resolve == null && (referencedName != null && Character.isUpperCase(referencedName.charAt(0)) || referenceQualifier == null)) {
        boolean hasNoNestedSubstitutionHandlers = false;
        PsiExpression qualifier;
        PsiReferenceExpression currentReference = reference;

        while ((qualifier = currentReference.getQualifierExpression()) != null) {
          if (!(qualifier instanceof PsiReferenceExpression) || pattern.getHandler(qualifier) instanceof SubstitutionHandler) {
            hasNoNestedSubstitutionHandlers = true;
            break;
          }
          currentReference = (PsiReferenceExpression)qualifier;
        }
        if (!hasNoNestedSubstitutionHandlers && PsiTreeUtil.getChildOfType(reference, PsiAnnotation.class) == null) {
          final String text;
          if (resolve != null) {
            final String fqName = ((PsiClass)resolve).getQualifiedName();
            text = (fqName == null) ? reference.getText() : fqName;
          }
          else {
            text = reference.getText();
          }
          createAndSetSubstitutionHandlerFromReference(reference, text, referenceParent instanceof PsiReferenceExpression);
        }
      }
    }
  }
@Override
  public void visitReferenceExpression(@NotNull PsiReferenceExpression reference)