Search in sources :

Example 71 with GrParameter

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

the class GrHighlightUtil method collectReassignedNames.

private static Set<String> collectReassignedNames(PsiElement scope) {
    final Set<String> result = ContainerUtil.newHashSet();
    PsiTreeUtil.processElements(scope, new PsiElementProcessor() {

        @Override
        public boolean execute(@NotNull PsiElement element) {
            if (!(element instanceof GrReferenceExpression) || !((GrReferenceExpression) element).isQualified()) {
                return true;
            }
            GrReferenceExpression ref = (GrReferenceExpression) element;
            if (isWriteAccess(ref)) {
                String varName = ref.getReferenceName();
                if (!result.contains(varName)) {
                    PsiElement target = ref.resolve();
                    if (target instanceof GrVariable && ((GrVariable) target).getInitializerGroovy() != null || target instanceof GrParameter) {
                        result.add(varName);
                    }
                }
            }
            return true;
        }
    });
    return result;
}
Also used : GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) GrParameter(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter) PsiElementProcessor(com.intellij.psi.search.PsiElementProcessor) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)

Example 72 with GrParameter

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

the class GroovyAnnotator method visitTryStatement.

@Override
public void visitTryStatement(@NotNull GrTryCatchStatement statement) {
    final GrCatchClause[] clauses = statement.getCatchClauses();
    List<PsiType> usedExceptions = new ArrayList<>();
    for (GrCatchClause clause : clauses) {
        final GrParameter parameter = clause.getParameter();
        if (parameter == null)
            continue;
        final GrTypeElement typeElement = parameter.getTypeElementGroovy();
        PsiType type = typeElement != null ? typeElement.getType() : TypesUtil.createType(CommonClassNames.JAVA_LANG_EXCEPTION, statement);
        if (typeElement instanceof GrDisjunctionTypeElement) {
            final GrTypeElement[] elements = ((GrDisjunctionTypeElement) typeElement).getTypeElements();
            PsiType[] types = PsiType.createArray(elements.length);
            for (int i = 0; i < elements.length; i++) {
                types[i] = elements[i].getType();
            }
            List<PsiType> usedInsideDisjunction = new ArrayList<>();
            for (int i = 0; i < types.length; i++) {
                if (checkExceptionUsed(usedExceptions, parameter, elements[i], types[i])) {
                    usedInsideDisjunction.add(types[i]);
                    for (int j = 0; j < types.length; j++) {
                        if (i != j && types[j].isAssignableFrom(types[i])) {
                            myHolder.createWarningAnnotation(elements[i], GroovyBundle.message("unnecessary.type", types[i].getCanonicalText(), types[j].getCanonicalText())).registerFix(new GrRemoveExceptionFix(true));
                        }
                    }
                }
            }
            usedExceptions.addAll(usedInsideDisjunction);
        } else {
            if (checkExceptionUsed(usedExceptions, parameter, typeElement, type)) {
                usedExceptions.add(type);
            }
        }
    }
}
Also used : GrParameter(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter)

Example 73 with GrParameter

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

the class GrParameterListImpl method addInternal.

/*@Override
  public PsiElement addAfter(@NotNull PsiElement element, PsiElement anchor) throws IncorrectOperationException {
    GrParameter[] params = getParameters();

    if (params.length == 0) {
      element = add(element);
    }
    else {
      element = super.addAfter(element, anchor);
      final ASTNode astNode = getNode();
      if (anchor != null) {
        astNode.addLeaf(mCOMMA, ",", element.getNode());
      }
      else {
        astNode.addLeaf(mCOMMA, ",", element.getNextSibling().getNode());
      }
      CodeStyleManager.getInstance(getManager().getProject()).reformat(this);
    }

    return element;
  }

  @Override
  public PsiElement addBefore(@NotNull PsiElement element, PsiElement anchor) throws IncorrectOperationException {
    GrParameter[] params = getParameters();

    if (params.length == 0) {
      element = add(element);
    }
    else {
      element = super.addBefore(element, anchor);
      final ASTNode astNode = getNode();
      if (anchor != null) {
        astNode.addLeaf(mCOMMA, ",", anchor.getNode());
      }
      else {
        astNode.addLeaf(mCOMMA, ",", element.getNode());
      }
      CodeStyleManager.getInstance(getManager().getProject()).reformat(this);
    }

    return element;
  }*/
@Override
public ASTNode addInternal(ASTNode first, ASTNode last, ASTNode anchor, Boolean before) {
    GrParameter[] params = getParameters();
    ASTNode result = super.addInternal(first, last, anchor, before);
    if (first == last && first.getPsi() instanceof GrParameter && params.length > 0) {
        if (before.booleanValue() && anchor != null) {
            getNode().addLeaf(GroovyTokenTypes.mCOMMA, ",", anchor);
        } else if (before.booleanValue() && anchor == null) {
            getNode().addLeaf(GroovyTokenTypes.mCOMMA, ",", result);
        } else if (!before.booleanValue() && anchor != null) {
            getNode().addLeaf(GroovyTokenTypes.mCOMMA, ",", result);
        } else if (!before.booleanValue() && anchor == null) {
            getNode().addLeaf(GroovyTokenTypes.mCOMMA, ",", result.getTreeNext());
        }
    }
    return result;
}
Also used : ASTNode(com.intellij.lang.ASTNode) GrParameter(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter)

Example 74 with GrParameter

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

the class ClosureSyntheticParameter method setName.

@Override
public PsiElement setName(@NotNull String newName) throws IncorrectOperationException {
    if (!newName.equals(getName())) {
        GrParameter parameter = GroovyPsiElementFactory.getInstance(getProject()).createParameter(newName, (String) null, null);
        myClosure.addParameter(parameter);
    }
    return this;
}
Also used : GrParameter(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter)

Example 75 with GrParameter

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

the class GrLightMethodBuilder method copyData.

protected void copyData(GrLightMethodBuilder dst) {
    dst.setMethodKind(getMethodKind());
    dst.setData(getData());
    dst.setNamedParameters(getNamedParameters());
    if (getNavigationElement() != this) {
        dst.setNavigationElement(getNavigationElement());
    }
    dst.setBaseIcon(myBaseIcon);
    dst.setReturnType(getReturnType());
    dst.setContainingClass(getContainingClass());
    dst.getModifierList().copyModifiers(this);
    dst.getParameterList().clear();
    for (GrParameter parameter : getParameterList().getParameters()) {
        dst.addParameter(parameter);
    }
}
Also used : GrParameter(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter)

Aggregations

GrParameter (org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter)99 GrClosableBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock)22 NotNull (org.jetbrains.annotations.NotNull)20 PsiElement (com.intellij.psi.PsiElement)19 GroovyPsiElementFactory (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)19 GrMethod (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod)18 GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)16 GrParameterList (org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameterList)14 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)13 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)12 ArrayList (java.util.ArrayList)11 GrVariable (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable)10 TextRange (com.intellij.openapi.util.TextRange)9 GrOpenBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock)9 Nullable (org.jetbrains.annotations.Nullable)8 GroovyResolveResult (org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult)7 Project (com.intellij.openapi.project.Project)6 IncorrectOperationException (com.intellij.util.IncorrectOperationException)6 GroovyFile (org.jetbrains.plugins.groovy.lang.psi.GroovyFile)6 GrField (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField)6