Search in sources :

Example 6 with GrTypeDefinitionBody

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinitionBody in project intellij-community by JetBrains.

the class GrTypeDefinitionImpl method setName.

@Override
public PsiElement setName(@NonNls @NotNull String name) throws IncorrectOperationException {
    boolean renameFile = isRenameFileOnClassRenaming();
    final String oldName = getName();
    org.jetbrains.plugins.groovy.lang.psi.impl.PsiImplUtil.setName(name, getNameIdentifierGroovy());
    final GrTypeDefinitionBody body = getBody();
    if (body != null) {
        for (PsiMethod method : body.getMethods()) {
            if (method.isConstructor() && method.getName().equals(oldName))
                method.setName(name);
        }
    }
    if (renameFile) {
        final PsiFile file = getContainingFile();
        final VirtualFile virtualFile = file.getVirtualFile();
        final String ext;
        if (virtualFile != null) {
            ext = virtualFile.getExtension();
        } else {
            ext = GroovyFileType.GROOVY_FILE_TYPE.getDefaultExtension();
        }
        file.setName(name + "." + ext);
    }
    return this;
}
Also used : GrTypeDefinitionBody(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinitionBody) VirtualFile(com.intellij.openapi.vfs.VirtualFile)

Example 7 with GrTypeDefinitionBody

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinitionBody in project intellij-community by JetBrains.

the class GrClassImplUtil method processDeclarations.

public static boolean processDeclarations(@NotNull GrTypeDefinition grType, @NotNull PsiScopeProcessor processor, @NotNull ResolveState state, @Nullable PsiElement lastParent, @NotNull PsiElement place) {
    if (place instanceof GrCodeReferenceElement && lastParent instanceof GrModifierList) {
        final PsiElement possibleAnnotation = PsiTreeUtil.skipParentsOfType(place, GrCodeReferenceElement.class);
        if (possibleAnnotation instanceof GrAnnotation && possibleAnnotation.getParent() == lastParent) {
            //don't process class members while resolving annotation which annotates current class
            return true;
        }
    }
    for (final PsiTypeParameter typeParameter : grType.getTypeParameters()) {
        if (!ResolveUtil.processElement(processor, typeParameter, state))
            return false;
    }
    NameHint nameHint = processor.getHint(NameHint.KEY);
    String name = nameHint == null ? null : nameHint.getName(state);
    ElementClassHint classHint = processor.getHint(ElementClassHint.KEY);
    final PsiSubstitutor substitutor = state.get(PsiSubstitutor.KEY);
    final PsiElementFactory factory = JavaPsiFacade.getElementFactory(place.getProject());
    boolean processInstanceMethods = (ResolveUtil.shouldProcessMethods(classHint) || ResolveUtil.shouldProcessProperties(classHint)) && shouldProcessInstanceMembers(grType, lastParent);
    LanguageLevel level = PsiUtil.getLanguageLevel(place);
    if (ResolveUtil.shouldProcessProperties(classHint)) {
        Map<String, CandidateInfo> fieldsMap = CollectClassMembersUtil.getAllFields(grType);
        if (name != null) {
            CandidateInfo fieldInfo = fieldsMap.get(name);
            if (fieldInfo != null) {
                if (!processField(grType, processor, state, place, processInstanceMethods, substitutor, factory, level, fieldInfo)) {
                    return false;
                }
            } else if (grType.isTrait() && lastParent != null) {
                PsiField field = findFieldByName(grType, name, false, true);
                if (field != null && field.hasModifierProperty(PsiModifier.PUBLIC)) {
                    if (!processField(grType, processor, state, place, processInstanceMethods, substitutor, factory, level, new CandidateInfo(field, PsiSubstitutor.EMPTY))) {
                        return false;
                    }
                }
            }
        } else {
            for (CandidateInfo info : fieldsMap.values()) {
                if (!processField(grType, processor, state, place, processInstanceMethods, substitutor, factory, level, info)) {
                    return false;
                }
            }
            if (grType.isTrait() && lastParent != null) {
                for (PsiField field : CollectClassMembersUtil.getFields(grType, true)) {
                    if (field.hasModifierProperty(PsiModifier.PUBLIC)) {
                        if (!processField(grType, processor, state, place, processInstanceMethods, substitutor, factory, level, new CandidateInfo(field, PsiSubstitutor.EMPTY))) {
                            return false;
                        }
                    }
                }
            }
        }
    }
    if (ResolveUtil.shouldProcessMethods(classHint)) {
        Map<String, List<CandidateInfo>> methodsMap = CollectClassMembersUtil.getAllMethods(grType, true);
        boolean isPlaceGroovy = place.getLanguage() == GroovyLanguage.INSTANCE;
        if (name == null) {
            for (List<CandidateInfo> list : methodsMap.values()) {
                for (CandidateInfo info : list) {
                    if (!processMethod(grType, processor, state, place, processInstanceMethods, substitutor, factory, level, isPlaceGroovy, info)) {
                        return false;
                    }
                }
            }
        } else {
            List<CandidateInfo> byName = methodsMap.get(name);
            if (byName != null) {
                for (CandidateInfo info : byName) {
                    if (!processMethod(grType, processor, state, place, processInstanceMethods, substitutor, factory, level, isPlaceGroovy, info)) {
                        return false;
                    }
                }
            }
        }
    }
    final GrTypeDefinitionBody body = grType.getBody();
    if (body != null) {
        if (ResolveUtil.shouldProcessClasses(classHint)) {
            for (PsiClass innerClass : getInnerClassesForResolve(grType, lastParent, place)) {
                if (name != null && !name.equals(innerClass.getName()))
                    continue;
                if (!processor.execute(innerClass, state))
                    return false;
            }
        }
    }
    return true;
}
Also used : GrModifierList(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.GrModifierList) GrAnnotation(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.annotation.GrAnnotation) ElementClassHint(com.intellij.psi.scope.ElementClassHint) CandidateInfo(com.intellij.psi.infos.CandidateInfo) GrTypeDefinitionBody(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinitionBody) GrCodeReferenceElement(org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement) LanguageLevel(com.intellij.pom.java.LanguageLevel) GrModifierList(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.GrModifierList) GrReferenceList(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrReferenceList) NameHint(com.intellij.psi.scope.NameHint)

Example 8 with GrTypeDefinitionBody

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinitionBody in project intellij-community by JetBrains.

the class GrVariableDeclarationElementType method shouldCreateStub.

@Override
public boolean shouldCreateStub(ASTNode node) {
    PsiElement parent = SharedImplUtil.getParent(node);
    if (parent instanceof GrTypeDefinitionBody) {
        // store fields
        return true;
    }
    if (PsiTreeUtil.getParentOfType(parent, GrTypeDefinition.class) != null) {
        // do not store variable declarations within classes, as they are not scripts
        return false;
    }
    PsiElement psi = node.getPsi();
    if (!(psi instanceof GrVariableDeclaration) || ((GrVariableDeclaration) psi).getModifierList().getRawAnnotations().length == 0) {
        // store only annotated declarations
        return false;
    }
    PsiFile file = psi.getContainingFile();
    return file instanceof GroovyFile && ((GroovyFile) file).isScript();
}
Also used : GrTypeDefinitionBody(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinitionBody) GrVariableDeclaration(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration) GrTypeDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition) PsiFile(com.intellij.psi.PsiFile) PsiElement(com.intellij.psi.PsiElement) GroovyFile(org.jetbrains.plugins.groovy.lang.psi.GroovyFile)

Example 9 with GrTypeDefinitionBody

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinitionBody in project intellij-community by JetBrains.

the class GroovyCompletionUtil method isFirstElementAfterPossibleModifiersInVariableDeclaration.

/**
   * return true, if the element is first element after modifiers and there is no type element
   */
public static boolean isFirstElementAfterPossibleModifiersInVariableDeclaration(PsiElement element, boolean acceptParameter) {
    if (element.getParent() instanceof GrTypeDefinitionBody && !(element instanceof PsiComment)) {
        //is first on the line?
        String text = element.getContainingFile().getText();
        int i = CharArrayUtil.shiftBackward(text, element.getTextRange().getStartOffset() - 1, " \t");
        return i >= 0 && (text.charAt(i) == '\n' || text.charAt(i) == '{');
    }
    final PsiElement parent = element.getParent();
    if (!(parent instanceof GrVariable))
        return false;
    if (acceptParameter && parent instanceof GrParameter) {
        return ((GrParameter) parent).getTypeElementGroovy() == null;
    }
    final PsiElement pparent = parent.getParent();
    if (!(pparent instanceof GrVariableDeclaration))
        return false;
    if (((GrVariableDeclaration) pparent).isTuple())
        return false;
    final GrVariableDeclaration variableDeclaration = (GrVariableDeclaration) pparent;
    if (variableDeclaration.getTypeElementGroovy() != null)
        return false;
    return variableDeclaration.getVariables()[0] == parent;
}
Also used : GrTypeDefinitionBody(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinitionBody) GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) GrVariableDeclaration(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration) GrParameter(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) LeafPsiElement(com.intellij.psi.impl.source.tree.LeafPsiElement)

Example 10 with GrTypeDefinitionBody

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinitionBody in project intellij-community by JetBrains.

the class GroovyBlockGenerator method generateSubBlocks.

public List<Block> generateSubBlocks() {
    //For binary expressions
    PsiElement blockPsi = myNode.getPsi();
    IElementType elementType = myNode.getElementType();
    if (blockPsi instanceof GrBinaryExpression && !(blockPsi.getParent() instanceof GrBinaryExpression)) {
        return generateForBinaryExpr();
    }
    //For multiline strings
    if ((elementType == GroovyTokenTypes.mSTRING_LITERAL || elementType == GroovyTokenTypes.mGSTRING_LITERAL) && myBlock.getTextRange().equals(myNode.getTextRange())) {
        String text = myNode.getText();
        if (text.length() > 6) {
            if (text.substring(0, 3).equals("'''") && text.substring(text.length() - 3).equals("'''") || text.substring(0, 3).equals("\"\"\"") & text.substring(text.length() - 3).equals("\"\"\"")) {
                return generateForMultiLineString();
            }
        }
    }
    //for gstrings
    if (elementType == GroovyElementTypes.GSTRING || elementType == GroovyElementTypes.REGEX || elementType == GroovyTokenTypes.mREGEX_LITERAL || elementType == GroovyTokenTypes.mDOLLAR_SLASH_REGEX_LITERAL) {
        final FormattingContext context = myNode.getPsi() instanceof GrString && ((GrString) myNode.getPsi()).isPlainString() ? myContext.createContext(true) : myContext;
        final ArrayList<Block> subBlocks = new ArrayList<>();
        ASTNode[] children = getGroovyChildren(myNode);
        for (ASTNode childNode : children) {
            if (childNode.getTextRange().getLength() > 0) {
                subBlocks.add(new GroovyBlock(childNode, getIndent(childNode), Wrap.createWrap(WrapType.NONE, false), context));
            }
        }
        return subBlocks;
    }
    // chained properties, calls, indexing, etc
    if (NESTED.contains(elementType) && blockPsi.getParent() != null && !NESTED.contains(blockPsi.getParent().getNode().getElementType())) {
        final List<Block> subBlocks = new ArrayList<>();
        AlignmentProvider.Aligner dotsAligner = myContext.getSettings().ALIGN_MULTILINE_CHAINED_METHODS ? myAlignmentProvider.createAligner(false) : null;
        final Wrap wrap = myWrappingProcessor.getChainedMethodCallWrap();
        addNestedChildren(myNode.getPsi(), subBlocks, dotsAligner, true, wrap);
        return subBlocks;
    }
    if (blockPsi instanceof GrListOrMap && ((GrListOrMap) blockPsi).isMap() && myContext.getGroovySettings().ALIGN_NAMED_ARGS_IN_MAP) {
        AlignmentProvider.Aligner labels = myAlignmentProvider.createAligner(false);
        AlignmentProvider.Aligner exprs = myAlignmentProvider.createAligner(true);
        GrNamedArgument[] namedArgs = ((GrListOrMap) blockPsi).getNamedArguments();
        for (GrNamedArgument arg : namedArgs) {
            GrArgumentLabel label = arg.getLabel();
            if (label != null)
                labels.append(label);
            PsiElement colon = arg.getColon();
            if (colon == null)
                colon = arg.getExpression();
            if (colon != null)
                exprs.append(colon);
        }
    }
    // For Parameter lists
    if (isListLikeClause(blockPsi)) {
        final ArrayList<Block> subBlocks = new ArrayList<>();
        List<ASTNode> astNodes = visibleChildren(myNode);
        if (mustAlign(blockPsi, astNodes)) {
            final AlignmentProvider.Aligner aligner = myAlignmentProvider.createAligner(false);
            for (ASTNode node : astNodes) {
                if (!isKeyword(node))
                    aligner.append(node.getPsi());
            }
        }
        for (ASTNode childNode : astNodes) {
            subBlocks.add(new GroovyBlock(childNode, getIndent(childNode), getChildWrap(childNode), myContext));
        }
        return subBlocks;
    }
    boolean classLevel = blockPsi instanceof GrTypeDefinitionBody;
    if (blockPsi instanceof GrClosableBlock && ((GrClosableBlock) blockPsi).getArrow() != null && ((GrClosableBlock) blockPsi).getParameters().length > 0 && !getClosureBodyVisibleChildren(myNode).isEmpty()) {
        GrClosableBlock closableBlock = (GrClosableBlock) blockPsi;
        ArrayList<Block> blocks = new ArrayList<>();
        PsiElement lbrace = closableBlock.getLBrace();
        if (lbrace != null) {
            ASTNode node = lbrace.getNode();
            blocks.add(new GroovyBlock(node, getIndent(node), Wrap.createWrap(WrapType.NONE, false), myContext));
        }
        /* {
        Indent indent = GroovyIndentProcessor.getChildIndent(myBlock, parameterListNode);
        GroovyBlock block = new GroovyBlock(parameterListNode, indent, myWrap, mySettings, myGroovySettings, myAlignmentProvider);
        blocks.add(block);
      }

      {
        PsiElement arrow = closableBlock.getArrow();
        ASTNode node = arrow.getNode();
        Indent indent = GroovyIndentProcessor.getChildIndent(myBlock, node);
        GroovyBlock block = new GroovyBlock(node, indent, myWrap, mySettings, myGroovySettings, myAlignmentProvider);
        blocks.add(block);
      }*/
        {
            Indent indent = Indent.getNormalIndent();
            ASTNode parameterListNode = closableBlock.getParameterList().getNode();
            ClosureBodyBlock bodyBlock = new ClosureBodyBlock(parameterListNode, indent, Wrap.createWrap(WrapType.NONE, false), myContext);
            blocks.add(bodyBlock);
        }
        PsiElement rbrace = closableBlock.getRBrace();
        if (rbrace != null) {
            ASTNode node = rbrace.getNode();
            blocks.add(new GroovyBlock(node, getIndent(node), Wrap.createWrap(WrapType.NONE, false), myContext));
        }
        return blocks;
    }
    if (blockPsi instanceof GrCodeBlock || blockPsi instanceof GroovyFile || classLevel) {
        return generateSubBlockForCodeBlocks(classLevel, visibleChildren(myNode), myContext.getGroovySettings().INDENT_LABEL_BLOCKS);
    }
    if (blockPsi instanceof GrMethod) {
        final ArrayList<Block> subBlocks = new ArrayList<>();
        for (ASTNode childNode : getGroovyChildren(myNode)) {
            if (childNode.getElementType() == GroovyTokenTypes.mLPAREN)
                continue;
            if (childNode.getElementType() == GroovyTokenTypes.mRPAREN)
                continue;
            if (childNode.getElementType() == GroovyElementTypes.PARAMETERS_LIST) {
                subBlocks.add(new ParameterListBlock(((GrMethod) blockPsi), Indent.getNoneIndent(), Wrap.createWrap(WrapType.NONE, false), myContext));
            } else if (canBeCorrectBlock(childNode)) {
                subBlocks.add(new GroovyBlock(childNode, getIndent(childNode), getChildWrap(childNode), myContext));
            }
        }
        return subBlocks;
    } else if (blockPsi instanceof GrTraditionalForClause) {
        if (myContext.getSettings().ALIGN_MULTILINE_FOR) {
            final GrTraditionalForClause clause = (GrTraditionalForClause) blockPsi;
            final AlignmentProvider.Aligner parenthesesAligner = myAlignmentProvider.createAligner(false);
            parenthesesAligner.append(clause.getInitialization());
            parenthesesAligner.append(clause.getCondition());
            parenthesesAligner.append(clause.getUpdate());
        }
    } else if (blockPsi instanceof GrBinaryExpression) {
        if (myContext.getSettings().ALIGN_MULTILINE_BINARY_OPERATION) {
            final GrBinaryExpression binary = (GrBinaryExpression) blockPsi;
            final GrExpression left = binary.getLeftOperand();
            final GrExpression right = binary.getRightOperand();
            if (left != null && right != null) {
                myAlignmentProvider.addPair(left, right, false);
            }
        }
    } else if (blockPsi instanceof GrAssignmentExpression) {
        if (myContext.getSettings().ALIGN_MULTILINE_ASSIGNMENT) {
            final GrAssignmentExpression assignment = (GrAssignmentExpression) blockPsi;
            final GrExpression lValue = assignment.getLValue();
            final GrExpression rValue = assignment.getRValue();
            if (lValue != null && rValue != null) {
                myAlignmentProvider.addPair(lValue, rValue, false);
            }
        }
    } else if (blockPsi instanceof GrConditionalExpression) {
        if (myContext.getSettings().ALIGN_MULTILINE_TERNARY_OPERATION) {
            final GrConditionalExpression conditional = (GrConditionalExpression) blockPsi;
            final AlignmentProvider.Aligner exprAligner = myAlignmentProvider.createAligner(false);
            exprAligner.append(conditional.getCondition());
            if (!(conditional instanceof GrElvisExpression)) {
                exprAligner.append(conditional.getThenBranch());
            }
            exprAligner.append(conditional.getElseBranch());
            ASTNode question = conditional.getNode().findChildByType(GroovyTokenTypes.mQUESTION);
            ASTNode colon = conditional.getNode().findChildByType(GroovyTokenTypes.mCOLON);
            if (question != null && colon != null) {
                AlignmentProvider.Aligner questionColonAligner = myAlignmentProvider.createAligner(false);
                questionColonAligner.append(question.getPsi());
                questionColonAligner.append(colon.getPsi());
            }
        }
    }
    // For other cases
    final ArrayList<Block> subBlocks = new ArrayList<>();
    for (ASTNode childNode : visibleChildren(myNode)) {
        subBlocks.add(new GroovyBlock(childNode, getIndent(childNode), getChildWrap(childNode), myContext));
    }
    return subBlocks;
}
Also used : FormattingContext(org.jetbrains.plugins.groovy.formatter.FormattingContext) GrNamedArgument(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrNamedArgument) GrString(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrString) ArrayList(java.util.ArrayList) GrString(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrString) AlignmentProvider(org.jetbrains.plugins.groovy.formatter.AlignmentProvider) GrListOrMap(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrListOrMap) ASTNode(com.intellij.lang.ASTNode) PsiElement(com.intellij.psi.PsiElement) LeafPsiElement(com.intellij.psi.impl.source.tree.LeafPsiElement) GrCodeBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrCodeBlock) GrArgumentLabel(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentLabel) GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) IElementType(com.intellij.psi.tree.IElementType) GrTypeDefinitionBody(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinitionBody) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) GrCodeBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrCodeBlock) GroovyFile(org.jetbrains.plugins.groovy.lang.psi.GroovyFile) GrTraditionalForClause(org.jetbrains.plugins.groovy.lang.psi.api.statements.clauses.GrTraditionalForClause)

Aggregations

GrTypeDefinitionBody (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinitionBody)11 LeafPsiElement (com.intellij.psi.impl.source.tree.LeafPsiElement)4 GrVariableDeclaration (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration)4 PsiElement (com.intellij.psi.PsiElement)3 GroovyFile (org.jetbrains.plugins.groovy.lang.psi.GroovyFile)3 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)3 GrTypeDefinition (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition)3 GrMethod (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod)3 ASTNode (com.intellij.lang.ASTNode)2 CandidateInfo (com.intellij.psi.infos.CandidateInfo)2 ArrayList (java.util.ArrayList)2 GrVariable (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable)2 GrCodeBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrCodeBlock)2 GrCodeReferenceElement (org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement)2 LineRange (com.intellij.codeInsight.editorActions.moveUpDown.LineRange)1 Pair (com.intellij.openapi.util.Pair)1 TextRange (com.intellij.openapi.util.TextRange)1 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1 LanguageLevel (com.intellij.pom.java.LanguageLevel)1 PsiFile (com.intellij.psi.PsiFile)1