use of org.jetbrains.plugins.groovy.lang.psi.api.toplevel.packaging.GrPackageDefinition in project intellij-community by JetBrains.
the class GrUnresolvedAccessChecker method checkCodeRefInner.
@Nullable
private HighlightInfo checkCodeRefInner(GrCodeReferenceElement refElement) {
if (PsiTreeUtil.getParentOfType(refElement, GroovyDocPsiElement.class) != null)
return null;
PsiElement nameElement = refElement.getReferenceNameElement();
if (nameElement == null)
return null;
if (isResolvedStaticImport(refElement))
return null;
GroovyResolveResult resolveResult = refElement.advancedResolve();
final PsiElement resolved = resolveResult.getElement();
if (!(refElement.getParent() instanceof GrPackageDefinition) && resolved == null) {
String message = GroovyBundle.message("cannot.resolve", refElement.getReferenceName());
HighlightInfo info = HighlightInfo.newHighlightInfo(HighlightInfoType.WRONG_REF).range(nameElement).descriptionAndTooltip(message).create();
// todo implement for nested classes
registerCreateClassByTypeFix(refElement, info, myDisplayKey);
registerAddImportFixes(refElement, info, myDisplayKey);
UnresolvedReferenceQuickFixProvider.registerReferenceFixes(refElement, new QuickFixActionRegistrarAdapter(info, myDisplayKey));
QuickFixFactory.getInstance().registerOrderEntryFixes(new QuickFixActionRegistrarAdapter(info, myDisplayKey), refElement);
return info;
}
if (refElement.getParent() instanceof GrNewExpression) {
boolean inStaticContext = GrStaticChecker.isInStaticContext(refElement);
if (!inStaticContext && GrUnresolvedAccessInspection.isSuppressed(refElement))
return null;
if (!inStaticContext) {
if (!myInspectionEnabled)
return null;
assert myInspection != null;
if (!myInspection.myHighlightInnerClasses)
return null;
}
GrNewExpression newExpression = (GrNewExpression) refElement.getParent();
if (resolved instanceof PsiClass) {
PsiClass clazz = (PsiClass) resolved;
final PsiClass outerClass = clazz.getContainingClass();
if (com.intellij.psi.util.PsiUtil.isInnerClass(clazz) && outerClass != null && newExpression.getArgumentList() != null && !PsiUtil.hasEnclosingInstanceInScope(outerClass, newExpression, true) && !hasEnclosingInstanceInArgList(newExpression.getArgumentList(), outerClass)) {
String qname = clazz.getQualifiedName();
LOG.assertTrue(qname != null, clazz.getText());
return createAnnotationForRef(refElement, inStaticContext, GroovyBundle.message("cannot.reference.non.static", qname));
}
}
}
return null;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.toplevel.packaging.GrPackageDefinition in project intellij-community by JetBrains.
the class GrUnresolvedAccessChecker method registerCreateClassByTypeFix.
private static void registerCreateClassByTypeFix(@NotNull GrReferenceElement refElement, @Nullable HighlightInfo info, final HighlightDisplayKey key) {
GrPackageDefinition packageDefinition = PsiTreeUtil.getParentOfType(refElement, GrPackageDefinition.class);
if (packageDefinition != null)
return;
PsiElement parent = refElement.getParent();
if (parent instanceof GrNewExpression && refElement.getManager().areElementsEquivalent(((GrNewExpression) parent).getReferenceElement(), refElement)) {
QuickFixAction.registerQuickFixAction(info, GroovyQuickFixFactory.getInstance().createClassFromNewAction((GrNewExpression) parent), key);
} else if (canBeClassOrPackage(refElement)) {
if (shouldBeInterface(refElement)) {
QuickFixAction.registerQuickFixAction(info, GroovyQuickFixFactory.getInstance().createClassFixAction(refElement, GrCreateClassKind.INTERFACE), key);
QuickFixAction.registerQuickFixAction(info, GroovyQuickFixFactory.getInstance().createClassFixAction(refElement, GrCreateClassKind.TRAIT), key);
} else if (shouldBeClass(refElement)) {
QuickFixAction.registerQuickFixAction(info, GroovyQuickFixFactory.getInstance().createClassFixAction(refElement, GrCreateClassKind.CLASS), key);
QuickFixAction.registerQuickFixAction(info, GroovyQuickFixFactory.getInstance().createClassFixAction(refElement, GrCreateClassKind.ENUM), key);
} else if (shouldBeAnnotation(refElement)) {
QuickFixAction.registerQuickFixAction(info, GroovyQuickFixFactory.getInstance().createClassFixAction(refElement, GrCreateClassKind.ANNOTATION), key);
} else {
QuickFixAction.registerQuickFixAction(info, GroovyQuickFixFactory.getInstance().createClassFixAction(refElement, GrCreateClassKind.CLASS), key);
QuickFixAction.registerQuickFixAction(info, GroovyQuickFixFactory.getInstance().createClassFixAction(refElement, GrCreateClassKind.INTERFACE), key);
if (!refElement.isQualified() || resolvesToGroovy(refElement.getQualifier())) {
QuickFixAction.registerQuickFixAction(info, GroovyQuickFixFactory.getInstance().createClassFixAction(refElement, GrCreateClassKind.TRAIT), key);
}
QuickFixAction.registerQuickFixAction(info, GroovyQuickFixFactory.getInstance().createClassFixAction(refElement, GrCreateClassKind.ENUM), key);
QuickFixAction.registerQuickFixAction(info, GroovyQuickFixFactory.getInstance().createClassFixAction(refElement, GrCreateClassKind.ANNOTATION), key);
}
}
}
use of org.jetbrains.plugins.groovy.lang.psi.api.toplevel.packaging.GrPackageDefinition in project intellij-community by JetBrains.
the class GroovyFileImpl method checkIsScript.
private boolean checkIsScript() {
final GrTopStatement[] topStatements = findChildrenByClass(GrTopStatement.class);
boolean hasClassDefinitions = false;
boolean hasTopStatements = false;
for (GrTopStatement st : topStatements) {
if (st instanceof GrTypeDefinition) {
hasClassDefinitions = true;
} else if (!(st instanceof GrImportStatement || st instanceof GrPackageDefinition)) {
hasTopStatements = true;
break;
}
}
return hasTopStatements || !hasClassDefinitions;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.toplevel.packaging.GrPackageDefinition in project intellij-community by JetBrains.
the class GroovyFileImpl method setPackageName.
@Override
public void setPackageName(String packageName) {
final ASTNode fileNode = getNode();
final GrPackageDefinition currentPackage = getPackageDefinition();
if (packageName == null || packageName.isEmpty()) {
if (currentPackage != null) {
final ASTNode currNode = currentPackage.getNode();
fileNode.removeChild(currNode);
}
return;
}
final GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(getProject());
final GrPackageDefinition newPackage = (GrPackageDefinition) factory.createTopElementFromText("package " + packageName);
if (currentPackage != null) {
final GrCodeReferenceElement packageReference = currentPackage.getPackageReference();
if (packageReference != null) {
GrCodeReferenceElement ref = newPackage.getPackageReference();
if (ref != null) {
packageReference.replace(ref);
}
return;
}
}
final ASTNode newNode = newPackage.getNode();
if (currentPackage != null) {
final ASTNode currNode = currentPackage.getNode();
fileNode.replaceChild(currNode, newNode);
} else {
ASTNode anchor = fileNode.getFirstChildNode();
if (anchor != null && anchor.getElementType() == GroovyTokenTypes.mSH_COMMENT) {
anchor = anchor.getTreeNext();
fileNode.addLeaf(GroovyTokenTypes.mNLS, "\n", anchor);
}
fileNode.addChild(newNode, anchor);
if (anchor != null && !anchor.getText().startsWith("\n\n")) {
fileNode.addLeaf(GroovyTokenTypes.mNLS, "\n", anchor);
}
}
}
use of org.jetbrains.plugins.groovy.lang.psi.api.toplevel.packaging.GrPackageDefinition in project intellij-community by JetBrains.
the class GrPackageInspection method getElementToHighlight.
@Nullable
private static PsiElement getElementToHighlight(GroovyFile file) {
GrPackageDefinition packageDefinition = file.getPackageDefinition();
if (packageDefinition != null)
return packageDefinition;
PsiClass[] classes = file.getClasses();
for (PsiClass aClass : classes) {
if (!(aClass instanceof SyntheticElement) && aClass instanceof GrTypeDefinition) {
return ((GrTypeDefinition) aClass).getNameIdentifierGroovy();
}
}
GrTopStatement[] statements = file.getTopStatements();
if (statements.length > 0) {
GrTopStatement first = statements[0];
if (first instanceof GrNamedElement)
return ((GrNamedElement) first).getNameIdentifierGroovy();
return first;
}
return null;
}
Aggregations