private void doVisitReferenceElement(@NotNull PsiJavaCodeReferenceElement ref) {
    JavaResolveResult result = HighlightVisitorImpl.resolveOptimised(ref, myFile);
    PsiElement resolved = result != null ? result.getElement() : null;

    if (resolved instanceof PsiVariable variable) {
      if (!(variable instanceof PsiField)) {
        PsiElement containingClass = PsiTreeUtil.getNonStrictParentOfType(ref, PsiClass.class, PsiLambdaExpression.class);
        while ((containingClass instanceof PsiAnonymousClass || containingClass instanceof PsiLambdaExpression) &&
               !PsiTreeUtil.isAncestor(containingClass, variable, false)) {
          if (containingClass instanceof PsiLambdaExpression ||
              !PsiTreeUtil.isAncestor(((PsiAnonymousClass)containingClass).getArgumentList(), ref, false)) {
            myHolder.add(HighlightNamesUtil.highlightImplicitAnonymousClassParameter(ref));
            break;
          }
          containingClass = PsiTreeUtil.getParentOfType(containingClass, PsiClass.class, PsiLambdaExpression.class);
        }
      }

      TextAttributesScheme colorsScheme = myHolder.getColorsScheme();
      PsiElement nameElement = ref.getReferenceNameElement();
      if (nameElement != null) {
        myHolder.add(HighlightNamesUtil.highlightVariableName(variable, nameElement, colorsScheme));
      }
    }
    else {
      highlightReferencedMethodOrClassName(ref, resolved);
    }
  }
private void doVisitReferenceElement(@NotNull PsiJavaCodeReferenceElement ref)