Search in sources :

Example 1 with GrModifierList

use of org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.GrModifierList in project intellij-community by JetBrains.

the class GrSetStrongTypeIntention method getOrCreateTypeElement.

@NotNull
private static TypeInfo getOrCreateTypeElement(@NotNull PsiElement parent, @NotNull PsiElement elementToBuildTemplateOn) {
    GrModifierList modifierList = getModifierList(parent);
    if (modifierList != null && modifierList.hasModifierProperty(GrModifier.DEF) && modifierList.getModifiers().length == 1) {
        PsiElement modifier = modifierList.getModifier(GrModifier.DEF);
        LOG.assertTrue(modifier != null);
        int modifierOffset = modifier.getTextRange().getEndOffset() - elementToBuildTemplateOn.getTextRange().getStartOffset();
        return new TypeInfo(modifier, modifierOffset);
    } else {
        int nameElementOffset;
        final PsiClassType typeToUse = TypesUtil.createType("Abc", parent);
        if (elementToBuildTemplateOn instanceof GrVariableDeclaration) {
            GrVariableDeclaration decl = (GrVariableDeclaration) elementToBuildTemplateOn;
            decl.setType(typeToUse);
            nameElementOffset = decl.getModifierList().getTextRange().getEndOffset() - elementToBuildTemplateOn.getTextRange().getStartOffset();
        } else {
            GrVariable var = (GrVariable) parent;
            var.setType(typeToUse);
            nameElementOffset = var.getNameIdentifierGroovy().getTextRange().getStartOffset() - elementToBuildTemplateOn.getTextRange().getStartOffset();
        }
        return new TypeInfo(getTypeElement(parent), nameElementOffset);
    }
}
Also used : GrModifierList(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.GrModifierList) GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) PsiClassType(com.intellij.psi.PsiClassType) GrVariableDeclaration(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration) PsiElement(com.intellij.psi.PsiElement) SupertypeConstraint(org.jetbrains.plugins.groovy.lang.psi.expectedTypes.SupertypeConstraint) TypeConstraint(org.jetbrains.plugins.groovy.lang.psi.expectedTypes.TypeConstraint) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with GrModifierList

use of org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.GrModifierList in project intellij-community by JetBrains.

the class GrInplaceVariableIntroducer method getTypeELementOrDef.

@Nullable
private static PsiElement getTypeELementOrDef(@NotNull GrVariable variable) {
    GrTypeElement typeElement = variable.getTypeElementGroovy();
    if (typeElement != null)
        return typeElement;
    GrModifierList modifierList = variable.getModifierList();
    if (modifierList != null)
        return modifierList.getModifier(GrModifier.DEF);
    return null;
}
Also used : GrModifierList(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.GrModifierList) GrTypeElement(org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeElement) Nullable(org.jetbrains.annotations.Nullable)

Example 3 with GrModifierList

use of org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.GrModifierList in project intellij-community by JetBrains.

the class GroovyOverrideImplementUtil method setupAnnotations.

private static void setupAnnotations(@NotNull GrTypeDefinition aClass, @NotNull PsiMethod method, @NotNull GrMethod result) {
    if (OverrideImplementUtil.isInsertOverride(method, aClass)) {
        result.getModifierList().addAnnotation(CommonClassNames.JAVA_LANG_OVERRIDE);
    }
    final GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(method.getProject());
    final PsiParameter[] originalParams = method.getParameterList().getParameters();
    GrParameter[] parameters = result.getParameters();
    for (int i = 0; i < parameters.length; i++) {
        GrParameter parameter = parameters[i];
        PsiParameter original = originalParams[i];
        for (PsiAnnotation annotation : original.getModifierList().getAnnotations()) {
            final GrModifierList modifierList = parameter.getModifierList();
            String qname = annotation.getQualifiedName();
            if (qname != null && modifierList.findAnnotation(qname) == null) {
                if (annotation instanceof GrAnnotation) {
                    modifierList.add(annotation);
                } else {
                    modifierList.add(factory.createAnnotationFromText(annotation.getText()));
                }
            }
        }
    }
}
Also used : GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) GrModifierList(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.GrModifierList) GrAnnotation(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.annotation.GrAnnotation) GrParameter(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter)

Example 4 with GrModifierList

use of org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.GrModifierList 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)

Example 5 with GrModifierList

use of org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.GrModifierList 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)

Aggregations

GrModifierList (org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.GrModifierList)39 GrAnnotation (org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.annotation.GrAnnotation)9 GrMethod (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod)5 GrTypeElement (org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeElement)5 Annotation (com.intellij.lang.annotation.Annotation)4 PsiElement (com.intellij.psi.PsiElement)4 TextRange (com.intellij.openapi.util.TextRange)3 NotNull (org.jetbrains.annotations.NotNull)3 GrModifierFix (org.jetbrains.plugins.groovy.codeInspection.bugs.GrModifierFix)3 GrParameter (org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter)3 CandidateInfo (com.intellij.psi.infos.CandidateInfo)2 Matcher (java.util.regex.Matcher)2 Nullable (org.jetbrains.annotations.Nullable)2 GrDocComment (org.jetbrains.plugins.groovy.lang.groovydoc.psi.api.GrDocComment)2 GroovyFile (org.jetbrains.plugins.groovy.lang.psi.GroovyFile)2 GroovyPsiElementFactory (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)2 GrVariable (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable)2 GrVariableDeclaration (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration)2 GrMember (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMember)2 LookupImpl (com.intellij.codeInsight.lookup.impl.LookupImpl)1