use of org.jetbrains.plugins.groovy.lang.psi.GroovyFile in project intellij-community by JetBrains.
the class GroovyFindJarFix method getFqns.
@Override
protected Collection<String> getFqns(@NotNull GrReferenceElement ref) {
GrImportStatement importStatement = PsiTreeUtil.getParentOfType(ref.getElement(), GrImportStatement.class);
//from static imports
if (importStatement != null) {
GrCodeReferenceElement reference = importStatement.getImportReference();
if (reference != null) {
return Collections.singleton(reference.getText());
}
return Collections.emptyList();
}
if (ref.getQualifier() != null)
return Collections.emptyList();
final String className = ref.getReferenceName();
if (className == null)
return Collections.emptyList();
PsiFile file = ref.getContainingFile().getOriginalFile();
if (!(file instanceof GroovyFile))
return Collections.emptyList();
GrImportStatement[] importList = ((GroovyFile) file).getImportStatements();
for (GrImportStatement imp : importList) {
if (className.equals(imp.getImportedName())) {
GrCodeReferenceElement importReference = imp.getImportReference();
if (importReference == null)
return Collections.emptyList();
return Collections.singleton(importReference.getText());
}
}
return Collections.emptyList();
}
use of org.jetbrains.plugins.groovy.lang.psi.GroovyFile in project intellij-community by JetBrains.
the class StaticImportInsertHandler method handleInsert.
@Override
public void handleInsert(InsertionContext context, JavaGlobalMemberLookupElement item) {
GroovyInsertHandler.INSTANCE.handleInsert(context, item);
final PsiMember member = item.getObject();
PsiDocumentManager.getInstance(member.getProject()).commitDocument(context.getDocument());
final GrReferenceExpression ref = PsiTreeUtil.findElementOfClassAtOffset(context.getFile(), context.getStartOffset(), GrReferenceExpression.class, false);
if (ref != null && ref.getQualifier() == null && context.getFile() instanceof GroovyFile && !importAlreadyExists(member, ((GroovyFile) context.getFile()), ref) && !PsiManager.getInstance(context.getProject()).areElementsEquivalent(ref.resolve(), member)) {
ref.bindToElementViaStaticImport(member);
}
}
use of org.jetbrains.plugins.groovy.lang.psi.GroovyFile 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.GroovyFile in project intellij-community by JetBrains.
the class BaseScriptAnnotationChecker method checkApplicability.
@Override
public boolean checkApplicability(@NotNull AnnotationHolder holder, @NotNull GrAnnotation annotation) {
if (GroovyCommonClassNames.GROOVY_TRANSFORM_BASE_SCRIPT.equals(annotation.getQualifiedName())) {
PsiFile file = annotation.getContainingFile();
if (file instanceof GroovyFile && !(((GroovyFile) file).isScript())) {
holder.createErrorAnnotation(annotation, GroovyBundle.message("base.script.annotation.is.allowed.only.inside.scripts"));
return true;
}
PsiElement pparent = annotation.getParent().getParent();
if (pparent instanceof GrVariableDeclaration) {
GrTypeElement typeElement = ((GrVariableDeclaration) pparent).getTypeElementGroovy();
PsiType type = typeElement != null ? typeElement.getType() : null;
if (!InheritanceUtil.isInheritor(type, GroovyCommonClassNames.GROOVY_LANG_SCRIPT)) {
String typeText = type != null ? type.getCanonicalText() : CommonClassNames.JAVA_LANG_OBJECT;
holder.createErrorAnnotation(annotation, GroovyBundle.message("declared.type.0.have.to.extend.script", typeText));
return true;
}
}
}
return false;
}
use of org.jetbrains.plugins.groovy.lang.psi.GroovyFile in project intellij-community by JetBrains.
the class GroovyFileIconProvider method getIcon.
@Nullable
@Override
public Icon getIcon(@NotNull VirtualFile virtualFile, @Iconable.IconFlags int flags, @Nullable Project project) {
if (project == null || virtualFile.getFileType() != GroovyFileType.GROOVY_FILE_TYPE)
return null;
final PsiFile psiFile = PsiManager.getInstance(project).findFile(virtualFile);
if (!(psiFile instanceof GroovyFile))
return null;
final GroovyFile file = (GroovyFile) psiFile;
final Icon icon;
if (file.isScript()) {
icon = GroovyScriptTypeDetector.getIcon(file);
} else if (GrFileIndexUtil.isGroovySourceFile(file)) {
final GrTypeDefinition[] typeDefinitions = file.getTypeDefinitions();
icon = typeDefinitions.length > 0 ? typeDefinitions[0].getIcon(flags) : JetgroovyIcons.Groovy.GroovyFile;
} else {
icon = JetgroovyIcons.Groovy.Groovy_outsideSources;
}
return ElementBase.createLayeredIcon(psiFile, icon, ElementBase.transformFlags(psiFile, flags));
}
Aggregations