Search in sources :

Example 6 with PyMemberInfo

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;
}
Also used : PyClass(com.jetbrains.python.psi.PyClass) MultiMap(com.intellij.util.containers.MultiMap) PyMemberInfo(com.jetbrains.python.refactoring.classes.membersManager.PyMemberInfo) PyElement(com.jetbrains.python.psi.PyElement) NotNull(org.jetbrains.annotations.NotNull)

Example 7 with PyMemberInfo

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);
}
Also used : PyNames(com.jetbrains.python.PyNames) Arrays(java.util.Arrays) JBIterable(com.intellij.util.containers.JBIterable) VirtualFile(com.intellij.openapi.vfs.VirtualFile) Document(com.intellij.openapi.editor.Document) RefactoringEventListener(com.intellij.refactoring.listeners.RefactoringEventListener) VirtualFileManager(com.intellij.openapi.vfs.VirtualFileManager) ArrayList(java.util.ArrayList) PyClassRefactoringUtil(com.jetbrains.python.refactoring.classes.PyClassRefactoringUtil) Comparing(com.intellij.openapi.util.Comparing) Project(com.intellij.openapi.project.Project) com.jetbrains.python.psi(com.jetbrains.python.psi) FileUtil(com.intellij.openapi.util.io.FileUtil) RefactoringEventData(com.intellij.refactoring.listeners.RefactoringEventData) Logger(com.intellij.openapi.diagnostic.Logger) ProjectRootManager(com.intellij.openapi.roots.ProjectRootManager) PythonFileType(com.jetbrains.python.PythonFileType) VfsUtilCore(com.intellij.openapi.vfs.VfsUtilCore) Collection(java.util.Collection) IOException(java.io.IOException) LocalFileSystem(com.intellij.openapi.vfs.LocalFileSystem) MembersManager(com.jetbrains.python.refactoring.classes.membersManager.MembersManager) Nullable(org.jetbrains.annotations.Nullable) Predicate(com.google.common.base.Predicate) Function(com.intellij.util.Function) ApplicationManagerEx(com.intellij.openapi.application.ex.ApplicationManagerEx) com.intellij.psi(com.intellij.psi) PathUtil(com.intellij.util.PathUtil) NotNull(org.jetbrains.annotations.NotNull) Collections(java.util.Collections) PyMemberInfo(com.jetbrains.python.refactoring.classes.membersManager.PyMemberInfo) Project(com.intellij.openapi.project.Project) RefactoringEventData(com.intellij.refactoring.listeners.RefactoringEventData) PyMemberInfo(com.jetbrains.python.refactoring.classes.membersManager.PyMemberInfo)

Example 8 with PyMemberInfo

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);
}
Also used : PyClass(com.jetbrains.python.psi.PyClass) VirtualFile(com.intellij.openapi.vfs.VirtualFile) WriteCommandAction(com.intellij.openapi.command.WriteCommandAction) ArrayList(java.util.ArrayList) PyMemberInfo(com.jetbrains.python.refactoring.classes.membersManager.PyMemberInfo) PsiManager(com.intellij.psi.PsiManager) PsiFile(com.intellij.psi.PsiFile) VirtualFile(com.intellij.openapi.vfs.VirtualFile) File(java.io.File) PsiFile(com.intellij.psi.PsiFile) PyElement(com.jetbrains.python.psi.PyElement)

Example 9 with PyMemberInfo

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)"));
}
Also used : PyClass(com.jetbrains.python.psi.PyClass) MultiMap(com.intellij.util.containers.MultiMap) PyMemberInfo(com.jetbrains.python.refactoring.classes.membersManager.PyMemberInfo) Collection(java.util.Collection) Capture(org.easymock.Capture)

Example 10 with PyMemberInfo

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;
}
Also used : PyClass(com.jetbrains.python.psi.PyClass) Project(com.intellij.openapi.project.Project) PyMemberInfo(com.jetbrains.python.refactoring.classes.membersManager.PyMemberInfo) PyElement(com.jetbrains.python.psi.PyElement)

Aggregations

PyMemberInfo (com.jetbrains.python.refactoring.classes.membersManager.PyMemberInfo)10 PyClass (com.jetbrains.python.psi.PyClass)9 PyElement (com.jetbrains.python.psi.PyElement)8 ArrayList (java.util.ArrayList)6 WriteCommandAction (com.intellij.openapi.command.WriteCommandAction)3 VirtualFile (com.intellij.openapi.vfs.VirtualFile)3 Project (com.intellij.openapi.project.Project)2 PsiFile (com.intellij.psi.PsiFile)2 PsiManager (com.intellij.psi.PsiManager)2 MultiMap (com.intellij.util.containers.MultiMap)2 File (java.io.File)2 Collection (java.util.Collection)2 NotNull (org.jetbrains.annotations.NotNull)2 Predicate (com.google.common.base.Predicate)1 ApplicationManagerEx (com.intellij.openapi.application.ex.ApplicationManagerEx)1 Logger (com.intellij.openapi.diagnostic.Logger)1 Document (com.intellij.openapi.editor.Document)1 ProjectRootManager (com.intellij.openapi.roots.ProjectRootManager)1 Comparing (com.intellij.openapi.util.Comparing)1 FileUtil (com.intellij.openapi.util.io.FileUtil)1