private static boolean isIdentifier(@NotNull PsiElement element) {
    if (element instanceof ForeignLeafPsiElement) return false;
    PsiElement parent = element.getParent();
    PsiElement identifier;
    if (parent instanceof PsiNameIdentifierOwner &&
        (identifier = ((PsiNameIdentifierOwner)parent).getNameIdentifier()) != null) {
      // Groovy has this twisted PSI where method.getNameIdentifier() is some random light element
      String text = element.getText();
      return identifier == element || text.equals(identifier.getText());
    }
    // or it maybe the reference name
    if (parent instanceof PsiReference) {
      PsiElement refElement = ((PsiReference)parent).getElement();
      return refElement == parent || refElement == element;
    }
    return false;
  }
private static boolean isIdentifier(@NotNull PsiElement element)