Search in sources :

Example 11 with GrParameterList

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

the class GrClosableBlockImpl method addParameter.

@Override
public GrParameter addParameter(GrParameter parameter) {
    GrParameterList parameterList = getParameterList();
    if (getArrow() == null) {
        final GrParameterList newParamList = (GrParameterList) addAfter(parameterList, getLBrace());
        parameterList.delete();
        ASTNode next = newParamList.getNode().getTreeNext();
        getNode().addLeaf(GroovyTokenTypes.mCLOSABLE_BLOCK_OP, "->", next);
        return (GrParameter) newParamList.add(parameter);
    }
    return (GrParameter) parameterList.add(parameter);
}
Also used : GrParameterList(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameterList) ASTNode(com.intellij.lang.ASTNode) GrParameter(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter)

Example 12 with GrParameterList

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

the class GrMethodBaseImpl method getParameterList.

@Override
@NotNull
public GrParameterList getParameterList() {
    final GrParameterList parameterList = getStubOrPsiChild(GroovyElementTypes.PARAMETERS_LIST);
    LOG.assertTrue(parameterList != null);
    return parameterList;
}
Also used : GrParameterList(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameterList) NotNull(org.jetbrains.annotations.NotNull)

Example 13 with GrParameterList

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

the class DelegatesToInspection method buildVisitor.

@NotNull
@Override
protected BaseInspectionVisitor buildVisitor() {
    return new BaseInspectionVisitor() {

        @Override
        public void visitAnnotation(@NotNull GrAnnotation annotation) {
            checkTarget(annotation);
            checkDelegatesTo(annotation);
        }

        private void checkTarget(GrAnnotation annotation) {
            if (!GroovyCommonClassNames.GROOVY_LANG_DELEGATES_TO_TARGET.equals(annotation.getQualifiedName()))
                return;
            final PsiElement owner = annotation.getParent().getParent();
            if (!(owner instanceof GrParameter))
                return;
            final boolean isTargetDeclared = annotation.findDeclaredAttributeValue("value") != null;
            String targetName = GrAnnotationUtil.inferStringAttribute(annotation, "value");
            final GrParameterList parameterList = DefaultGroovyMethods.asType(owner.getParent(), GrParameterList.class);
            for (GrParameter parameter : parameterList.getParameters()) {
                final PsiAnnotation delegatesTo = parameter.getModifierList().findAnnotation(GroovyCommonClassNames.GROOVY_LANG_DELEGATES_TO);
                if (delegatesTo != null) {
                    if (isTargetDeclared) {
                        final String curTarget = GrAnnotationUtil.inferStringAttribute(delegatesTo, "target");
                        if (curTarget != null && curTarget.equals(targetName)) {
                            //target is used
                            return;
                        }
                    } else {
                        if (delegatesTo.findDeclaredAttributeValue("target") == null && delegatesTo.findDeclaredAttributeValue("value") == null) {
                            // target is used
                            return;
                        }
                    }
                }
            }
            registerError(annotation.getClassReference(), GroovyInspectionBundle.message("target.annotation.is.unused"), LocalQuickFix.EMPTY_ARRAY, ProblemHighlightType.GENERIC_ERROR_OR_WARNING);
        }

        private void checkDelegatesTo(GrAnnotation annotation) {
            if (!GroovyCommonClassNames.GROOVY_LANG_DELEGATES_TO.equals(annotation.getQualifiedName()))
                return;
            final PsiElement owner = annotation.getParent().getParent();
            if (!(owner instanceof GrParameter))
                return;
            final PsiAnnotationMemberValue targetPair = annotation.findDeclaredAttributeValue("target");
            if (targetPair == null)
                return;
            String targetName = GrAnnotationUtil.inferStringAttribute(annotation, "target");
            final GrParameterList parameterList = DefaultGroovyMethods.asType(owner.getParent(), GrParameterList.class);
            for (GrParameter parameter : parameterList.getParameters()) {
                final PsiAnnotation target = parameter.getModifierList().findAnnotation(GroovyCommonClassNames.GROOVY_LANG_DELEGATES_TO_TARGET);
                if (target != null) {
                    final String curTarget = GrAnnotationUtil.inferStringAttribute(target, "value");
                    if (curTarget != null && curTarget.equals(targetName)) {
                        //target is used
                        return;
                    }
                }
            }
            registerError(targetPair, GroovyInspectionBundle.message("target.0.does.not.exist", targetName != null ? targetName : "?"), LocalQuickFix.EMPTY_ARRAY, ProblemHighlightType.GENERIC_ERROR_OR_WARNING);
        }
    };
}
Also used : GrParameterList(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameterList) BaseInspectionVisitor(org.jetbrains.plugins.groovy.codeInspection.BaseInspectionVisitor) GrAnnotation(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.annotation.GrAnnotation) PsiAnnotation(com.intellij.psi.PsiAnnotation) GrParameter(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter) NotNull(org.jetbrains.annotations.NotNull) PsiElement(com.intellij.psi.PsiElement) PsiAnnotationMemberValue(com.intellij.psi.PsiAnnotationMemberValue) NotNull(org.jetbrains.annotations.NotNull)

Example 14 with GrParameterList

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

the class GroovyValidationUtil method validateNewParameterName.

public static boolean validateNewParameterName(GrParameter variable, MultiMap<PsiElement, String> conflicts, @NotNull String varName) {
    GrParameterList list = PsiTreeUtil.getParentOfType(variable, GrParameterList.class);
    GrParametersOwner owner = PsiTreeUtil.getParentOfType(variable, GrParametersOwner.class);
    assert owner != null;
    for (GrParameter parameter : list.getParameters()) {
        if (parameter.equals(variable))
            continue;
        validateVariableOccurrencesDownImpl(parameter, conflicts, varName);
    }
    validateVariableOccurrencesDown(owner, list, conflicts, varName);
    PsiElement parent = owner.getParent();
    validateVariableOccurrencesUp(parent, owner, conflicts, varName, parent instanceof GroovyFile);
    return conflicts.isEmpty();
}
Also used : GrParameterList(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameterList) GrParameter(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter) PsiElement(com.intellij.psi.PsiElement) GroovyFile(org.jetbrains.plugins.groovy.lang.psi.GroovyFile)

Example 15 with GrParameterList

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

the class GrBlockImpl method addStatementBefore.

@Override
@NotNull
public GrStatement addStatementBefore(@NotNull GrStatement element, @Nullable GrStatement anchor) throws IncorrectOperationException {
    if (anchor == null && getRBrace() == null) {
        throw new IncorrectOperationException();
    }
    if (anchor != null && !this.equals(anchor.getParent())) {
        throw new IncorrectOperationException();
    }
    final LeafElement nls = Factory.createSingleLeafElement(GroovyTokenTypes.mNLS, "\n", 0, 1, null, getManager());
    PsiElement actualAnchor = anchor == null ? getRBrace() : anchor;
    if (mayUseNewLinesAsSeparators()) {
        PsiElement prev = actualAnchor.getPrevSibling();
        if (prev instanceof GrParameterList && prev.getTextLength() == 0 && prev.getPrevSibling() != null) {
            prev = prev.getPrevSibling();
        }
        if (!PsiUtil.isLineFeed(prev)) {
            addBefore(nls.getPsi(), actualAnchor);
        }
    }
    element = (GrStatement) addBefore(element, actualAnchor);
    if (mayUseNewLinesAsSeparators()) {
        addBefore(nls.getPsi(), actualAnchor);
    } else {
        addBefore(Factory.createSingleLeafElement(GroovyTokenTypes.mNLS, "\n", 0, 1, null, getManager()).getPsi(), actualAnchor);
    }
    return element;
}
Also used : GrParameterList(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameterList) IncorrectOperationException(com.intellij.util.IncorrectOperationException) LeafElement(com.intellij.psi.impl.source.tree.LeafElement) PsiElement(com.intellij.psi.PsiElement) LazyParseablePsiElement(com.intellij.psi.impl.source.tree.LazyParseablePsiElement) ASTDelegatePsiElement(com.intellij.extapi.psi.ASTDelegatePsiElement) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

GrParameterList (org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameterList)22 GrParameter (org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter)14 PsiElement (com.intellij.psi.PsiElement)12 NotNull (org.jetbrains.annotations.NotNull)7 GrClosableBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock)7 GroovyPsiElementFactory (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)4 GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)4 PsiType (com.intellij.psi.PsiType)3 IncorrectOperationException (com.intellij.util.IncorrectOperationException)3 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)3 GrVariable (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable)3 GrVariableDeclaration (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration)3 ASTNode (com.intellij.lang.ASTNode)2 Document (com.intellij.openapi.editor.Document)2 Nullable (org.jetbrains.annotations.Nullable)2 GrDocComment (org.jetbrains.plugins.groovy.lang.groovydoc.psi.api.GrDocComment)2 GrDocTag (org.jetbrains.plugins.groovy.lang.groovydoc.psi.api.GrDocTag)2 GrListOrMap (org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrListOrMap)2 GrArgumentList (org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList)2 GrCall (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrCall)2