Search in sources :

Example 16 with UsageInfo

use of com.intellij.usageView.UsageInfo in project intellij-community by JetBrains.

the class PyMoveFileHandler method findUsages.

@Override
public List<UsageInfo> findUsages(PsiFile file, PsiDirectory newParent, boolean searchInComments, boolean searchInNonJavaFiles) {
    if (file != null) {
        file.putUserData(ORIGINAL_FILE_LOCATION, file.getVirtualFile().getUrl());
        final List<UsageInfo> usages = PyRefactoringUtil.findUsages(file, false);
        for (UsageInfo usage : usages) {
            final PsiElement element = usage.getElement();
            if (element != null) {
                element.putCopyableUserData(REFERENCED_ELEMENT, file);
            }
        }
        return usages;
    }
    return null;
}
Also used : UsageInfo(com.intellij.usageView.UsageInfo)

Example 17 with UsageInfo

use of com.intellij.usageView.UsageInfo in project intellij-community by JetBrains.

the class PyMoveFileHandler method retargetUsages.

@Override
public void retargetUsages(List<UsageInfo> usages, Map<PsiElement, PsiElement> oldToNewMap) {
    final Set<PsiFile> updatedFiles = new HashSet<>();
    for (UsageInfo usage : usages) {
        final PsiElement usageElement = usage.getElement();
        if (usageElement != null) {
            final PsiNamedElement movedElement = usageElement.getCopyableUserData(REFERENCED_ELEMENT);
            usageElement.putCopyableUserData(REFERENCED_ELEMENT, null);
            if (movedElement != null) {
                final PsiFile usageFile = usageElement.getContainingFile();
                final PyImportStatementBase importStmt = PsiTreeUtil.getParentOfType(usageElement, PyImportStatementBase.class);
                // TODO: Retarget qualified expressions in docstrings
                if (importStmt != null) {
                    if (usageFile.getUserData(ORIGINAL_FILE_LOCATION) != null) {
                        // Leave relative imports as they are after #updateRelativeImportsInModule
                        final TypeEvalContext typeEvalContext = TypeEvalContext.userInitiated(usageFile.getProject(), usageFile);
                        final PyResolveContext resolveContext = PyResolveContext.defaultContext().withTypeEvalContext(typeEvalContext);
                        if (ContainerUtil.getFirstItem(PyUtil.multiResolveTopPriority(usageElement, resolveContext)) == movedElement) {
                            continue;
                        }
                    }
                    updatedFiles.add(usageFile);
                    final boolean usageInsideImportElement = PsiTreeUtil.getParentOfType(usageElement, PyImportElement.class) != null;
                    if (usageInsideImportElement) {
                        // or simple unqualified import of the module (import module).
                        if (PyClassRefactoringUtil.updateUnqualifiedImportOfElement(importStmt, movedElement)) {
                            continue;
                        }
                    }
                    final QualifiedName newElementName = QualifiedNameFinder.findCanonicalImportPath(movedElement, usageElement);
                    if (importStmt instanceof PyFromImportStatement) {
                        if (!usageInsideImportElement) {
                            replaceRelativeImportSourceWithQualifiedExpression((PyFromImportStatement) importStmt, newElementName);
                        }
                    } else {
                        replaceWithQualifiedExpression(usageElement, newElementName);
                    }
                } else if (usageElement instanceof PyReferenceExpression) {
                    updatedFiles.add(usageFile);
                    if (((PyReferenceExpression) usageElement).isQualified()) {
                        final QualifiedName newQualifiedName = QualifiedNameFinder.findCanonicalImportPath(movedElement, usageElement);
                        replaceWithQualifiedExpression(usageElement, newQualifiedName);
                    } else {
                        final QualifiedName newName = QualifiedName.fromComponents(PyClassRefactoringUtil.getOriginalName(movedElement));
                        replaceWithQualifiedExpression(usageElement, newName);
                    }
                }
            }
        }
    }
    if (!updatedFiles.isEmpty()) {
        final PyImportOptimizer optimizer = new PyImportOptimizer();
        for (PsiFile file : updatedFiles) {
            final boolean injectedFragment = InjectedLanguageManager.getInstance(file.getProject()).isInjectedFragment(file);
            if (!injectedFragment) {
                optimizer.processFile(file).run();
            }
        }
    }
}
Also used : PyResolveContext(com.jetbrains.python.psi.resolve.PyResolveContext) QualifiedName(com.intellij.psi.util.QualifiedName) TypeEvalContext(com.jetbrains.python.psi.types.TypeEvalContext) PyImportOptimizer(com.jetbrains.python.codeInsight.imports.PyImportOptimizer) UsageInfo(com.intellij.usageView.UsageInfo)

Example 18 with UsageInfo

use of com.intellij.usageView.UsageInfo in project intellij-community by JetBrains.

the class ExtractClosureFromClosureProcessor method findUsages.

@NotNull
@Override
protected UsageInfo[] findUsages() {
    final GrVariable var = (GrVariable) myHelper.getToSearchFor();
    if (var != null) {
        final List<UsageInfo> result = new ArrayList<>();
        for (PsiReference ref : ReferencesSearch.search(var)) {
            final PsiElement element = ref.getElement();
            if (element.getLanguage() != GroovyLanguage.INSTANCE) {
                result.add(new OtherLanguageUsageInfo(ref));
                continue;
            }
            final GrCall call = GroovyRefactoringUtil.getCallExpressionByMethodReference(element);
            if (call == null)
                continue;
            result.add(new ExternalUsageInfo(element));
        }
        return result.toArray(new UsageInfo[result.size()]);
    }
    return UsageInfo.EMPTY_ARRAY;
}
Also used : GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) GrCall(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrCall) ArrayList(java.util.ArrayList) PsiReference(com.intellij.psi.PsiReference) ExternalUsageInfo(com.intellij.refactoring.introduceParameter.ExternalUsageInfo) ExternalUsageInfo(com.intellij.refactoring.introduceParameter.ExternalUsageInfo) UsageInfo(com.intellij.usageView.UsageInfo) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 19 with UsageInfo

use of com.intellij.usageView.UsageInfo in project intellij-community by JetBrains.

the class ExtractClosureFromMethodProcessor method preprocessUsages.

@Override
protected boolean preprocessUsages(@NotNull Ref<UsageInfo[]> refUsages) {
    UsageInfo[] usagesIn = refUsages.get();
    MultiMap<PsiElement, String> conflicts = new MultiMap<>();
    final GrStatement[] statements = myHelper.getStatements();
    for (GrStatement statement : statements) {
        GroovyIntroduceParameterUtil.detectAccessibilityConflicts(statement, usagesIn, conflicts, false, myProject);
    }
    for (UsageInfo info : usagesIn) {
        if (info instanceof OtherLanguageUsageInfo) {
            final String lang = CommonRefactoringUtil.htmlEmphasize(info.getElement().getLanguage().getDisplayName());
            conflicts.putValue(info.getElement(), GroovyRefactoringBundle.message("cannot.process.usage.in.language.{0}", lang));
        }
    }
    if (!myMethod.hasModifierProperty(PsiModifier.PRIVATE)) {
        final AnySupers anySupers = new AnySupers();
        for (GrStatement statement : statements) {
            statement.accept(anySupers);
        }
        if (anySupers.containsSupers()) {
            for (UsageInfo usageInfo : usagesIn) {
                if (!(usageInfo.getElement() instanceof PsiMethod) && !(usageInfo instanceof InternalUsageInfo)) {
                    if (!PsiTreeUtil.isAncestor(myMethod.getContainingClass(), usageInfo.getElement(), false)) {
                        conflicts.putValue(statements[0], RefactoringBundle.message("parameter.initializer.contains.0.but.not.all.calls.to.method.are.in.its.class", CommonRefactoringUtil.htmlEmphasize(PsiKeyword.SUPER)));
                        break;
                    }
                }
            }
        }
    }
    if (!conflicts.isEmpty() && ApplicationManager.getApplication().isUnitTestMode()) {
        throw new ConflictsInTestsException(conflicts.values());
    }
    if (!conflicts.isEmpty()) {
        final ConflictsDialog conflictsDialog = prepareConflictsDialog(conflicts, usagesIn);
        if (!conflictsDialog.showAndGet()) {
            if (conflictsDialog.isShowConflicts())
                prepareSuccessful();
            return false;
        }
    }
    prepareSuccessful();
    return true;
}
Also used : GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement) MultiMap(com.intellij.util.containers.MultiMap) ConflictsDialog(com.intellij.refactoring.ui.ConflictsDialog) UsageInfo(com.intellij.usageView.UsageInfo) NoConstructorClassUsageInfo(com.intellij.refactoring.util.usageInfo.NoConstructorClassUsageInfo) DefaultConstructorImplicitUsageInfo(com.intellij.refactoring.util.usageInfo.DefaultConstructorImplicitUsageInfo) AnySupers(org.jetbrains.plugins.groovy.refactoring.util.AnySupers)

Example 20 with UsageInfo

use of com.intellij.usageView.UsageInfo in project intellij-community by JetBrains.

the class GroovyInlineLocalProcessor method preprocessUsages.

@Override
protected boolean preprocessUsages(@NotNull Ref<UsageInfo[]> refUsages) {
    final MultiMap<PsiElement, String> conflicts = new MultiMap<>();
    final UsageInfo[] usages = refUsages.get();
    for (UsageInfo usage : usages) {
        collectConflicts(usage.getReference(), conflicts);
    }
    return showConflicts(conflicts, usages);
}
Also used : MultiMap(com.intellij.util.containers.MultiMap) PsiElement(com.intellij.psi.PsiElement) UsageInfo(com.intellij.usageView.UsageInfo)

Aggregations

UsageInfo (com.intellij.usageView.UsageInfo)283 PsiElement (com.intellij.psi.PsiElement)70 NotNull (org.jetbrains.annotations.NotNull)57 ArrayList (java.util.ArrayList)41 MoveRenameUsageInfo (com.intellij.refactoring.util.MoveRenameUsageInfo)38 IncorrectOperationException (com.intellij.util.IncorrectOperationException)34 PsiFile (com.intellij.psi.PsiFile)33 MultiMap (com.intellij.util.containers.MultiMap)30 VirtualFile (com.intellij.openapi.vfs.VirtualFile)29 Nullable (org.jetbrains.annotations.Nullable)20 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)19 DefaultConstructorImplicitUsageInfo (com.intellij.refactoring.util.usageInfo.DefaultConstructorImplicitUsageInfo)17 NoConstructorClassUsageInfo (com.intellij.refactoring.util.usageInfo.NoConstructorClassUsageInfo)17 Project (com.intellij.openapi.project.Project)16 HashSet (com.intellij.util.containers.HashSet)15 TextRange (com.intellij.openapi.util.TextRange)12 PsiReference (com.intellij.psi.PsiReference)12 ProgressIndicator (com.intellij.openapi.progress.ProgressIndicator)11 PsiClass (com.intellij.psi.PsiClass)11 LocalSearchScope (com.intellij.psi.search.LocalSearchScope)11