PsiElement (@NotNull PsiElement psiElement) { final ProjectFileIndex index = ProjectRootManager.getInstance(psiElement.getProject()).getFileIndex(); final PsiFile containingFile = psiElement.getContainingFile(); if (containingFile instanceof GroovyFileBase) { final VirtualFile file = containingFile.getVirtualFile(); if (file != null && (index.isUnderSourceRootOfType(file, JavaModuleSourceRootTypes.SOURCES) || index.isInLibrary(file))) { if (psiElement instanceof GroovyFileBase grFile) { if (grFile.getViewProvider().getBaseLanguage().equals(GroovyLanguage.INSTANCE)) { final PsiClass[] psiClasses = grFile.getClasses(); if (psiClasses.length == 1 && !grFile.isScript()) { return psiClasses[0]; } } } else if (psiElement instanceof GrTypeDefinition) { return psiElement; } } return containingFile; } return psiElement; }