@Override protected void processIntention(@NotNull PsiElement element, Project project, Editor editor) throws IncorrectOperationException { if (!(element instanceof GrReferenceElement)) return; final GrReferenceElement ref = (GrReferenceElement) element; final PsiElement resolved = ref.resolve(); if (!(resolved instanceof PsiClass)) return; final String qname = ((PsiClass) resolved).getQualifiedName(); final GrImportStatement importStatement = GroovyPsiElementFactory.getInstance(project) .createImportStatementFromText(qname, true, true, null); final PsiFile containingFile = element.getContainingFile(); if (!(containingFile instanceof GroovyFile)) return; ((GroovyFile) containingFile).addImport(importStatement); for (PsiReference reference : ReferencesSearch.search(resolved, new LocalSearchScope(containingFile))) { final PsiElement refElement = reference.getElement(); if (refElement == null) continue; final PsiElement parent = refElement.getParent(); if (parent instanceof GrQualifiedReference<?>) { org.jetbrains.plugins.groovy.codeStyle.GrReferenceAdjuster.shortenReference( (GrQualifiedReference<?>) parent); } } }
public GrReferenceExpression bindToElementViaStaticImport(@NotNull PsiMember member) { if (getQualifier() != null) { throw new IncorrectOperationException("Reference has qualifier"); } if (StringUtil.isEmpty(getReferenceName())) { throw new IncorrectOperationException("Reference has empty name"); } PsiClass containingClass = member.getContainingClass(); if (containingClass == null) { throw new IncorrectOperationException("Member has no containing class"); } final PsiFile file = getContainingFile(); if (file instanceof GroovyFile) { GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(getProject()); String text = "import static " + containingClass.getQualifiedName() + "." + member.getName(); final GrImportStatement statement = factory.createImportStatementFromText(text); ((GroovyFile) file).addImport(statement); } return this; }