Search in sources :

Example 51 with GrParameter

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

the class RemoveUnusedGrParameterFix method isAvailable.

@Override
public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) {
    PsiElement at = file.findElementAt(editor.getCaretModel().getOffset());
    GrParameter parameter = PsiTreeUtil.getParentOfType(at, GrParameter.class);
    return parameter != null && myName.equals(parameter.getName());
}
Also used : GrParameter(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter) PsiElement(com.intellij.psi.PsiElement)

Example 52 with GrParameter

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

the class ExtractUtil method collectUsedLocalVarsOrParamsDeclaredOutside.

private static Collection<GrVariable> collectUsedLocalVarsOrParamsDeclaredOutside(ExtractInfoHelper helper) {
    final Collection<GrVariable> result = new HashSet<>();
    final TextRange range = getRangeOfRefactoring(helper);
    final int start = range.getStartOffset();
    final int end = range.getEndOffset();
    final GroovyRecursiveElementVisitor visitor = new GroovyRecursiveElementVisitor() {

        @Override
        public void visitReferenceExpression(@NotNull GrReferenceExpression ref) {
            final PsiElement resolved = ref.resolve();
            if ((resolved instanceof GrParameter || PsiUtil.isLocalVariable(resolved)) && resolved.isPhysical()) {
                final int offset = resolved.getTextRange().getStartOffset();
                //var is declared outside of selected code
                if (offset < start || end <= offset) {
                    result.add((GrVariable) resolved);
                }
            }
        }
    };
    final GrStatement[] statements = helper.getStatements();
    for (GrStatement statement : statements) {
        statement.accept(visitor);
    }
    return result;
}
Also used : GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) TextRange(com.intellij.openapi.util.TextRange) GroovyRecursiveElementVisitor(org.jetbrains.plugins.groovy.lang.psi.GroovyRecursiveElementVisitor) GrParameter(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter) NotNull(org.jetbrains.annotations.NotNull) PsiElement(com.intellij.psi.PsiElement) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)

Example 53 with GrParameter

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

the class GrUnresolvableLocalCollisionDetector method visitUpstreamCollisions.

private static void visitUpstreamCollisions(PsiElement element, String newName, GroovyPsiElement place, CollidingVariableVisitor visitor) {
    final GrReferenceExpression refExpr = GroovyPsiElementFactory.getInstance(place.getProject()).createReferenceExpressionFromText(newName, place);
    final GroovyResolveResult[] results = refExpr.multiResolve(false);
    for (GroovyResolveResult result : results) {
        final PsiElement resolved = result.getElement();
        if (resolved instanceof GrParameter || (resolved instanceof GrVariable && !(resolved instanceof GrField))) {
            final PsiElement parent = PsiTreeUtil.findCommonParent(resolved, element);
            if (parent != null) {
                PsiElement current = element;
                while (current != null && current != parent) {
                    if (current instanceof PsiMethod || current instanceof PsiClass || current instanceof GrClosableBlock) {
                        return;
                    }
                    current = current.getParent();
                }
            }
            if (!place.getManager().areElementsEquivalent(element, resolved)) {
                visitor.visitCollidingVariable((PsiVariable) resolved);
            }
        }
    }
}
Also used : GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) GrField(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField) GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) PsiMethod(com.intellij.psi.PsiMethod) PsiClass(com.intellij.psi.PsiClass) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) GrParameter(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter) PsiElement(com.intellij.psi.PsiElement) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)

Example 54 with GrParameter

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

the class ClassItemGeneratorImpl method generateDelegateCall.

private StringBuilder generateDelegateCall(GrReflectedMethod method) {
    final GrParameter[] actualParams = method.getParameterList().getParameters();
    final GrParameter[] parameters = method.getBaseMethod().getParameters();
    Set<String> actual = new HashSet<>(actualParams.length);
    for (GrParameter param : actualParams) {
        actual.add(param.getName());
    }
    StringBuilder builder = new StringBuilder();
    if (method.isConstructor()) {
        builder.append("this");
    } else {
        if (!PsiType.VOID.equals(context.typeProvider.getReturnType(method))) {
            builder.append("return ");
        }
        builder.append(method.getName());
    }
    builder.append('(');
    for (GrParameter parameter : parameters) {
        if (actual.contains(parameter.getName())) {
            builder.append(parameter.getName());
        } else {
            LOG.assertTrue(parameter.isOptional());
            final GrExpression initializer = parameter.getInitializerGroovy();
            LOG.assertTrue(initializer != null);
            builder.append(initializer.getText());
        }
        builder.append(", ");
    }
    builder.delete(builder.length() - 2, builder.length());
    //builder.removeFromTheEnd(2);
    builder.append(')');
    final GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(context.project);
    final GrStatement delegateCall;
    if (method.isConstructor()) {
        delegateCall = factory.createConstructorInvocation(builder.toString(), method);
    } else {
        delegateCall = factory.createStatementFromText(builder.toString(), method);
    }
    final StringBuilder result = new StringBuilder();
    delegateCall.accept(new CodeBlockGenerator(result, this.context.extend()));
    return result;
}
Also used : GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GrParameter(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter) HashSet(com.intellij.util.containers.HashSet) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)

Example 55 with GrParameter

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

the class CodeBlockGenerator method generateCodeBlock.

public void generateCodeBlock(@NotNull GrParameter[] parameters, @Nullable GrCodeBlock block, boolean shouldInsertReturnNull) {
    builder.append("{");
    for (GrParameter parameter : parameters) {
        if (context.analyzedVars.toWrap(parameter)) {
            StringBuilder typeText = new StringBuilder().append(GroovyCommonClassNames.GROOVY_LANG_REFERENCE);
            GenerationUtil.writeTypeParameters(typeText, new PsiType[] { context.typeProvider.getParameterType(parameter) }, parameter, new GeneratorClassNameProvider());
            builder.append("final ").append(typeText).append(' ').append(context.analyzedVars.toVarName(parameter)).append(" = new ").append(typeText).append('(').append(parameter.getName()).append(");\n");
        }
    }
    visitStatementOwner(block, shouldInsertReturnNull);
    builder.append("}\n");
}
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