Search in sources :

Example 86 with GrParameter

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

the class MyPredicate method processIntention.

@Override
protected void processIntention(@NotNull PsiElement element, @NotNull final Project project, Editor editor) throws IncorrectOperationException {
    final GrListOrMap map = (GrListOrMap) element;
    final GrNamedArgument[] namedArguments = map.getNamedArguments();
    LOG.assertTrue(map.getInitializers().length == 0);
    final PsiFile file = map.getContainingFile();
    final String packageName = file instanceof GroovyFileBase ? ((GroovyFileBase) file).getPackageName() : "";
    final CreateClassDialog dialog = new CreateClassDialog(project, GroovyBundle.message("create.class.family.name"), "", packageName, GrCreateClassKind.CLASS, true, ModuleUtilCore.findModuleForPsiElement(element));
    dialog.show();
    if (dialog.getExitCode() != DialogWrapper.OK_EXIT_CODE)
        return;
    boolean replaceReturnType = checkForReturnFromMethod(map);
    boolean variableDeclaration = checkForVariableDeclaration(map);
    final GrParameter methodParameter = checkForMethodParameter(map);
    final String qualifiedClassName = dialog.getClassName();
    final String selectedPackageName = StringUtil.getPackageName(qualifiedClassName);
    final String shortName = StringUtil.getShortName(qualifiedClassName);
    final GrTypeDefinition typeDefinition = createClass(project, namedArguments, selectedPackageName, shortName);
    final PsiClass generatedClass = CreateClassActionBase.createClassByType(dialog.getTargetDirectory(), typeDefinition.getName(), PsiManager.getInstance(project), map, GroovyTemplates.GROOVY_CLASS, true);
    final PsiClass replaced = (PsiClass) generatedClass.replace(typeDefinition);
    replaceMapWithClass(project, map, replaced, replaceReturnType, variableDeclaration, methodParameter);
}
Also used : GroovyFileBase(org.jetbrains.plugins.groovy.lang.psi.GroovyFileBase) GrNamedArgument(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrNamedArgument) CreateClassDialog(com.intellij.codeInsight.intention.impl.CreateClassDialog) GrTypeDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition) GrListOrMap(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrListOrMap) GrParameter(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter)

Example 87 with GrParameter

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

the class ParameterToMapEntryTest method doTestImpl.

private void doTestImpl(String filePath) {
    myFixture.configureByFile(filePath);
    int offset = myFixture.getEditor().getCaretModel().getOffset();
    final PsiFile file = myFixture.getFile();
    final ConvertParameterToMapEntryIntention intention = new ConvertParameterToMapEntryIntention();
    PsiElement element = file.findElementAt(offset);
    while (element != null && !(element instanceof GrReferenceExpression || element instanceof GrParameter)) {
        element = element.getParent();
    }
    Assert.assertNotNull(element);
    final PsiElementPredicate condition = intention.getElementPredicate();
    Assert.assertTrue(condition.satisfiedBy(element));
    // Launch it!
    intention.processIntention(element, myFixture.getProject(), myFixture.getEditor());
    PostprocessReformattingAspect.getInstance(getProject()).doPostponedFormatting();
    final String result = file.getText();
    //System.out.println(result);
    myFixture.checkResultByFile(filePath.replace(".groovy", ".test"), true);
//    String expected = getExpectedResult(filePath);
//    Assert.assertEquals(expected, result);
}
Also used : PsiFile(com.intellij.psi.PsiFile) PsiElementPredicate(org.jetbrains.plugins.groovy.intentions.base.PsiElementPredicate) GrParameter(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter) PsiElement(com.intellij.psi.PsiElement) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)

Example 88 with GrParameter

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

the class GrDocParameterReferenceImpl method multiResolve.

@Override
@NotNull
public ResolveResult[] multiResolve(boolean incompleteCode) {
    final String name = getName();
    if (name == null)
        return ResolveResult.EMPTY_ARRAY;
    ArrayList<GroovyResolveResult> candidates = new ArrayList<>();
    final PsiElement owner = GrDocCommentUtil.findDocOwner(this);
    if (owner instanceof GrMethod) {
        final GrMethod method = (GrMethod) owner;
        final GrParameter[] parameters = method.getParameters();
        for (GrParameter parameter : parameters) {
            if (name.equals(parameter.getName())) {
                candidates.add(new GroovyResolveResultImpl(parameter, true));
            }
        }
        return candidates.toArray(new ResolveResult[candidates.size()]);
    } else {
        final PsiElement firstChild = getFirstChild();
        if (owner instanceof GrTypeParameterListOwner && firstChild != null) {
            final ASTNode node = firstChild.getNode();
            if (node != null && GroovyDocTokenTypes.mGDOC_TAG_VALUE_LT.equals(node.getElementType())) {
                final PsiTypeParameter[] typeParameters = ((PsiTypeParameterListOwner) owner).getTypeParameters();
                for (PsiTypeParameter typeParameter : typeParameters) {
                    if (name.equals(typeParameter.getName())) {
                        candidates.add(new GroovyResolveResultImpl(typeParameter, true));
                    }
                }
            }
        }
    }
    return ResolveResult.EMPTY_ARRAY;
}
Also used : ArrayList(java.util.ArrayList) GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) GrParameter(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter) GroovyResolveResultImpl(org.jetbrains.plugins.groovy.lang.psi.impl.GroovyResolveResultImpl) GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) ASTNode(com.intellij.lang.ASTNode) GrTypeParameterListOwner(org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeParameterListOwner) NotNull(org.jetbrains.annotations.NotNull)

Example 89 with GrParameter

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

the class GrNamedArgumentSearchVisitor method find.

public static Map<String, NamedArgumentDescriptor> find(GrVariable variable) {
    final GrExpression initializerGroovy = variable.getInitializerGroovy();
    if (!(initializerGroovy instanceof GrClosableBlock)) {
        return Collections.emptyMap();
    }
    final GrClosableBlock closure = (GrClosableBlock) initializerGroovy;
    final GrParameter[] parameters = closure.getAllParameters();
    if (parameters.length == 0)
        return Collections.emptyMap();
    GrParameter parameter = parameters[0];
    GrNamedArgumentSearchVisitor visitor = new GrNamedArgumentSearchVisitor(parameter.getName());
    closure.accept(visitor);
    return visitor.getResult();
}
Also used : GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GrParameter(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter)

Example 90 with GrParameter

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

the class ControlFlowBuilder method visitClosure.

@Override
public void visitClosure(@NotNull GrClosableBlock closure) {
    //do not go inside closures except gstring injections
    if (closure.getParent() instanceof GrStringInjection) {
        for (GrParameter parameter : closure.getAllParameters()) {
            if (myPolicy.isVariableInitialized(parameter)) {
                addNode(new ReadWriteVariableInstruction(parameter.getName(), parameter, ReadWriteVariableInstruction.WRITE));
            }
        }
        addNode(new ReadWriteVariableInstruction("owner", closure.getLBrace(), ReadWriteVariableInstruction.WRITE));
        super.visitClosure(closure);
        return;
    }
    ReadWriteVariableInstruction[] reads = ControlFlowBuilderUtil.getReadsWithoutPriorWrites(closure.getControlFlow(), false);
    for (ReadWriteVariableInstruction read : reads) {
        PsiElement element = read.getElement();
        if (!(element instanceof GrReferenceExpression) || myPolicy.isReferenceAccepted((GrReferenceExpression) element)) {
            addNodeAndCheckPending(new ReadWriteVariableInstruction(read.getVariableName(), closure, ReadWriteVariableInstruction.READ));
        }
    }
    addNodeAndCheckPending(new InstructionImpl(closure));
}
Also used : GrStringInjection(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrStringInjection) GrParameter(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)

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