Search in sources :

Example 71 with GroovyFile

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

the class GroovyOverrideImplementUtil method setupTraitMethodBody.

private static void setupTraitMethodBody(Project project, GrMethod resultMethod, GrTraitMethod traitMethod) {
    PsiClass traitClass = traitMethod.getPrototype().getContainingClass();
    StringBuilder builder = new StringBuilder();
    builder.append("\nreturn ");
    builder.append(traitClass.getQualifiedName());
    builder.append(".super.");
    builder.append(traitMethod.getName());
    builder.append("(");
    GrParameter[] parameters = resultMethod.getParameters();
    for (GrParameter parameter : parameters) {
        builder.append(parameter.getName()).append(",");
    }
    if (parameters.length > 0) {
        builder.replace(builder.length() - 1, builder.length(), ")\n");
    } else {
        builder.append(")\n");
    }
    GroovyFile file = GroovyPsiElementFactory.getInstance(project).createGroovyFile(builder, false, null);
    GrOpenBlock block = resultMethod.getBlock();
    block.getNode().addChildren(file.getFirstChild().getNode(), null, block.getRBrace().getNode());
}
Also used : GrOpenBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock) GrParameter(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter) GroovyFile(org.jetbrains.plugins.groovy.lang.psi.GroovyFile)

Example 72 with GroovyFile

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

the class GroovyOverrideImplementUtil method setupOverridingMethodBody.

private static void setupOverridingMethodBody(Project project, PsiMethod method, GrMethod resultMethod, FileTemplate template, PsiSubstitutor substitutor) {
    final PsiType returnType = substitutor.substitute(getSuperReturnType(method));
    String returnTypeText = "";
    if (returnType != null) {
        returnTypeText = returnType.getPresentableText();
    }
    Properties properties = FileTemplateManager.getInstance(project).getDefaultProperties();
    properties.setProperty(FileTemplate.ATTRIBUTE_RETURN_TYPE, returnTypeText);
    properties.setProperty(FileTemplate.ATTRIBUTE_DEFAULT_RETURN_VALUE, PsiTypesUtil.getDefaultValueOfType(returnType));
    properties.setProperty(FileTemplate.ATTRIBUTE_CALL_SUPER, callSuper(method, resultMethod));
    JavaTemplateUtil.setClassAndMethodNameProperties(properties, method.getContainingClass(), resultMethod);
    try {
        String bodyText = StringUtil.replace(template.getText(properties), ";", "");
        GroovyFile file = GroovyPsiElementFactory.getInstance(project).createGroovyFile("\n " + bodyText + "\n", false, null);
        GrOpenBlock block = resultMethod.getBlock();
        block.getNode().addChildren(file.getFirstChild().getNode(), null, block.getRBrace().getNode());
    } catch (IOException e) {
        LOG.error(e);
    }
}
Also used : GrOpenBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock) IOException(java.io.IOException) Properties(java.util.Properties) GroovyFile(org.jetbrains.plugins.groovy.lang.psi.GroovyFile)

Example 73 with GroovyFile

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

the class ClassNameDiffersFromFileNamePredicate method satisfiedBy.

@Override
public boolean satisfiedBy(PsiElement element) {
    final PsiElement parent = element.getParent();
    if (!(parent instanceof GrTypeDefinition))
        return false;
    if (((GrTypeDefinition) parent).getNameIdentifierGroovy() != element)
        return false;
    final String name = ((GrTypeDefinition) parent).getName();
    if (name == null || name.isEmpty())
        return false;
    if (myClassConsumer != null)
        myClassConsumer.consume(((GrTypeDefinition) parent));
    final PsiFile file = element.getContainingFile();
    if (!(file instanceof GroovyFile))
        return false;
    if (!file.isPhysical())
        return false;
    if (name.equals(FileUtil.getNameWithoutExtension(file.getName())))
        return false;
    if (mySearchForClassInMultiClassFile) {
        return ((GroovyFile) file).getClasses().length > 1;
    } else {
        return !((GroovyFile) file).isScript();
    }
}
Also used : GrTypeDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition) PsiFile(com.intellij.psi.PsiFile) PsiElement(com.intellij.psi.PsiElement) GroovyFile(org.jetbrains.plugins.groovy.lang.psi.GroovyFile)

Example 74 with GroovyFile

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

the class AntTasksProvider method getAntTasks.

public static Set<LightMethodBuilder> getAntTasks(PsiElement place) {
    final PsiFile file = place.getContainingFile();
    if (!(file instanceof GroovyFile)) {
        return Collections.emptySet();
    }
    return CachedValuesManager.getManager(file.getProject()).getCachedValue(file, GANT_METHODS, () -> {
        Map<String, Class> antObjects = getAntObjects((GroovyFile) file);
        final Set<LightMethodBuilder> methods = new HashSet<>();
        final Project project = file.getProject();
        final PsiType closureType = TypesUtil.createType(GroovyCommonClassNames.GROOVY_LANG_CLOSURE, file);
        final PsiClassType stringType = TypesUtil.createType(CommonClassNames.JAVA_LANG_STRING, file);
        for (String name : antObjects.keySet()) {
            methods.add(new AntBuilderMethod(file, name, closureType, antObjects.get(name), stringType));
        }
        return CachedValueProvider.Result.create(methods, PsiModificationTracker.JAVA_STRUCTURE_MODIFICATION_COUNT, ProjectRootManager.getInstance(project));
    }, false);
}
Also used : Project(com.intellij.openapi.project.Project) ReflectedProject(com.intellij.lang.ant.ReflectedProject) LightMethodBuilder(com.intellij.psi.impl.light.LightMethodBuilder) GroovyFile(org.jetbrains.plugins.groovy.lang.psi.GroovyFile)

Example 75 with GroovyFile

use of org.jetbrains.plugins.groovy.lang.psi.GroovyFile 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)

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