use of com.jetbrains.python.refactoring.classes.membersManager.PyMemberInfo in project intellij-community by JetBrains.
the class MembersBasedPresenterImpl method getConflicts.
/**
* Checks if one of destination classes already has members that should be moved, so conflict would take place.
*
* @return map of conflicts (if any)
* @see #getDestClassesToCheckConflicts()
*/
@NotNull
protected final MultiMap<PyClass, PyMemberInfo<?>> getConflicts() {
final MultiMap<PyClass, PyMemberInfo<?>> result = new MultiMap<>();
final Collection<PyMemberInfo<PyElement>> memberInfos = myView.getSelectedMemberInfos();
for (final PyClass destinationClass : getDestClassesToCheckConflicts()) {
for (final PyMemberInfo<PyElement> pyMemberInfo : memberInfos) {
if (pyMemberInfo.hasConflict(destinationClass)) {
result.putValue(destinationClass, pyMemberInfo);
}
}
}
return result;
}
use of com.jetbrains.python.refactoring.classes.membersManager.PyMemberInfo in project intellij-community by JetBrains.
the class PyExtractSuperclassHelper method extractSuperclass.
static void extractSuperclass(final PyClass clazz, @NotNull Collection<PyMemberInfo<PyElement>> selectedMemberInfos, final String superBaseName, final String targetFile) {
final Project project = clazz.getProject();
//We will need to change it probably while param may be read-only
//noinspection AssignmentToMethodParameter
selectedMemberInfos = new ArrayList<>(selectedMemberInfos);
final RefactoringEventData beforeData = new RefactoringEventData();
beforeData.addElements(JBIterable.from(selectedMemberInfos).transform((Function<PyMemberInfo<PyElement>, PsiElement>) info -> info.getMember()).toList());
project.getMessageBus().syncPublisher(RefactoringEventListener.REFACTORING_EVENT_TOPIC).refactoringStarted(getRefactoringId(), beforeData);
// PY-12171
final PyMemberInfo<PyElement> objectMember = MembersManager.findMember(selectedMemberInfos, ALLOW_OBJECT);
if (LanguageLevel.forElement(clazz).isPy3K() && !isObjectParentDeclaredExplicitly(clazz)) {
// Remove object from list if Py3
if (objectMember != null) {
selectedMemberInfos.remove(objectMember);
}
} else {
// Always add object if < Py3
if (objectMember == null) {
final PyMemberInfo<PyElement> object = MembersManager.findMember(clazz, ALLOW_OBJECT);
if (object != null) {
selectedMemberInfos.add(object);
}
}
}
final String text = "class " + superBaseName + ":\n pass" + "\n";
PyClass newClass = PyElementGenerator.getInstance(project).createFromText(LanguageLevel.getDefault(), PyClass.class, text);
newClass = placeNewClass(project, newClass, clazz, targetFile);
MembersManager.moveAllMembers(selectedMemberInfos, clazz, newClass);
if (!newClass.getContainingFile().equals(clazz.getContainingFile())) {
// To remove unneeded imports only if user used different file
PyClassRefactoringUtil.optimizeImports(clazz.getContainingFile());
}
PyClassRefactoringUtil.addSuperclasses(project, clazz, null, newClass);
final RefactoringEventData afterData = new RefactoringEventData();
afterData.addElement(newClass);
project.getMessageBus().syncPublisher(RefactoringEventListener.REFACTORING_EVENT_TOPIC).refactoringDone(getRefactoringId(), afterData);
}
use of com.jetbrains.python.refactoring.classes.membersManager.PyMemberInfo in project intellij-community by JetBrains.
the class PyExtractSuperclassTest method testMultifileNew.
public void testMultifileNew() {
String baseName = "/refactoring/extractsuperclass/multifile/";
myFixture.configureByFile(baseName + "source.py");
final String className = "Foo";
final String superclassName = "Suppa";
final PyClass clazz = findClass(className);
final List<PyMemberInfo<PyElement>> members = new ArrayList<>();
final PyElement member = findMember(className, ".foo");
members.add(MembersManager.findMember(clazz, member));
final VirtualFile base_dir = myFixture.getFile().getVirtualFile().getParent();
new WriteCommandAction.Simple(myFixture.getProject()) {
@Override
protected void run() throws Throwable {
//noinspection ConstantConditions
final String path = base_dir.getPath() + "/a/b";
PyExtractSuperclassHelper.extractSuperclass(clazz, members, superclassName, path);
}
}.execute();
final PsiManager psi_mgr = PsiManager.getInstance(myFixture.getProject());
VirtualFile vfile = base_dir.findChild("a");
assertTrue(vfile.isDirectory());
vfile = vfile.findChild(PyNames.INIT_DOT_PY);
assertNotNull(vfile);
vfile = base_dir.findChild("a").findChild("b");
assertTrue(vfile.isDirectory());
vfile = vfile.findChild(PyNames.INIT_DOT_PY);
assertNotNull(vfile);
PsiFile psi_file = psi_mgr.findFile(vfile);
String result = psi_file.getText().trim();
File expected_file = new File(getTestDataPath() + baseName, "target.new.py");
String expected = psi_mgr.findFile(LocalFileSystem.getInstance().findFileByIoFile(expected_file)).getText().trim();
assertEquals(expected, result);
}
use of com.jetbrains.python.refactoring.classes.membersManager.PyMemberInfo in project intellij-community by JetBrains.
the class PyPullUpPresenterTest method testConflicts.
/**
* Ensures that presenter displays conflicts if destination class already has that members
*/
public void testConflicts() throws Exception {
final PyPullUpPresenterImpl sut = configureByClass("ChildWithConflicts");
configureParent();
final Collection<PyMemberInfo<PyElement>> infos = getMemberInfos(sut);
final Capture<MultiMap<PyClass, PyMemberInfo<?>>> conflictCapture = new Capture<>();
EasyMock.expect(myView.showConflictsDialog(EasyMock.capture(conflictCapture), EasyMock.<Collection<PyMemberInfo<?>>>anyObject())).andReturn(false).anyTimes();
EasyMock.expect(myView.getSelectedMemberInfos()).andReturn(infos).anyTimes();
final PyClass parent = getClassByName("ParentWithConflicts");
EasyMock.expect(myView.getSelectedParent()).andReturn(parent).anyTimes();
myMocksControl.replay();
sut.okClicked();
final MultiMap<PyClass, PyMemberInfo<?>> conflictMap = conflictCapture.getValue();
Assert.assertTrue("No conflicts found, while it should", conflictMap.containsKey(parent));
final Collection<String> conflictedMemberNames = Collections2.transform(conflictMap.get(parent), NameTransformer.INSTANCE);
Assert.assertThat("Failed to find right conflicts", conflictedMemberNames, Matchers.containsInAnyOrder("extends Bar", "CLASS_FIELD", "self.instance_field", "my_func(self)", "__init__(self)"));
}
use of com.jetbrains.python.refactoring.classes.membersManager.PyMemberInfo in project intellij-community by JetBrains.
the class PyPullUpPresenterImpl method isWritable.
private boolean isWritable() {
final Collection<PyMemberInfo<PyElement>> infos = myView.getSelectedMemberInfos();
if (infos.isEmpty()) {
return true;
}
final PyElement element = infos.iterator().next().getMember();
final Project project = element.getProject();
if (!CommonRefactoringUtil.checkReadOnlyStatus(project, myView.getSelectedParent()))
return false;
final PyClass container = PyUtil.getContainingClassOrSelf(element);
if (container == null || !CommonRefactoringUtil.checkReadOnlyStatus(project, container))
return false;
for (final PyMemberInfo<PyElement> info : infos) {
final PyElement member = info.getMember();
if (!CommonRefactoringUtil.checkReadOnlyStatus(project, member))
return false;
}
return true;
}
Aggregations