Search in sources :

Example 46 with GrTypeElement

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

the class GroovyPsiElementFactoryImpl method createTypeElement.

@Override
@NotNull
public GrTypeElement createTypeElement(@NotNull String typeText, @Nullable final PsiElement context) throws IncorrectOperationException {
    final GroovyFile file = createGroovyFileChecked("def " + typeText + " someVar", false, context);
    GrTopStatement[] topStatements = file.getTopStatements();
    if (topStatements == null || topStatements.length == 0)
        throw new IncorrectOperationException("can't create type element from:" + typeText);
    GrTopStatement statement = topStatements[0];
    if (!(statement instanceof GrVariableDeclaration))
        throw new IncorrectOperationException("can't create type element from:" + typeText);
    GrVariableDeclaration decl = (GrVariableDeclaration) statement;
    final GrTypeElement element = decl.getTypeElementGroovy();
    if (element == null)
        throw new IncorrectOperationException("can't create type element from:" + typeText);
    return element;
}
Also used : GrTypeElement(org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeElement) IncorrectOperationException(com.intellij.util.IncorrectOperationException) GrTopStatement(org.jetbrains.plugins.groovy.lang.psi.api.toplevel.GrTopStatement) NotNull(org.jetbrains.annotations.NotNull)

Example 47 with GrTypeElement

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

the class PsiImplUtil method isMainMethod.

public static boolean isMainMethod(GrMethod method) {
    if (!method.getName().equals(MAIN_METHOD))
        return false;
    else if (!method.hasModifierProperty(PsiModifier.STATIC))
        return false;
    final GrParameter[] parameters = method.getParameters();
    if (parameters.length == 0)
        return false;
    if (parameters.length == 1 && parameters[0].getTypeElementGroovy() == null)
        return true;
    int args_count = 0;
    int optional_count = 0;
    for (GrParameter p : parameters) {
        final GrTypeElement declaredType = p.getTypeElementGroovy();
        if ((declaredType == null || declaredType.getType().equalsToText(CommonClassNames.JAVA_LANG_STRING + "[]")) && p.getInitializerGroovy() == null) {
            args_count++;
        }
        if (p.getInitializerGroovy() != null)
            optional_count++;
    }
    return optional_count == parameters.length - 1 && args_count == 1;
}
Also used : GrTypeElement(org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeElement) GrParameter(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter)

Example 48 with GrTypeElement

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

the class GrReferenceElementImpl method getTypeArguments.

@Override
@NotNull
public PsiType[] getTypeArguments() {
    final GrTypeArgumentList typeArgsList = getTypeArgumentList();
    if (typeArgsList == null)
        return PsiType.EMPTY_ARRAY;
    final GrTypeElement[] args = typeArgsList.getTypeArgumentElements();
    if (args.length == 0)
        return PsiType.EMPTY_ARRAY;
    PsiType[] result = PsiType.createArray(args.length);
    for (int i = 0; i < result.length; i++) {
        result[i] = args[i].getType();
    }
    return result;
}
Also used : GrTypeElement(org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeElement) GrTypeArgumentList(org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeArgumentList) NotNull(org.jetbrains.annotations.NotNull)

Example 49 with GrTypeElement

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

the class GrSplitDeclarationIntention method createVarDeclaration.

private static GrStatement createVarDeclaration(Project project, GrVariable variable, String modifiers, boolean isTuple) {
    StringBuilder builder = new StringBuilder();
    builder.append(modifiers).append(' ');
    GrTypeElement typeElement = variable.getTypeElementGroovy();
    if (typeElement != null) {
        builder.append(typeElement.getText()).append(' ');
    }
    builder.append(variable.getName());
    GrExpression initializer = variable.getInitializerGroovy();
    if (initializer != null) {
        builder.append('=').append(initializer.getText());
    }
    GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(project);
    GrVariableDeclaration decl = (GrVariableDeclaration) factory.createStatementFromText(builder);
    if (isTuple && (variable.getDeclaredType() != null || decl.getModifierList().getModifiers().length > 1)) {
        decl.getModifierList().setModifierProperty(GrModifier.DEF, false);
    }
    return decl;
}
Also used : GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) GrTypeElement(org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeElement) GrVariableDeclaration(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)

Example 50 with GrTypeElement

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

the class GrConvertTypeCastToSafeCastIntention method processIntention.

@Override
protected void processIntention(@NotNull PsiElement element, @NotNull Project project, Editor editor) throws IncorrectOperationException {
    if (!(element instanceof GrTypeCastExpression))
        return;
    GrExpression operand = ((GrTypeCastExpression) element).getOperand();
    GrTypeElement type = ((GrTypeCastExpression) element).getCastTypeElement();
    if (type == null)
        return;
    if (operand == null)
        return;
    GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(project);
    GrExpression safeCast = factory.createExpressionFromText(operand.getText() + " as " + type.getText());
    ((GrTypeCastExpression) element).replaceWithExpression(safeCast, true);
}
Also used : GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) GrTypeElement(org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeElement) GrTypeCastExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrTypeCastExpression) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)

Aggregations

GrTypeElement (org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeElement)52 Nullable (org.jetbrains.annotations.Nullable)12 PsiType (com.intellij.psi.PsiType)9 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)9 PsiElement (com.intellij.psi.PsiElement)7 GrVariableDeclaration (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration)7 IncorrectOperationException (com.intellij.util.IncorrectOperationException)6 NotNull (org.jetbrains.annotations.NotNull)6 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)6 GrParameter (org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter)6 TextRange (com.intellij.openapi.util.TextRange)5 GroovyPsiElementFactory (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)5 GrModifierList (org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.GrModifierList)5 GrVariable (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable)4 GrTypeCastExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrTypeCastExpression)4 GrMethod (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod)4 Document (com.intellij.openapi.editor.Document)3 GrSafeCastExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrSafeCastExpression)3 Template (com.intellij.codeInsight.template.Template)2 TemplateBuilderImpl (com.intellij.codeInsight.template.TemplateBuilderImpl)2