Search in sources :

Example 16 with GroovyScriptClass

use of org.jetbrains.plugins.groovy.lang.psi.impl.synthetic.GroovyScriptClass 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 17 with GroovyScriptClass

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

the class BaseScriptTransformationSupport method applyTransformation.

@Override
public void applyTransformation(@NotNull TransformationContext context) {
    if (!(context.getCodeClass() instanceof GroovyScriptClass))
        return;
    GroovyScriptClass scriptClass = (GroovyScriptClass) context.getCodeClass();
    LightMethodBuilder mainMethod = new LightMethodBuilder(scriptClass.getManager(), GroovyLanguage.INSTANCE, "main").setMethodReturnType(PsiType.VOID).addParameter("args", new PsiArrayType(PsiType.getJavaLangString(scriptClass.getManager(), scriptClass.getResolveScope()))).addModifiers(PsiModifier.PUBLIC, PsiModifier.STATIC);
    LightMethodBuilder runMethod = new LightMethodBuilder(scriptClass.getManager(), GroovyLanguage.INSTANCE, "run").setMethodReturnType(TypesUtil.getJavaLangObject(scriptClass)).addModifier(PsiModifier.PUBLIC);
    context.addMethod(runMethod, true);
    context.addMethod(mainMethod, true);
    context.setSuperType(getBaseClassType(scriptClass));
}
Also used : GroovyScriptClass(org.jetbrains.plugins.groovy.lang.psi.impl.synthetic.GroovyScriptClass) LightMethodBuilder(com.intellij.psi.impl.light.LightMethodBuilder) PsiArrayType(com.intellij.psi.PsiArrayType)

Example 18 with GroovyScriptClass

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

the class GroovyScriptRunConfigurationProducer method createConfigurationByElement.

@Override
protected RunnerAndConfigurationSettings createConfigurationByElement(final Location location, final ConfigurationContext context) {
    final PsiElement element = location.getPsiElement();
    final PsiFile file = element.getContainingFile();
    if (!(file instanceof GroovyFile)) {
        return null;
    }
    GroovyFile groovyFile = (GroovyFile) file;
    final PsiClass aClass = GroovyRunnerPsiUtil.getRunningClass(location.getPsiElement());
    if (aClass instanceof GroovyScriptClass || GroovyRunnerPsiUtil.isRunnable(aClass)) {
        final RunnerAndConfigurationSettings settings = createConfiguration(aClass);
        if (settings != null) {
            mySourceElement = element;
            final GroovyScriptRunConfiguration configuration = (GroovyScriptRunConfiguration) settings.getConfiguration();
            GroovyScriptUtil.getScriptType(groovyFile).tuneConfiguration(groovyFile, configuration, location);
            return settings;
        }
    }
    if (file.getText().contains("@Grab")) {
        ApplicationConfigurationProducer producer = new ApplicationConfigurationProducer();
        ConfigurationFromContext settings = producer.createConfigurationFromContext(context);
        if (settings != null) {
            PsiElement src = settings.getSourceElement();
            mySourceElement = src;
            return createConfiguration(src instanceof PsiMethod ? ((PsiMethod) src).getContainingClass() : (PsiClass) src);
        }
        return null;
    } else {
        return null;
    }
}
Also used : GroovyScriptClass(org.jetbrains.plugins.groovy.lang.psi.impl.synthetic.GroovyScriptClass) ApplicationConfigurationProducer(com.intellij.execution.application.ApplicationConfigurationProducer) ConfigurationFromContext(com.intellij.execution.actions.ConfigurationFromContext) RunnerAndConfigurationSettings(com.intellij.execution.RunnerAndConfigurationSettings) GroovyFile(org.jetbrains.plugins.groovy.lang.psi.GroovyFile)

Example 19 with GroovyScriptClass

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

the class ReachingDefinitionsCollector method filterNonlocals.

private static VariableInfo[] filterNonlocals(Map<String, VariableInfo> infos, GrStatement place) {
    List<VariableInfo> result = new ArrayList<>();
    for (Iterator<VariableInfo> iterator = infos.values().iterator(); iterator.hasNext(); ) {
        VariableInfo info = iterator.next();
        String name = info.getName();
        GroovyPsiElement property = ResolveUtil.resolveProperty(place, name);
        if (property instanceof GrVariable) {
            iterator.remove();
        } else if (property instanceof GrReferenceExpression) {
            GrMember member = PsiTreeUtil.getParentOfType(property, GrMember.class);
            if (member == null) {
                continue;
            } else if (!member.hasModifierProperty(PsiModifier.STATIC)) {
                if (member.getContainingClass() instanceof GroovyScriptClass) {
                    //binding variable
                    continue;
                }
            }
        }
        if (ResolveUtil.resolveClass(place, name) == null) {
            result.add(info);
        }
    }
    return result.toArray(new VariableInfo[result.size()]);
}
Also used : GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) GrMember(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMember) GroovyScriptClass(org.jetbrains.plugins.groovy.lang.psi.impl.synthetic.GroovyScriptClass) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)

Example 20 with GroovyScriptClass

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

the class GroovyFileImpl method getScriptClass.

@Override
public GroovyScriptClass getScriptClass() {
    if (!isScript()) {
        return null;
    }
    GroovyScriptClass aClass = myScriptClass;
    if (aClass == null) {
        aClass = new GroovyScriptClass(this);
        myScriptClass = aClass;
    }
    return aClass;
}
Also used : GroovyScriptClass(org.jetbrains.plugins.groovy.lang.psi.impl.synthetic.GroovyScriptClass)

Aggregations

GroovyScriptClass (org.jetbrains.plugins.groovy.lang.psi.impl.synthetic.GroovyScriptClass)26 GroovyFile (org.jetbrains.plugins.groovy.lang.psi.GroovyFile)8 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)6 PsiElement (com.intellij.psi.PsiElement)4 NotNull (org.jetbrains.annotations.NotNull)4 GroovyPsiElementFactory (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)3 GrClosableBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock)3 GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)3 GrTypeDefinition (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition)3 GrMember (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMember)3 GrMethod (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod)3 GrImportStatement (org.jetbrains.plugins.groovy.lang.psi.api.toplevel.imports.GrImportStatement)3 PsiClass (com.intellij.psi.PsiClass)2 LightMethodBuilder (com.intellij.psi.impl.light.LightMethodBuilder)2 GrDocComment (org.jetbrains.plugins.groovy.lang.groovydoc.psi.api.GrDocComment)2 GrVariable (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable)2 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)2 GrPackageDefinition (org.jetbrains.plugins.groovy.lang.psi.api.toplevel.packaging.GrPackageDefinition)2 Template (com.intellij.codeInsight.template.Template)1 TemplateManager (com.intellij.codeInsight.template.TemplateManager)1