Search in sources :

Example 1 with GrDocTag

use of org.jetbrains.plugins.groovy.lang.groovydoc.psi.api.GrDocTag 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 2 with GrDocTag

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

the class GroovyBlock method getChildAttributes.

@Override
@NotNull
public ChildAttributes getChildAttributes(final int newChildIndex) {
    ASTNode astNode = getNode();
    final PsiElement psiParent = astNode.getPsi();
    if (psiParent instanceof GroovyFileBase) {
        return new ChildAttributes(Indent.getNoneIndent(), null);
    }
    if (psiParent instanceof GrSwitchStatement) {
        List<Block> subBlocks = getSubBlocks();
        if (newChildIndex > 0) {
            Block block = subBlocks.get(newChildIndex - 1);
            if (block instanceof GroovyBlock) {
                PsiElement anchorPsi = ((GroovyBlock) block).getNode().getPsi();
                if (anchorPsi instanceof GrCaseSection) {
                    for (GrStatement statement : ((GrCaseSection) anchorPsi).getStatements()) {
                        if (statement instanceof GrBreakStatement || statement instanceof GrContinueStatement || statement instanceof GrReturnStatement || statement instanceof GrThrowStatement) {
                            final Indent indent = GroovyIndentProcessor.getSwitchCaseIndent(myContext.getSettings());
                            return new ChildAttributes(indent, null);
                        }
                    }
                    int indentSize = myContext.getSettings().getIndentOptions().INDENT_SIZE;
                    final int spaces = myContext.getSettings().INDENT_CASE_FROM_SWITCH ? 2 * indentSize : indentSize;
                    return new ChildAttributes(Indent.getSpaceIndent(spaces), null);
                }
            }
        }
    }
    if (psiParent instanceof GrCaseLabel) {
        return new ChildAttributes(GroovyIndentProcessor.getSwitchCaseIndent(getContext().getSettings()), null);
    }
    if (psiParent instanceof GrCaseSection) {
        return getSwitchIndent((GrCaseSection) psiParent, newChildIndex);
    }
    if (TokenSets.BLOCK_SET.contains(astNode.getElementType()) || GroovyElementTypes.SWITCH_STATEMENT.equals(astNode.getElementType())) {
        return new ChildAttributes(Indent.getNormalIndent(), null);
    }
    if (GroovyElementTypes.CASE_SECTION.equals(astNode.getElementType())) {
        return new ChildAttributes(Indent.getNormalIndent(), null);
    }
    if (psiParent instanceof GrBinaryExpression || psiParent instanceof GrConditionalExpression || psiParent instanceof GrCommandArgumentList || psiParent instanceof GrArgumentList || psiParent instanceof GrParameterList || psiParent instanceof GrListOrMap || psiParent instanceof GrAnnotationArgumentList || psiParent instanceof GrVariable || psiParent instanceof GrAssignmentExpression) {
        return new ChildAttributes(Indent.getContinuationWithoutFirstIndent(), null);
    }
    if (psiParent instanceof GrDocComment || psiParent instanceof GrDocTag) {
        return new ChildAttributes(Indent.getSpaceIndent(GroovyIndentProcessor.GDOC_COMMENT_INDENT), null);
    }
    if (psiParent instanceof GrIfStatement || psiParent instanceof GrLoopStatement) {
        return new ChildAttributes(Indent.getNormalIndent(), null);
    }
    if (psiParent instanceof GrLabeledStatement && newChildIndex == 2) {
        final Indent indent = getContext().getGroovySettings().INDENT_LABEL_BLOCKS ? Indent.getLabelIndent() : Indent.getNoneIndent();
        return new ChildAttributes(indent, null);
    }
    return new ChildAttributes(Indent.getNoneIndent(), null);
}
Also used : GrParameterList(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameterList) GrCommandArgumentList(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrCommandArgumentList) GroovyFileBase(org.jetbrains.plugins.groovy.lang.psi.GroovyFileBase) GrListOrMap(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrListOrMap) GrBinaryExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrBinaryExpression) GrDocComment(org.jetbrains.plugins.groovy.lang.groovydoc.psi.api.GrDocComment) GrBreakStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.branch.GrBreakStatement) GrContinueStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.branch.GrContinueStatement) GrAssignmentExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrAssignmentExpression) GrCaseSection(org.jetbrains.plugins.groovy.lang.psi.api.statements.clauses.GrCaseSection) ASTNode(com.intellij.lang.ASTNode) GrDocTag(org.jetbrains.plugins.groovy.lang.groovydoc.psi.api.GrDocTag) GrCaseLabel(org.jetbrains.plugins.groovy.lang.psi.api.statements.clauses.GrCaseLabel) GrThrowStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.branch.GrThrowStatement) GrReturnStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.branch.GrReturnStatement) GrAnnotationArgumentList(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.annotation.GrAnnotationArgumentList) GrArgumentList(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList) GrConditionalExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrConditionalExpression) NotNull(org.jetbrains.annotations.NotNull)

Example 3 with GrDocTag

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

the class GroovyPsiElementFactoryImpl method createDocReferenceElementFromFQN.

@NotNull
@Override
public GrDocReferenceElement createDocReferenceElementFromFQN(@NotNull String qName) {
    PsiFile file = createGroovyFileChecked("/** @see " + qName + " */");
    PsiElement element = file.getFirstChild();
    assert element instanceof GrDocComment;
    GrDocTag tag = PsiTreeUtil.getChildOfType(element, GrDocTag.class);
    assert tag != null : "Doc tag points to null";
    return PsiTreeUtil.getChildOfType(tag, GrDocReferenceElement.class);
}
Also used : GrDocTag(org.jetbrains.plugins.groovy.lang.groovydoc.psi.api.GrDocTag) GrDocComment(org.jetbrains.plugins.groovy.lang.groovydoc.psi.api.GrDocComment) NotNull(org.jetbrains.annotations.NotNull)

Example 4 with GrDocTag

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

the class GroovyPsiElementFactoryImpl method createDocMemberReferenceFromText.

@NotNull
@Override
public GrDocMemberReference createDocMemberReferenceFromText(@NotNull String className, @NotNull String text) {
    PsiFile file = createGroovyFileChecked("/** @see " + className + "#" + text + " */");
    PsiElement element = file.getFirstChild();
    assert element instanceof GrDocComment;
    GrDocTag tag = PsiTreeUtil.getChildOfType(element, GrDocTag.class);
    assert tag != null : "Doc tag points to null";
    return PsiTreeUtil.getChildOfType(tag, GrDocMemberReference.class);
}
Also used : GrDocTag(org.jetbrains.plugins.groovy.lang.groovydoc.psi.api.GrDocTag) GrDocComment(org.jetbrains.plugins.groovy.lang.groovydoc.psi.api.GrDocComment) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

GrDocComment (org.jetbrains.plugins.groovy.lang.groovydoc.psi.api.GrDocComment)4 GrDocTag (org.jetbrains.plugins.groovy.lang.groovydoc.psi.api.GrDocTag)4 NotNull (org.jetbrains.annotations.NotNull)3 GrParameterList (org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameterList)2 ASTNode (com.intellij.lang.ASTNode)1 CanonicalTypes (com.intellij.refactoring.util.CanonicalTypes)1 HashSet (com.intellij.util.containers.HashSet)1 GrDocParameterReference (org.jetbrains.plugins.groovy.lang.groovydoc.psi.api.GrDocParameterReference)1 GroovyFileBase (org.jetbrains.plugins.groovy.lang.psi.GroovyFileBase)1 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)1 GrListOrMap (org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrListOrMap)1 GrModifierList (org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.GrModifierList)1 GrAnnotationArgumentList (org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.annotation.GrAnnotationArgumentList)1 GrArgumentList (org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList)1 GrBreakStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.branch.GrBreakStatement)1 GrContinueStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.branch.GrContinueStatement)1 GrReturnStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.branch.GrReturnStatement)1 GrThrowStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.branch.GrThrowStatement)1 GrCaseLabel (org.jetbrains.plugins.groovy.lang.psi.api.statements.clauses.GrCaseLabel)1 GrCaseSection (org.jetbrains.plugins.groovy.lang.psi.api.statements.clauses.GrCaseSection)1