mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
Groovy: import static intention should be available on argument list
This commit is contained in:
+56
-32
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2000-2013 JetBrains s.r.o.
|
||||
* Copyright 2000-2014 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -23,13 +23,17 @@ import com.intellij.psi.search.LocalSearchScope;
|
||||
import com.intellij.psi.search.searches.ReferencesSearch;
|
||||
import com.intellij.util.IncorrectOperationException;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.jetbrains.plugins.groovy.codeStyle.GrReferenceAdjuster;
|
||||
import org.jetbrains.plugins.groovy.intentions.base.Intention;
|
||||
import org.jetbrains.plugins.groovy.intentions.base.PsiElementPredicate;
|
||||
import org.jetbrains.plugins.groovy.lang.psi.GrQualifiedReference;
|
||||
import org.jetbrains.plugins.groovy.lang.psi.GroovyFile;
|
||||
import org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory;
|
||||
import org.jetbrains.plugins.groovy.lang.psi.GroovyRecursiveElementVisitor;
|
||||
import org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList;
|
||||
import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression;
|
||||
import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrMethodCall;
|
||||
import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression;
|
||||
import org.jetbrains.plugins.groovy.lang.psi.api.toplevel.imports.GrImportStatement;
|
||||
import org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeArgumentList;
|
||||
@@ -41,27 +45,19 @@ public class ImportStaticIntention extends Intention {
|
||||
private static final Key<PsiElement> TEMP_REFERENT_USER_DATA = new Key<PsiElement>("TEMP_REFERENT_USER_DATA");
|
||||
|
||||
@Override
|
||||
protected void processIntention(@NotNull PsiElement element, final Project project, final Editor editor)
|
||||
throws IncorrectOperationException {
|
||||
final PsiElement resolved;
|
||||
final String name;
|
||||
final GroovyFile file;
|
||||
final GrImportStatement importStatement;
|
||||
boolean isAnythingShortened;
|
||||
if (!(element instanceof GrReferenceExpression)) return;
|
||||
final GrReferenceExpression ref = (GrReferenceExpression)element;
|
||||
resolved = ref.resolve();
|
||||
protected void processIntention(@NotNull PsiElement element, final Project project, final Editor editor) throws IncorrectOperationException {
|
||||
final PsiElement resolved = resolve(element);
|
||||
if (!(resolved instanceof PsiMember)) return;
|
||||
|
||||
final PsiClass containingClass = ((PsiMember)resolved).getContainingClass();
|
||||
if (containingClass == null) return;
|
||||
final String qname = containingClass.getQualifiedName();
|
||||
name = ((PsiMember)resolved).getName();
|
||||
final String name = ((PsiMember)resolved).getName();
|
||||
if (name == null) return;
|
||||
|
||||
final PsiFile containingFile = element.getContainingFile();
|
||||
if (!(containingFile instanceof GroovyFile)) return;
|
||||
file = (GroovyFile)containingFile;
|
||||
final GroovyFile file = (GroovyFile)containingFile;
|
||||
file.accept(new GroovyRecursiveElementVisitor() {
|
||||
@Override
|
||||
public void visitReferenceExpression(GrReferenceExpression expression) {
|
||||
@@ -77,17 +73,9 @@ public class ImportStaticIntention extends Intention {
|
||||
|
||||
final GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(project);
|
||||
final GrImportStatement tempImport = factory.createImportStatementFromText(qname + "." + name, true, false, null);
|
||||
importStatement = file.addImport(tempImport);
|
||||
|
||||
isAnythingShortened = false;
|
||||
for (PsiReference reference : ReferencesSearch.search(resolved, new LocalSearchScope(containingFile))) {
|
||||
final PsiElement refElement = reference.getElement();
|
||||
if (refElement instanceof GrQualifiedReference<?>) {
|
||||
isAnythingShortened |=
|
||||
org.jetbrains.plugins.groovy.codeStyle.GrReferenceAdjuster.shortenReference((GrQualifiedReference<?>)refElement);
|
||||
}
|
||||
}
|
||||
final GrImportStatement importStatement = file.addImport(tempImport);
|
||||
|
||||
boolean isAnythingShortened = shortenUsages(resolved, containingFile);
|
||||
if (!isAnythingShortened) {
|
||||
importStatement.delete();
|
||||
return;
|
||||
@@ -111,7 +99,7 @@ public class ImportStaticIntention extends Intention {
|
||||
if (qualifier instanceof GrReferenceExpression) {
|
||||
PsiElement aClass = ((GrReferenceExpression)qualifier).resolve();
|
||||
if (aClass == ((PsiMember)resolved).getContainingClass()) {
|
||||
org.jetbrains.plugins.groovy.codeStyle.GrReferenceAdjuster.shortenReference(expression);
|
||||
GrReferenceAdjuster.shortenReference(expression);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -129,6 +117,18 @@ public class ImportStaticIntention extends Intention {
|
||||
});
|
||||
}
|
||||
|
||||
private static boolean shortenUsages(PsiElement resolved, PsiFile containingFile) {
|
||||
boolean isAnythingShortened = false;
|
||||
for (PsiReference reference : ReferencesSearch.search(resolved, new LocalSearchScope(containingFile))) {
|
||||
final PsiElement refElement = reference.getElement();
|
||||
if (refElement instanceof GrQualifiedReference<?>) {
|
||||
boolean shortened = GrReferenceAdjuster.shortenReference((GrQualifiedReference<?>)refElement);
|
||||
isAnythingShortened |= shortened;
|
||||
}
|
||||
}
|
||||
return isAnythingShortened;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean isStopElement(PsiElement element) {
|
||||
return super.isStopElement(element) || element instanceof GrReferenceExpression;
|
||||
@@ -140,15 +140,39 @@ public class ImportStaticIntention extends Intention {
|
||||
return new PsiElementPredicate() {
|
||||
@Override
|
||||
public boolean satisfiedBy(PsiElement element) {
|
||||
if (!(element instanceof GrReferenceExpression)) return false;
|
||||
final GrReferenceExpression ref = (GrReferenceExpression)element;
|
||||
if (ref.getQualifier() == null) return false;
|
||||
final PsiElement resolved = ref.resolve();
|
||||
if (resolved == null) return false;
|
||||
return resolved instanceof PsiMember && !(resolved instanceof PsiClass) &&
|
||||
((PsiMember)resolved).hasModifierProperty(PsiModifier.STATIC) &&
|
||||
((PsiMember)resolved).getContainingClass() != null;
|
||||
final PsiElement resolved = resolve(element);
|
||||
|
||||
if (resolved == null) return false;
|
||||
return resolved instanceof PsiMember && !(resolved instanceof PsiClass) &&
|
||||
((PsiMember)resolved).hasModifierProperty(PsiModifier.STATIC) &&
|
||||
((PsiMember)resolved).getContainingClass() != null;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private static PsiElement resolve(PsiElement element) {
|
||||
GrReferenceExpression ref = findRef(element);
|
||||
|
||||
if (ref == null || ref.getQualifier() == null) return null;
|
||||
return ref.resolve();
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private static GrReferenceExpression findRef(PsiElement element) {
|
||||
if ((element instanceof GrReferenceExpression)) {
|
||||
return (GrReferenceExpression)element;
|
||||
|
||||
}
|
||||
else if (element instanceof GrArgumentList) {
|
||||
PsiElement parent = element.getParent();
|
||||
if (parent instanceof GrMethodCall) {
|
||||
GrExpression invoked = ((GrMethodCall)parent).getInvokedExpression();
|
||||
if (invoked instanceof GrReferenceExpression) {
|
||||
return ((GrReferenceExpression)invoked);
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user