use of org.jetbrains.plugins.groovy.lang.psi.GrQualifiedReference in project intellij-community by JetBrains.
the class ImportOnDemandIntention method processIntention.
@Override
protected void processIntention(@NotNull PsiElement element, @NotNull 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);
}
}
}
use of org.jetbrains.plugins.groovy.lang.psi.GrQualifiedReference in project intellij-community by JetBrains.
the class ImportStaticIntention method shortenUsages.
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;
}
use of org.jetbrains.plugins.groovy.lang.psi.GrQualifiedReference in project intellij-community by JetBrains.
the class GrStaticChecker method isInStaticContext.
public static boolean isInStaticContext(@NotNull PsiElement place, @Nullable PsiClass targetClass) {
if (place instanceof GrReferenceExpression) {
GrQualifiedReference reference = (GrQualifiedReference) place;
PsiElement qualifier = reference.getQualifier();
if (qualifier != null && !PsiUtil.isThisOrSuperRef(reference)) {
if (PsiUtil.isInstanceThisRef(qualifier) || PsiUtil.isSuperReference(qualifier)) {
return false;
} else if (PsiUtil.isThisReference(qualifier)) {
//instance 'this' already is processed. So it static 'this'
return true;
}
return qualifier instanceof GrQualifiedReference && ResolveUtil.resolvesToClass(qualifier);
}
if (PsiUtil.isSuperReference(reference))
return false;
//this reference should be checked as all other refs
}
PsiElement run = place;
while (run != null && run != targetClass) {
if (targetClass == null && run instanceof PsiClass)
return false;
if (run instanceof GrClosableBlock)
return false;
if (run instanceof PsiModifierListOwner && ((PsiModifierListOwner) run).hasModifierProperty(PsiModifier.STATIC))
return true;
run = run.getParent();
}
return false;
}
use of org.jetbrains.plugins.groovy.lang.psi.GrQualifiedReference in project intellij-community by JetBrains.
the class GrIntroduceFieldProcessor method replaceOccurrence.
@NotNull
private PsiElement replaceOccurrence(@NotNull GrVariable field, @NotNull PsiElement occurrence, @NotNull PsiClass containingClass) {
boolean isOriginal = occurrence == myContext.getExpression();
final GrReferenceExpression newExpr = createRefExpression(field, occurrence, containingClass);
final PsiElement replaced;
if (occurrence instanceof GrExpression) {
replaced = ((GrExpression) occurrence).replaceWithExpression(newExpr, false);
} else {
replaced = occurrence.replace(newExpr);
}
if (replaced instanceof GrQualifiedReference<?>) {
GrReferenceAdjuster.shortenReference((GrQualifiedReference<?>) replaced);
}
if (isOriginal) {
updateCaretPosition(replaced);
}
return replaced;
}
use of org.jetbrains.plugins.groovy.lang.psi.GrQualifiedReference in project intellij-community by JetBrains.
the class GroovyBlockGenerator method addNestedChildren.
private void addNestedChildren(final PsiElement elem, List<Block> list, @Nullable AlignmentProvider.Aligner aligner, final boolean topLevel, Wrap wrap) {
final List<ASTNode> children = visibleChildren(elem.getNode());
if (elem instanceof GrMethodCallExpression) {
GrExpression invokedExpression = ((GrMethodCallExpression) elem).getInvokedExpression();
if (invokedExpression instanceof GrQualifiedReference) {
final PsiElement nameElement = ((GrQualifiedReference) invokedExpression).getReferenceNameElement();
if (nameElement != null) {
List<ASTNode> grandChildren = visibleChildren(invokedExpression.getNode());
int i = 0;
while (i < grandChildren.size() && nameElement != grandChildren.get(i).getPsi()) i++;
if (i > 0) {
processNestedChildrenPrefix(list, aligner, false, grandChildren, i, wrap);
}
if (i < grandChildren.size()) {
LOG.assertTrue(nameElement == grandChildren.get(i).getPsi());
list.add(new MethodCallWithoutQualifierBlock(nameElement, wrap, topLevel, children, elem, myContext));
}
return;
}
}
}
processNestedChildrenPrefix(list, aligner, topLevel, children, children.size(), wrap);
}
Aggregations