Search in sources :

Example 46 with GroovyFile

use of org.jetbrains.plugins.groovy.lang.psi.GroovyFile in project intellij-community by JetBrains.

the class GroovyShortNamesCache method getScriptClassesByFQName.

public List<PsiClass> getScriptClassesByFQName(final String name, final GlobalSearchScope scope, final boolean srcOnly) {
    GlobalSearchScope actualScope = srcOnly ? new GrSourceFilterScope(scope) : scope;
    final Collection<GroovyFile> files = StubIndex.getElements(GrFullScriptNameIndex.KEY, name.hashCode(), myProject, actualScope, GroovyFile.class);
    if (files.isEmpty()) {
        return Collections.emptyList();
    }
    final ArrayList<PsiClass> result = new ArrayList<>();
    for (GroovyFile file : files) {
        if (file.isScript()) {
            final PsiClass scriptClass = file.getScriptClass();
            if (scriptClass != null && name.equals(scriptClass.getQualifiedName())) {
                result.add(scriptClass);
            }
        }
    }
    return result;
}
Also used : GrSourceFilterScope(org.jetbrains.plugins.groovy.lang.psi.impl.search.GrSourceFilterScope) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) ArrayList(java.util.ArrayList) PsiClass(com.intellij.psi.PsiClass) GroovyFile(org.jetbrains.plugins.groovy.lang.psi.GroovyFile)

Example 47 with GroovyFile

use of org.jetbrains.plugins.groovy.lang.psi.GroovyFile in project intellij-community by JetBrains.

the class ResolveUtil method isScriptField.

public static boolean isScriptField(GrVariable var) {
    GrModifierList list = var.getModifierList();
    if (list == null)
        return false;
    PsiFile containingFile = var.getContainingFile();
    if (!(containingFile instanceof GroovyFile) || !((GroovyFile) containingFile).isScript())
        return false;
    GrMember member = PsiTreeUtil.getParentOfType(var, GrTypeDefinition.class, GrMethod.class);
    if (member != null)
        return false;
    for (GrAnnotation annotation : list.getAnnotations()) {
        String qualifiedName = annotation.getQualifiedName();
        if (qualifiedName == null)
            continue;
        if (qualifiedName.equals(GroovyCommonClassNames.GROOVY_TRANSFORM_FIELD))
            return true;
    }
    return false;
}
Also used : GrModifierList(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.GrModifierList) GrMember(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMember) GrAnnotation(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.annotation.GrAnnotation) GroovyFile(org.jetbrains.plugins.groovy.lang.psi.GroovyFile)

Example 48 with GroovyFile

use of org.jetbrains.plugins.groovy.lang.psi.GroovyFile in project intellij-community by JetBrains.

the class ResolveUtil method findDuplicate.

public static PsiNamedElement findDuplicate(@NotNull GrVariable variable) {
    if (isScriptField(variable)) {
        final String name = variable.getName();
        final GroovyScriptClass script = (GroovyScriptClass) ((GroovyFile) variable.getContainingFile()).getScriptClass();
        assert script != null;
        List<GrField> duplicates = ContainerUtil.filter(script.getFields(), (GrField f) -> {
            if (!(f instanceof GrScriptField))
                return false;
            if (!name.equals(f.getName()))
                return false;
            if (((GrScriptField) f).getOriginalVariable() == variable)
                return false;
            return true;
        });
        return duplicates.size() > 0 ? duplicates.get(0) : null;
    } else {
        PsiNamedElement duplicate = resolveExistingElement(variable, new DuplicateVariablesProcessor(variable), GrVariable.class);
        final PsiElement context1 = variable.getContext();
        if (duplicate == null && variable instanceof GrParameter && context1 != null) {
            final PsiElement context = context1.getContext();
            if (context instanceof GrClosableBlock || context instanceof GrMethod && !(context.getParent() instanceof GroovyFile) || context instanceof GrTryCatchStatement) {
                duplicate = resolveExistingElement(context.getParent(), new DuplicateVariablesProcessor(variable), GrVariable.class);
            }
        }
        if (duplicate instanceof GrLightParameter && "args".equals(duplicate.getName())) {
            return null;
        } else {
            return duplicate;
        }
    }
}
Also used : GrLightParameter(org.jetbrains.plugins.groovy.lang.psi.impl.synthetic.GrLightParameter) GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) GrParameter(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter) GrScriptField(org.jetbrains.plugins.groovy.lang.psi.impl.synthetic.GrScriptField) GroovyScriptClass(org.jetbrains.plugins.groovy.lang.psi.impl.synthetic.GroovyScriptClass) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) GroovyFile(org.jetbrains.plugins.groovy.lang.psi.GroovyFile)

Example 49 with GroovyFile

use of org.jetbrains.plugins.groovy.lang.psi.GroovyFile in project intellij-community by JetBrains.

the class NewGroovyClassAction method doCreate.

@Override
protected final GrTypeDefinition doCreate(PsiDirectory dir, String className, String templateName) throws IncorrectOperationException {
    final String fileName = className + NewGroovyActionBase.GROOVY_EXTENSION;
    final PsiFile fromTemplate = GroovyTemplatesFactory.createFromTemplate(dir, className, fileName, templateName, true);
    if (fromTemplate instanceof GroovyFile) {
        CodeStyleManager.getInstance(fromTemplate.getManager()).reformat(fromTemplate);
        return ((GroovyFile) fromTemplate).getTypeDefinitions()[0];
    }
    final String description = fromTemplate.getFileType().getDescription();
    throw new IncorrectOperationException(GroovyBundle.message("groovy.file.extension.is.not.mapped.to.groovy.file.type", description));
}
Also used : PsiFile(com.intellij.psi.PsiFile) IncorrectOperationException(com.intellij.util.IncorrectOperationException) GroovyFile(org.jetbrains.plugins.groovy.lang.psi.GroovyFile)

Example 50 with GroovyFile

use of org.jetbrains.plugins.groovy.lang.psi.GroovyFile in project android by JetBrains.

the class GradleDslFile method parse.

public void parse() {
    ApplicationManager.getApplication().assertReadAccessAllowed();
    PsiFile psiFile = PsiManager.getInstance(myProject).findFile(myFile);
    GroovyFile myPsiFile = null;
    if (psiFile instanceof GroovyFile) {
        myPsiFile = (GroovyFile) psiFile;
    }
    if (myPsiFile == null) {
        return;
    }
    setPsiElement(myPsiFile);
    parse(myPsiFile);
}
Also used : PsiFile(com.intellij.psi.PsiFile) GroovyFile(org.jetbrains.plugins.groovy.lang.psi.GroovyFile)

Aggregations

GroovyFile (org.jetbrains.plugins.groovy.lang.psi.GroovyFile)91 PsiFile (com.intellij.psi.PsiFile)26 PsiElement (com.intellij.psi.PsiElement)21 NotNull (org.jetbrains.annotations.NotNull)17 GrImportStatement (org.jetbrains.plugins.groovy.lang.psi.api.toplevel.imports.GrImportStatement)17 VirtualFile (com.intellij.openapi.vfs.VirtualFile)13 Project (com.intellij.openapi.project.Project)10 GrTypeDefinition (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition)10 PsiClass (com.intellij.psi.PsiClass)9 GrClosableBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock)9 Nullable (org.jetbrains.annotations.Nullable)8 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)8 GroovyScriptClass (org.jetbrains.plugins.groovy.lang.psi.impl.synthetic.GroovyScriptClass)8 GroovyPsiElementFactory (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)7 Module (com.intellij.openapi.module.Module)6 IncorrectOperationException (com.intellij.util.IncorrectOperationException)6 GrReferenceElement (org.jetbrains.plugins.groovy.lang.psi.GrReferenceElement)6 GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)6 GrParameter (org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter)6 ASTNode (com.intellij.lang.ASTNode)5