Search in sources :

Example 6 with GroovyPsiElement

use of org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement in project intellij-community by JetBrains.

the class IntroduceVariableValidatorTest method processFile.

private String processFile(String fileText) throws IncorrectOperationException, InvalidDataException, IOException {
    String result = "";
    int startOffset = fileText.indexOf(TestUtils.BEGIN_MARKER);
    if (startOffset < 0) {
        startOffset = fileText.indexOf(ALL_MARKER);
        replaceAllOccurences = true;
        fileText = IntroduceVariableTest.removeAllMarker(fileText);
    } else {
        replaceAllOccurences = false;
        fileText = TestUtils.removeBeginMarker(fileText);
    }
    int endOffset = fileText.indexOf(TestUtils.END_MARKER);
    fileText = TestUtils.removeEndMarker(fileText);
    myFixture.configureByText(GroovyFileType.GROOVY_FILE_TYPE, fileText);
    Editor myEditor = myFixture.getEditor();
    myEditor.getSelectionModel().setSelection(startOffset, endOffset);
    GrExpression selectedExpr = PsiImplUtil.findElementInRange(myFixture.getFile(), startOffset, endOffset, GrExpression.class);
    Assert.assertNotNull("Selected expression reference points to null", selectedExpr);
    final PsiElement tempContainer = GrIntroduceHandlerBase.getEnclosingContainer(selectedExpr);
    Assert.assertTrue(tempContainer instanceof GroovyPsiElement);
    PsiElement[] occurences = GroovyRefactoringUtil.getExpressionOccurrences(PsiUtil.skipParentheses(selectedExpr, false), tempContainer);
    String varName = "preved";
    GroovyVariableValidator validator = new GroovyVariableValidator(new GrIntroduceContextImpl(getProject(), myEditor, selectedExpr, null, null, occurences, tempContainer));
    result = validator.isOKTest(varName, replaceAllOccurences);
    return result;
}
Also used : GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) Editor(com.intellij.openapi.editor.Editor) GroovyVariableValidator(org.jetbrains.plugins.groovy.refactoring.introduce.variable.GroovyVariableValidator) GrIntroduceContextImpl(org.jetbrains.plugins.groovy.refactoring.introduce.GrIntroduceContextImpl) PsiElement(com.intellij.psi.PsiElement) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)

Example 7 with GroovyPsiElement

use of org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement in project intellij-community by JetBrains.

the class GrPullUpConflictsUtil method checkConflicts.

public static MultiMap<PsiElement, String> checkConflicts(final MemberInfoBase<? extends GrMember>[] infos, @NotNull final PsiClass subclass, @Nullable PsiClass superClass, @NotNull final PsiPackage targetPackage, @NotNull PsiDirectory targetDirectory, final InterfaceContainmentVerifier interfaceContainmentVerifier, boolean movedMembers2Super) {
    final PsiElement targetRepresentativeElement;
    final boolean isInterfaceTarget;
    if (superClass != null) {
        isInterfaceTarget = superClass.isInterface();
        targetRepresentativeElement = superClass;
    } else {
        isInterfaceTarget = false;
        targetRepresentativeElement = targetDirectory;
    }
    final Set<GrMember> movedMembers = ContainerUtil.newHashSet();
    final Set<GrMethod> abstractMethods = ContainerUtil.newHashSet();
    for (MemberInfoBase<? extends GrMember> info : infos) {
        GrMember member = info.getMember();
        if (member instanceof GrMethod) {
            if (!info.isToAbstract() && !isInterfaceTarget) {
                movedMembers.add(member);
            } else {
                abstractMethods.add((GrMethod) member);
            }
        } else {
            movedMembers.add(member);
        }
    }
    final Set<PsiMethod> allAbstractMethods = new HashSet<>(abstractMethods);
    if (superClass != null) {
        for (PsiMethod method : subclass.getMethods()) {
            if (!movedMembers.contains(method) && !method.hasModifierProperty(PsiModifier.PRIVATE)) {
                if (method.findSuperMethods(superClass).length > 0) {
                    allAbstractMethods.add(method);
                }
            }
        }
    }
    final MultiMap<PsiElement, String> conflicts = new MultiMap<>();
    GrRefactoringConflictsUtil.analyzeAccessibilityConflicts(movedMembers, superClass, conflicts, VisibilityUtil.ESCALATE_VISIBILITY, targetRepresentativeElement, allAbstractMethods);
    if (superClass != null) {
        if (movedMembers2Super) {
            checkSuperclassMembers(superClass, infos, conflicts);
            if (isInterfaceTarget) {
                checkInterfaceTarget(infos, conflicts);
            }
        } else {
            final String qualifiedName = superClass.getQualifiedName();
            assert qualifiedName != null;
            if (superClass.hasModifierProperty(PsiModifier.PACKAGE_LOCAL)) {
                if (!Comparing.strEqual(StringUtil.getPackageName(qualifiedName), targetPackage.getQualifiedName())) {
                    conflicts.putValue(superClass, RefactoringUIUtil.getDescription(superClass, true) + " won't be accessible from " + RefactoringUIUtil.getDescription(targetPackage, true));
                }
            }
        }
    }
    // check if moved methods use other members in the classes between Subclass and Superclass
    List<PsiElement> checkModuleConflictsList = new ArrayList<>();
    for (PsiMember member : movedMembers) {
        if (member instanceof PsiMethod || member instanceof PsiClass && !(member instanceof PsiCompiledElement)) {
            GrClassMemberReferenceVisitor visitor = movedMembers2Super ? new ConflictingUsagesOfSubClassMembers(member, movedMembers, abstractMethods, subclass, superClass, superClass != null ? null : targetPackage, conflicts, interfaceContainmentVerifier) : new ConflictingUsagesOfSuperClassMembers(member, subclass, targetPackage, movedMembers, conflicts);
            ((GroovyPsiElement) member).accept(visitor);
        }
        checkModuleConflictsList.add(member);
    }
    for (final PsiMethod method : abstractMethods) {
        ContainerUtil.addIfNotNull(checkModuleConflictsList, method.getParameterList());
        ContainerUtil.addIfNotNull(checkModuleConflictsList, method.getReturnTypeElement());
        ContainerUtil.addIfNotNull(checkModuleConflictsList, method.getTypeParameterList());
    }
    GrRefactoringConflictsUtil.analyzeModuleConflicts(subclass.getProject(), checkModuleConflictsList, UsageInfo.EMPTY_ARRAY, targetRepresentativeElement, conflicts);
    final String fqName = subclass.getQualifiedName();
    final String packageName;
    if (fqName != null) {
        packageName = StringUtil.getPackageName(fqName);
    } else {
        final PsiFile psiFile = PsiTreeUtil.getParentOfType(subclass, PsiFile.class);
        if (psiFile instanceof PsiClassOwner) {
            packageName = ((PsiClassOwner) psiFile).getPackageName();
        } else {
            packageName = null;
        }
    }
    final boolean toDifferentPackage = !Comparing.strEqual(targetPackage.getQualifiedName(), packageName);
    for (final GrMethod abstractMethod : abstractMethods) {
        abstractMethod.accept(new GrClassMemberReferenceVisitor(subclass) {

            @Override
            protected void visitClassMemberReferenceElement(GrMember classMember, GrReferenceElement classMemberReference) {
                if (classMember != null && willBeMoved(classMember, movedMembers)) {
                    boolean isAccessible = false;
                    if (classMember.hasModifierProperty(PsiModifier.PRIVATE)) {
                        isAccessible = true;
                    } else if (classMember.hasModifierProperty(PsiModifier.PACKAGE_LOCAL) && toDifferentPackage) {
                        isAccessible = true;
                    }
                    if (isAccessible) {
                        String message = RefactoringUIUtil.getDescription(abstractMethod, false) + " uses " + RefactoringUIUtil.getDescription(classMember, true) + " which won't be accessible from the subclass.";
                        message = CommonRefactoringUtil.capitalize(message);
                        conflicts.putValue(classMember, message);
                    }
                }
            }
        });
        if (abstractMethod.hasModifierProperty(PsiModifier.PACKAGE_LOCAL) && toDifferentPackage) {
            if (!isInterfaceTarget) {
                String message = "Can't make " + RefactoringUIUtil.getDescription(abstractMethod, false) + " abstract as it won't be accessible from the subclass.";
                message = CommonRefactoringUtil.capitalize(message);
                conflicts.putValue(abstractMethod, message);
            }
        }
    }
    return conflicts;
}
Also used : GrClassMemberReferenceVisitor(org.jetbrains.plugins.groovy.refactoring.classMembers.GrClassMemberReferenceVisitor) ArrayList(java.util.ArrayList) MultiMap(com.intellij.util.containers.MultiMap) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) GrReferenceElement(org.jetbrains.plugins.groovy.lang.psi.GrReferenceElement) HashSet(java.util.HashSet) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) GrMember(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMember) GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod)

Example 8 with GroovyPsiElement

use of org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement in project intellij-community by JetBrains.

the class GrPullUpHelper method postProcessMember.

@Override
public void postProcessMember(PsiMember member) {
    ((GrMember) member).accept(myExplicitSuperDeleter);
    ((GrMember) member).accept(myThisSuperAdjuster);
    GroovyChangeContextUtil.decodeContextInfo(member, null, null);
    ((GroovyPsiElement) member).accept(new GroovyRecursiveElementVisitor() {

        @Override
        public void visitReferenceExpression(@NotNull GrReferenceExpression referenceExpression) {
            if (processRef(referenceExpression))
                return;
            super.visitReferenceExpression(referenceExpression);
        }

        @Override
        public void visitCodeReferenceElement(@NotNull GrCodeReferenceElement refElement) {
            if (processRef(refElement))
                return;
            super.visitCodeReferenceElement(refElement);
        }

        private boolean processRef(@NotNull GrReferenceElement<? extends GroovyPsiElement> refElement) {
            final PsiElement qualifier = refElement.getQualifier();
            if (qualifier != null) {
                final Boolean preserveQualifier = qualifier.getCopyableUserData(PRESERVE_QUALIFIER);
                if (preserveQualifier != null && !preserveQualifier) {
                    refElement.setQualifier(null);
                    return true;
                }
            }
            return false;
        }
    });
}
Also used : GrCodeReferenceElement(org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) GrMember(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMember) GroovyRecursiveElementVisitor(org.jetbrains.plugins.groovy.lang.psi.GroovyRecursiveElementVisitor) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)

Example 9 with GroovyPsiElement

use of org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement in project intellij-community by JetBrains.

the class GrBracesSurrounder method doSurroundElements.

@Override
protected GroovyPsiElement doSurroundElements(PsiElement[] elements, PsiElement context) throws IncorrectOperationException {
    GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(elements[0].getProject());
    final PsiElement e0 = elements[0];
    final PsiElement parent = e0.getParent();
    final GrCodeBlock block;
    if (parent instanceof GrControlStatement) {
        block = factory.createMethodBodyFromText("\n");
        final PsiElement prev = e0.getPrevSibling();
        if (prev != null && prev.getNode().getElementType().equals(GroovyTokenTypes.mNLS)) {
            final ASTNode parentNode = e0.getParent().getNode();
            parentNode.addLeaf(TokenType.WHITE_SPACE, " ", prev.getNode());
            parentNode.removeChild(prev.getNode());
        }
    } else {
        block = factory.createClosureFromText("{}");
    }
    GroovyManyStatementsSurrounder.addStatements(block, elements);
    return block;
}
Also used : GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) ASTNode(com.intellij.lang.ASTNode) GrControlStatement(org.jetbrains.plugins.groovy.lang.psi.api.formatter.GrControlStatement) PsiElement(com.intellij.psi.PsiElement) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) GrCodeBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrCodeBlock)

Example 10 with GroovyPsiElement

use of org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement in project intellij-community by JetBrains.

the class IfSurrounder method getSurroundSelectionRange.

@Override
protected TextRange getSurroundSelectionRange(GroovyPsiElement element) {
    assert element instanceof GrIfStatement;
    GrCondition condition = ((GrIfStatement) element).getCondition();
    int endOffset = element.getTextRange().getEndOffset();
    if (condition != null) {
        PsiElement child = condition.getFirstChild();
        assert child != null;
        endOffset = child.getTextRange().getStartOffset();
        condition.getParent().getNode().removeChild(condition.getNode());
    }
    return new TextRange(endOffset, endOffset);
}
Also used : GrIfStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrIfStatement) TextRange(com.intellij.openapi.util.TextRange) GrCondition(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrCondition) PsiElement(com.intellij.psi.PsiElement) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)

Aggregations

GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)98 PsiElement (com.intellij.psi.PsiElement)34 Nullable (org.jetbrains.annotations.Nullable)17 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)17 GroovyPsiElementFactory (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)16 GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)13 GrArgumentList (org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList)12 GrClosableBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock)11 GrMethod (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod)9 GrListOrMap (org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrListOrMap)8 GrNamedArgument (org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrNamedArgument)8 GrApplicationStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrApplicationStatement)8 GrMethodCallExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression)8 GrTypeDefinition (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition)8 Project (com.intellij.openapi.project.Project)7 TextRange (com.intellij.openapi.util.TextRange)7 NotNull (org.jetbrains.annotations.NotNull)7 GroovyRecursiveElementVisitor (org.jetbrains.plugins.groovy.lang.psi.GroovyRecursiveElementVisitor)7 GrVariable (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable)7 GroovyResolveResult (org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult)6