Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,62 +15,68 @@
*/
package com.jetbrains.python.impl.refactoring.classes.membersManager;

import java.util.Collection;

import jakarta.annotation.Nonnull;

import com.jetbrains.python.psi.PyClass;
import consulo.language.editor.refactoring.localize.RefactoringLocalize;
import consulo.language.editor.refactoring.ui.ConflictsDialog;
import consulo.language.editor.refactoring.ui.RefactoringUIUtil;
import consulo.project.Project;
import consulo.language.psi.PsiElement;
import consulo.language.editor.refactoring.RefactoringBundle;
import consulo.localize.LocalizeValue;
import consulo.project.Project;
import consulo.python.impl.localize.PyLocalize;
import consulo.util.collection.MultiMap;
import com.jetbrains.python.impl.PyBundle;
import com.jetbrains.python.psi.PyClass;
import jakarta.annotation.Nonnull;

import java.util.Collection;

/**
* Displays error messages about fact that destination class already contains some member infos
* or members under refactoring would not be available at the new place.
*
* @author Ilya.Kazakevich
*/
public class MembersConflictDialog extends ConflictsDialog
{
/**
* @param project project under refactoring
* @param duplicatesConflict duplicates conflicts : that means destination class has the same member.
* If member "foo" already exists in class "bar": pass [bar] -] [foo].
* @param dependenciesConflicts dependency conflict: list of elements used by member under refactoring and would not be available
* at new destination. If user wants to move method, that uses field "bar" which would not be available at new class,
* pass [bar] field
*/
public MembersConflictDialog(@Nonnull final Project project, @Nonnull final MultiMap<PyClass, PyMemberInfo<?>> duplicatesConflict, @Nonnull final Collection<PyMemberInfo<?>>
dependenciesConflicts)
{
super(project, convertDescription(duplicatesConflict, dependenciesConflicts), null, true, false);
}

@Nonnull
private static MultiMap<PsiElement, String> convertDescription(@Nonnull final MultiMap<PyClass, PyMemberInfo<?>> duplicateConflictDescriptions,
@Nonnull final Collection<PyMemberInfo<?>> dependenciesConflicts)
{
final MultiMap<PsiElement, String> result = new MultiMap<>();
for(final PyClass aClass : duplicateConflictDescriptions.keySet())
{
for(final PyMemberInfo<?> pyMemberInfo : duplicateConflictDescriptions.get(aClass))
{
final String message = RefactoringBundle.message("0.already.contains.a.1", RefactoringUIUtil.getDescription(aClass, false), RefactoringUIUtil.getDescription(pyMemberInfo.getMember(),
false));
result.putValue(aClass, message);
}
}
public class MembersConflictDialog extends ConflictsDialog {
/**
* @param project project under refactoring
* @param duplicatesConflict duplicates conflicts : that means destination class has the same member.
* If member "foo" already exists in class "bar": pass [bar] -] [foo].
* @param dependenciesConflicts dependency conflict: list of elements used by member under refactoring and would not be available
* at new destination. If user wants to move method, that uses field "bar" which would not be available at new class,
* pass [bar] field
*/
public MembersConflictDialog(
@Nonnull Project project,
@Nonnull MultiMap<PyClass, PyMemberInfo<?>> duplicatesConflict,
@Nonnull Collection<PyMemberInfo<?>> dependenciesConflicts
) {
super(project, convertDescription(duplicatesConflict, dependenciesConflicts), null, true, false);
}

for(final PyMemberInfo<?> memberUnderConflict : dependenciesConflicts)
{
result.putValue(memberUnderConflict.getMember(), PyBundle.message("refactoring.will.not.be.accessible", RefactoringUIUtil.getDescription(memberUnderConflict.getMember(), false)));
}
@Nonnull
private static MultiMap<PsiElement, LocalizeValue> convertDescription(
@Nonnull MultiMap<PyClass, PyMemberInfo<?>> duplicateConflictDescriptions,
@Nonnull Collection<PyMemberInfo<?>> dependenciesConflicts
) {
MultiMap<PsiElement, LocalizeValue> result = new MultiMap<>();
for (PyClass aClass : duplicateConflictDescriptions.keySet()) {
for (PyMemberInfo<?> pyMemberInfo : duplicateConflictDescriptions.get(aClass)) {
LocalizeValue message = RefactoringLocalize.zeroAlreadyContainsA1(
RefactoringUIUtil.getDescription(aClass, false),
RefactoringUIUtil.getDescription(
pyMemberInfo.getMember(),
false
)
);
result.putValue(aClass, message);
}
}

for (PyMemberInfo<?> memberUnderConflict : dependenciesConflicts) {
result.putValue(
memberUnderConflict.getMember(),
PyLocalize.refactoringWillNotBeAccessible(RefactoringUIUtil.getDescription(memberUnderConflict.getMember(), false))
);
}

return result;
}
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@
*/
package com.jetbrains.python.impl.refactoring.rename;

import consulo.annotation.access.RequiredReadAction;
import consulo.language.editor.refactoring.rename.RenamePsiElementProcessor;
import consulo.language.psi.PsiElement;
import consulo.language.psi.util.PsiTreeUtil;
import consulo.localize.LocalizeValue;
import consulo.util.collection.MultiMap;
import com.jetbrains.python.codeInsight.controlflow.ScopeOwner;
import com.jetbrains.python.psi.PyClass;
Expand All @@ -29,55 +31,62 @@
/**
* @author yole
*/
public abstract class RenamePyElementProcessor extends RenamePsiElementProcessor
{
@Override
public void findExistingNameConflicts(PsiElement element, String newName, MultiMap<PsiElement, String> conflicts)
{
PyElement container = PsiTreeUtil.getParentOfType(element, ScopeOwner.class);
if(container instanceof PyFile)
{
PyFile pyFile = (PyFile) container;
PyClass conflictingClass = pyFile.findTopLevelClass(newName);
if(conflictingClass != null)
{
conflicts.putValue(conflictingClass, "A class named '" + newName + "' is already defined in " + pyFile.getName());
}
PyFunction conflictingFunction = pyFile.findTopLevelFunction(newName);
if(conflictingFunction != null)
{
conflicts.putValue(conflictingFunction, "A function named '" + newName + "' is already defined in " + pyFile.getName());
}
PyTargetExpression conflictingVariable = pyFile.findTopLevelAttribute(newName);
if(conflictingVariable != null)
{
conflicts.putValue(conflictingFunction, "A variable named '" + newName + "' is already defined in " + pyFile.getName());
}
}
else if(container instanceof PyClass)
{
PyClass pyClass = (PyClass) container;
PyClass conflictingClass = pyClass.findNestedClass(newName, true);
if(conflictingClass != null)
{
conflicts.putValue(conflictingClass, "A class named '" + newName + "' is already defined in class '" + pyClass.getName() + "'");
}
PyFunction conflictingFunction = pyClass.findMethodByName(newName, true, null);
if(conflictingFunction != null)
{
conflicts.putValue(conflictingFunction, "A function named '" + newName + "' is already defined in class '" + pyClass.getName() + "'");
}
PyTargetExpression conflictingAttribute = pyClass.findClassAttribute(newName, true, null);
if(conflictingAttribute != null)
{
conflicts.putValue(conflictingAttribute, "An attribute named '" + newName + "' is already defined in class '" + pyClass.getName() + "'");
}
}
}
public abstract class RenamePyElementProcessor extends RenamePsiElementProcessor {
@Override
@RequiredReadAction
public void findExistingNameConflicts(PsiElement element, String newName, MultiMap<PsiElement, LocalizeValue> conflicts) {
PyElement container = PsiTreeUtil.getParentOfType(element, ScopeOwner.class);
if (container instanceof PyFile pyFile) {
PyClass conflictingClass = pyFile.findTopLevelClass(newName);
if (conflictingClass != null) {
conflicts.putValue(
conflictingClass,
LocalizeValue.localizeTODO("A class named '" + newName + "' is already defined in " + pyFile.getName())
);
}
PyFunction conflictingFunction = pyFile.findTopLevelFunction(newName);
if (conflictingFunction != null) {
conflicts.putValue(
conflictingFunction,
LocalizeValue.localizeTODO("A function named '" + newName + "' is already defined in " + pyFile.getName())
);
}
PyTargetExpression conflictingVariable = pyFile.findTopLevelAttribute(newName);
if (conflictingVariable != null) {
conflicts.putValue(
conflictingFunction,
LocalizeValue.localizeTODO("A variable named '" + newName + "' is already defined in " + pyFile.getName())
);
}
}
else if (container instanceof PyClass) {
PyClass pyClass = (PyClass) container;
PyClass conflictingClass = pyClass.findNestedClass(newName, true);
if (conflictingClass != null) {
conflicts.putValue(
conflictingClass,
LocalizeValue.localizeTODO("A class named '" + newName + "' is already defined in class '" + pyClass.getName() + "'")
);
}
PyFunction conflictingFunction = pyClass.findMethodByName(newName, true, null);
if (conflictingFunction != null) {
conflicts.putValue(
conflictingFunction,
LocalizeValue.localizeTODO("A function named '" + newName + "' is already defined in class '" + pyClass.getName() + "'")
);
}
PyTargetExpression conflictingAttribute = pyClass.findClassAttribute(newName, true, null);
if (conflictingAttribute != null) {
conflicts.putValue(
conflictingAttribute,
LocalizeValue.localizeTODO("An attribute named '" + newName + "' is already defined in class '" + pyClass.getName() + "'")
);
}
}
}

@Override
public String getHelpID(PsiElement element)
{
return "python.reference.rename";
}
@Override
public String getHelpID(PsiElement element) {
return "python.reference.rename";
}
}
Loading