Search in sources :

Example 1 with GrTypeElement

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

the class DynamicDialog method doOKAction.

@Override
protected void doOKAction() {
    super.doOKAction();
    mySettings.setContainingClassName((String) myClassComboBox.getSelectedItem());
    mySettings.setStatic(myStaticCheckBox.isSelected());
    GrTypeElement typeElement = getEnteredTypeName();
    if (typeElement == null) {
        mySettings.setType(CommonClassNames.JAVA_LANG_OBJECT);
    } else {
        PsiType type = typeElement.getType();
        if (type instanceof PsiPrimitiveType) {
            type = TypesUtil.boxPrimitiveType(type, typeElement.getManager(), ProjectScope.getAllScope(myProject));
        }
        final String typeQualifiedName = type.getCanonicalText();
        if (typeQualifiedName != null) {
            mySettings.setType(typeQualifiedName);
        } else {
            mySettings.setType(type.getPresentableText());
        }
    }
    final Document document = PsiDocumentManager.getInstance(myProject).getDocument(myContext.getContainingFile());
    CommandProcessor.getInstance().executeCommand(myProject, () -> {
        UndoManager.getInstance(myProject).undoableActionPerformed(new GlobalUndoableAction(document) {

            @Override
            public void undo() throws UnexpectedUndoException {
                final DItemElement itemElement;
                if (mySettings.isMethod()) {
                    final List<ParamInfo> myPairList = mySettings.getParams();
                    final String[] argumentsTypes = QuickfixUtil.getArgumentsTypes(myPairList);
                    itemElement = myDynamicManager.findConcreteDynamicMethod(mySettings.getContainingClassName(), mySettings.getName(), argumentsTypes);
                } else {
                    itemElement = myDynamicManager.findConcreteDynamicProperty(mySettings.getContainingClassName(), mySettings.getName());
                }
                if (itemElement == null) {
                    Messages.showWarningDialog(myProject, GroovyInspectionBundle.message("Cannot.perform.undo.operation"), GroovyInspectionBundle.message("Undo.disable"));
                    return;
                }
                final DClassElement classElement = myDynamicManager.getClassElementByItem(itemElement);
                if (classElement == null) {
                    Messages.showWarningDialog(myProject, GroovyInspectionBundle.message("Cannot.perform.undo.operation"), GroovyInspectionBundle.message("Undo.disable"));
                    return;
                }
                removeElement(itemElement);
                if (classElement.getMethods().isEmpty() && classElement.getProperties().isEmpty()) {
                    myDynamicManager.removeClassElement(classElement);
                }
            }

            @Override
            public void redo() throws UnexpectedUndoException {
                addElement(mySettings);
            }
        });
        addElement(mySettings);
    }, "Add dynamic element", null);
}
Also used : GrTypeElement(org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeElement) DClassElement(org.jetbrains.plugins.groovy.annotator.intentions.dynamic.elements.DClassElement) UnexpectedUndoException(com.intellij.openapi.command.undo.UnexpectedUndoException) GlobalUndoableAction(com.intellij.openapi.command.undo.GlobalUndoableAction) List(java.util.List) Document(com.intellij.openapi.editor.Document) DItemElement(org.jetbrains.plugins.groovy.annotator.intentions.dynamic.elements.DItemElement)

Example 2 with GrTypeElement

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

the class DynamicDialog method doValidate.

@Override
protected ValidationInfo doValidate() {
    final GrTypeElement typeElement = getEnteredTypeName();
    if (typeElement == null) {
        return new ValidationInfo(GroovyInspectionBundle.message("no.type.specified"), myTypeComboBox);
    }
    final PsiType type = typeElement.getType();
    if (type instanceof PsiClassType && ((PsiClassType) type).resolve() == null) {
        return new ValidationInfo(GroovyInspectionBundle.message("unresolved.type.status", type.getPresentableText()), myTypeComboBox);
    }
    return null;
}
Also used : GrTypeElement(org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeElement) ValidationInfo(com.intellij.openapi.ui.ValidationInfo)

Example 3 with GrTypeElement

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

the class GrChangeSignatureUsageProcessor method processPrimaryMethodInner.

private static boolean processPrimaryMethodInner(JavaChangeInfo changeInfo, GrMethod method, @Nullable PsiMethod baseMethod) {
    if (changeInfo.isNameChanged()) {
        String newName = baseMethod == null ? changeInfo.getNewName() : RefactoringUtil.suggestNewOverriderName(method.getName(), baseMethod.getName(), changeInfo.getNewName());
        if (newName != null && !newName.equals(method.getName())) {
            method.setName(changeInfo.getNewName());
        }
    }
    final GrModifierList modifierList = method.getModifierList();
    if (changeInfo.isVisibilityChanged()) {
        modifierList.setModifierProperty(changeInfo.getNewVisibility(), true);
    }
    PsiSubstitutor substitutor = baseMethod != null ? calculateSubstitutor(method, baseMethod) : PsiSubstitutor.EMPTY;
    final PsiMethod context = changeInfo.getMethod();
    GrTypeElement oldReturnTypeElement = method.getReturnTypeElementGroovy();
    if (changeInfo.isReturnTypeChanged()) {
        CanonicalTypes.Type newReturnType = changeInfo.getNewReturnType();
        if (newReturnType == null) {
            if (oldReturnTypeElement != null) {
                oldReturnTypeElement.delete();
                if (modifierList.getModifiers().length == 0) {
                    modifierList.setModifierProperty(GrModifier.DEF, true);
                }
            }
        } else {
            PsiType type = newReturnType.getType(context, method.getManager());
            GrReferenceAdjuster.shortenAllReferencesIn(method.setReturnType(substitutor.substitute(type)));
            if (oldReturnTypeElement == null) {
                modifierList.setModifierProperty(GrModifier.DEF, false);
            }
        }
    }
    JavaParameterInfo[] newParameters = changeInfo.getNewParameters();
    final GrParameterList parameterList = method.getParameterList();
    GrParameter[] oldParameters = parameterList.getParameters();
    final PsiParameter[] oldBaseParams = baseMethod != null ? baseMethod.getParameterList().getParameters() : null;
    Set<GrParameter> toRemove = new HashSet<>(oldParameters.length);
    ContainerUtil.addAll(toRemove, oldParameters);
    GrParameter anchor = null;
    final GrDocComment docComment = method.getDocComment();
    final GrDocTag[] tags = docComment == null ? null : docComment.getTags();
    int newParamIndex = 0;
    for (JavaParameterInfo newParameter : newParameters) {
        //if old parameter name differs from base method parameter name we don't change it
        final String newName;
        final int oldIndex = newParameter.getOldIndex();
        if (oldIndex >= 0 && oldBaseParams != null) {
            final String oldName = oldParameters[oldIndex].getName();
            if (oldName.equals(oldBaseParams[oldIndex].getName())) {
                newName = newParameter.getName();
            } else {
                newName = oldName;
            }
        } else {
            newName = newParameter.getName();
        }
        final GrParameter oldParameter = oldIndex >= 0 ? oldParameters[oldIndex] : null;
        if (docComment != null && oldParameter != null) {
            final String oldName = oldParameter.getName();
            for (GrDocTag tag : tags) {
                if ("@param".equals(tag.getName())) {
                    final GrDocParameterReference parameterReference = tag.getDocParameterReference();
                    if (parameterReference != null && oldName.equals(parameterReference.getText())) {
                        parameterReference.handleElementRename(newName);
                    }
                }
            }
        }
        GrParameter grParameter = createNewParameter(substitutor, context, parameterList, newParameter, newName);
        if (oldParameter != null) {
            grParameter.getModifierList().replace(oldParameter.getModifierList());
        }
        if ("def".equals(newParameter.getTypeText())) {
            grParameter.getModifierList().setModifierProperty(GrModifier.DEF, true);
        } else if (StringUtil.isEmpty(newParameter.getTypeText())) {
            grParameter.getModifierList().setModifierProperty(GrModifier.DEF, false);
        }
        anchor = (GrParameter) parameterList.addAfter(grParameter, anchor);
        if (newParamIndex < oldParameters.length) {
            GrParameter oldParam = oldParameters[newParamIndex];
            PsiElement prev = oldParam.getPrevSibling();
            if (prev instanceof PsiWhiteSpace) {
                parameterList.addBefore(prev, anchor);
            }
        }
        newParamIndex++;
    }
    for (GrParameter oldParameter : toRemove) {
        oldParameter.delete();
    }
    JavaCodeStyleManager.getInstance(parameterList.getProject()).shortenClassReferences(parameterList);
    CodeStyleManager.getInstance(parameterList.getProject()).reformat(parameterList);
    if (changeInfo.isExceptionSetOrOrderChanged()) {
        final ThrownExceptionInfo[] infos = changeInfo.getNewExceptions();
        PsiClassType[] exceptionTypes = new PsiClassType[infos.length];
        for (int i = 0; i < infos.length; i++) {
            ThrownExceptionInfo info = infos[i];
            exceptionTypes[i] = (PsiClassType) info.createType(method, method.getManager());
        }
        PsiReferenceList thrownList = GroovyPsiElementFactory.getInstance(method.getProject()).createThrownList(exceptionTypes);
        thrownList = (PsiReferenceList) method.getThrowsList().replace(thrownList);
        JavaCodeStyleManager.getInstance(thrownList.getProject()).shortenClassReferences(thrownList);
        CodeStyleManager.getInstance(method.getProject()).reformat(method.getThrowsList());
    }
    return true;
}
Also used : GrParameterList(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameterList) GrTypeElement(org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeElement) CanonicalTypes(com.intellij.refactoring.util.CanonicalTypes) GrDocParameterReference(org.jetbrains.plugins.groovy.lang.groovydoc.psi.api.GrDocParameterReference) GrParameter(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter) GrDocComment(org.jetbrains.plugins.groovy.lang.groovydoc.psi.api.GrDocComment) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) HashSet(com.intellij.util.containers.HashSet) GrModifierList(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.GrModifierList) GrDocTag(org.jetbrains.plugins.groovy.lang.groovydoc.psi.api.GrDocTag)

Example 4 with GrTypeElement

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

the class ExpressionGenerator method visitInstanceofExpression.

@Override
public void visitInstanceofExpression(@NotNull GrInstanceOfExpression expression) {
    final GrExpression operand = expression.getOperand();
    final GrTypeElement typeElement = expression.getTypeElement();
    writeInstanceof(operand, typeElement != null ? typeElement.getType() : null, expression);
}
Also used : GrTypeElement(org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeElement)

Example 5 with GrTypeElement

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

the class ExpressionGenerator method visitSafeCastExpression.

@Override
public void visitSafeCastExpression(@NotNull GrSafeCastExpression typeCastExpression) {
    final GrExpression operand = (GrExpression) PsiUtil.skipParenthesesIfSensibly(typeCastExpression.getOperand(), false);
    final GrTypeElement typeElement = typeCastExpression.getCastTypeElement();
    if (operand instanceof GrListOrMap && ((GrListOrMap) operand).isMap() && typeElement != null) {
        AnonymousFromMapGenerator.writeAnonymousMap((GrListOrMap) operand, typeElement, builder, context);
        return;
    }
    final PsiType type = typeElement.getType();
    if (operand instanceof GrListOrMap && !((GrListOrMap) operand).isMap() && type instanceof PsiArrayType) {
        builder.append("new ");
        final GrExpression[] initializers = ((GrListOrMap) operand).getInitializers();
        if (initializers.length == 0) {
            TypeWriter.writeTypeForNew(builder, ((PsiArrayType) type).getComponentType(), typeCastExpression);
            builder.append("[0]");
        } else {
            TypeWriter.writeTypeForNew(builder, type, typeCastExpression);
            builder.append('{');
            for (GrExpression initializer : initializers) {
                initializer.accept(this);
                builder.append(", ");
            }
            if (initializers.length > 0) {
                builder.delete(builder.length() - 2, builder.length());
            //builder.removeFromTheEnd(2);
            }
            builder.append('}');
        }
        return;
    }
    final GroovyResolveResult resolveResult = PsiImplUtil.extractUniqueResult(typeCastExpression.multiResolve(false));
    final PsiElement resolved = resolveResult.getElement();
    if (resolved instanceof PsiMethod) {
        final GrExpression typeParam;
        try {
            typeParam = factory.createExpressionFromText(typeElement.getText(), typeCastExpression);
        } catch (IncorrectOperationException e) {
            generateCast(typeElement, operand);
            return;
        }
        invokeMethodOn(((PsiMethod) resolved), operand, new GrExpression[] { typeParam }, GrNamedArgument.EMPTY_ARRAY, GrClosableBlock.EMPTY_ARRAY, resolveResult.getSubstitutor(), typeCastExpression);
    } else {
        generateCast(typeElement, operand);
    }
}
Also used : GrTypeElement(org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeElement) GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) IncorrectOperationException(com.intellij.util.IncorrectOperationException) GrListOrMap(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrListOrMap) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)

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