Search in sources :

Example 6 with GrTypeElement

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

the class GenerationUtil method getVarTypes.

static Set<String> getVarTypes(GrVariableDeclaration variableDeclaration) {
    GrVariable[] variables = variableDeclaration.getVariables();
    final GrTypeElement typeElement = variableDeclaration.getTypeElementGroovy();
    Set<String> types = new HashSet<>(variables.length);
    if (typeElement == null) {
        if (variables.length > 1) {
            for (GrVariable variable : variables) {
                final GrExpression initializer = variable.getInitializerGroovy();
                if (initializer != null) {
                    final PsiType varType = initializer.getType();
                    if (varType != null) {
                        types.add(getTypeText(varType, variableDeclaration));
                    }
                }
            }
        }
    }
    return types;
}
Also used : GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) GrTypeElement(org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeElement) HashSet(com.intellij.util.containers.hash.HashSet)

Example 7 with GrTypeElement

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

the class StubGenerator method writeVariableDeclarations.

@Override
public void writeVariableDeclarations(StringBuilder text, GrVariableDeclaration variableDeclaration) {
    GrTypeElement typeElement = variableDeclaration.getTypeElementGroovy();
    final GrModifierList modifierList = variableDeclaration.getModifierList();
    final PsiNameHelper nameHelper = PsiNameHelper.getInstance(variableDeclaration.getProject());
    for (final GrVariable variable : variableDeclaration.getVariables()) {
        String name = variable.getName();
        if (!nameHelper.isIdentifier(name)) {
            //does not have a java image
            continue;
        }
        ModifierListGenerator.writeModifiers(text, modifierList, STUB_FIELD_MODIFIERS, false);
        //type
        PsiType declaredType = typeElement == null ? PsiType.getJavaLangObject(variable.getManager(), variable.getResolveScope()) : typeElement.getType();
        TypeWriter.writeType(text, declaredType, variableDeclaration, classNameProvider);
        text.append(' ').append(name).append(" = ").append(getVariableInitializer(variable, declaredType));
        text.append(";\n");
    }
}
Also used : GrModifierList(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.GrModifierList) GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) GrTypeElement(org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeElement)

Example 8 with GrTypeElement

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

the class AddReturnTypeFix method findMethod.

@Nullable
private static GrMethod findMethod(PsiFile file, final int offset) {
    final PsiElement at = file.findElementAt(offset);
    if (at == null)
        return null;
    if (at.getParent() instanceof GrReturnStatement) {
        final GrReturnStatement returnStatement = ((GrReturnStatement) at.getParent());
        final PsiElement word = returnStatement.getReturnWord();
        if (!word.getTextRange().contains(offset))
            return null;
        final GroovyPsiElement returnOwner = PsiTreeUtil.getParentOfType(returnStatement, GrClosableBlock.class, GrMethod.class);
        if (returnOwner instanceof GrMethod) {
            final GrTypeElement returnTypeElement = ((GrMethod) returnOwner).getReturnTypeElementGroovy();
            if (returnTypeElement == null) {
                return (GrMethod) returnOwner;
            }
        }
        return null;
    }
    final GrMethod method = PsiTreeUtil.getParentOfType(at, GrMethod.class, false, GrTypeDefinition.class, GrClosableBlock.class);
    if (method != null && GrHighlightUtil.getMethodHeaderTextRange(method).contains(offset)) {
        if (method.getReturnTypeElementGroovy() == null) {
            return method;
        }
    }
    return null;
}
Also used : GrTypeElement(org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeElement) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) GrReturnStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.branch.GrReturnStatement) PsiElement(com.intellij.psi.PsiElement) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) Nullable(org.jetbrains.annotations.Nullable)

Example 9 with GrTypeElement

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

the class ConvertSimpleGetterToPropertyIntention method processIntention.

@Override
protected void processIntention(@NotNull PsiElement element, @NotNull Project project, Editor editor) throws IncorrectOperationException {
    GrMethod method = (GrMethod) element.getParent();
    GrOpenBlock block = method.getBlock();
    if (block == null)
        return;
    GrStatement statement = block.getStatements()[0];
    GrExpression value;
    if (statement instanceof GrReturnStatement) {
        value = ((GrReturnStatement) statement).getReturnValue();
    } else {
        value = (GrExpression) statement;
    }
    String fieldName = GroovyPropertyUtils.getPropertyNameByGetter(method);
    if (fieldName == null)
        return;
    PsiClass aClass = method.getContainingClass();
    if (aClass == null)
        return;
    List<String> modifiers = ContainerUtil.newArrayList();
    for (String modifier : MODIFIERS_TO_CHECK) {
        if (method.hasModifierProperty(modifier))
            modifiers.add(modifier);
    }
    modifiers.add(PsiModifier.FINAL);
    GrTypeElement returnTypeElement = method.getReturnTypeElementGroovy();
    PsiType returnType = returnTypeElement == null ? null : returnTypeElement.getType();
    GrVariableDeclaration declaration = GroovyPsiElementFactory.getInstance(project).createFieldDeclaration(ArrayUtil.toStringArray(modifiers), fieldName, value, returnType);
    PsiElement replaced = method.replace(declaration);
    JavaCodeStyleManager.getInstance(project).shortenClassReferences(replaced);
}
Also used : GrTypeElement(org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeElement) GrVariableDeclaration(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration) GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) PsiClass(com.intellij.psi.PsiClass) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GrOpenBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock) GrReturnStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.branch.GrReturnStatement) PsiElement(com.intellij.psi.PsiElement) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement) PsiType(com.intellij.psi.PsiType)

Example 10 with GrTypeElement

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

the class GrFinalListener method perform.

public void perform(final boolean generateFinal, final String modifier, final GrVariable variable) {
    final Document document = myEditor.getDocument();
    LOG.assertTrue(variable != null);
    final GrModifierList modifierList = variable.getModifierList();
    LOG.assertTrue(modifierList != null);
    final int textOffset = modifierList.getTextOffset();
    final Runnable runnable = () -> {
        if (generateFinal) {
            final GrTypeElement typeElement = variable.getTypeElementGroovy();
            final int typeOffset = typeElement != null ? typeElement.getTextOffset() : textOffset;
            document.insertString(typeOffset, modifier + " ");
        } else {
            final int idx = modifierList.getText().indexOf(modifier);
            document.deleteString(textOffset + idx, textOffset + idx + modifier.length() + 1);
        }
    };
    final LookupImpl lookup = (LookupImpl) LookupManager.getActiveLookup(myEditor);
    if (lookup != null) {
        lookup.performGuardedChange(runnable);
    } else {
        runnable.run();
    }
    PsiDocumentManager.getInstance(variable.getProject()).commitDocument(document);
}
Also used : GrModifierList(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.GrModifierList) GrTypeElement(org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeElement) LookupImpl(com.intellij.codeInsight.lookup.impl.LookupImpl) Document(com.intellij.openapi.editor.Document)

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