Search in sources :

Example 1 with PyElement

use of com.jetbrains.python.psi.PyElement in project intellij-community by JetBrains.

the class PyClassRefactoringTest method findMember.

/**
   * @param className  class where member should be found
   * @param memberName member that starts with dot (<code>.</code>) is treated as method.
   *                   member that starts with dash (<code>#</code>) is treated as attribute.
   *                   It is treated parent class otherwise
   * @return member or null if not found
   */
@NotNull
protected PyElement findMember(@NotNull final String className, @NotNull String memberName) {
    final PyElement result;
    //TODO: Get rid of this chain of copy pastes
    if (memberName.contains(".")) {
        result = findMethod(className, memberName.substring(1));
    } else if (memberName.contains("#")) {
        result = findField(className, memberName.substring(1));
    } else {
        result = findClass(memberName);
    }
    Assert.assertNotNull(String.format("No member %s found in class %s", memberName, className), result);
    return result;
}
Also used : PyElement(com.jetbrains.python.psi.PyElement) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with PyElement

use of com.jetbrains.python.psi.PyElement in project intellij-community by JetBrains.

the class PyExtractSuperclassPresenterTest method testInvalidSuperClassNameLeadsToError.

/**
   * Checks that presenter displays error if user enters invalid name for new class
   */
public void testInvalidSuperClassNameLeadsToError() {
    final String className = "Child";
    final PyClass aClass = getClassByName(className);
    final List<PyMemberInfo<PyElement>> classMemberInfos = new PyMemberInfoStorage(aClass).getClassMemberInfos(aClass);
    assert !classMemberInfos.isEmpty() : "No member infos for " + className;
    final PyMemberInfo<PyElement> pyMemberInfo = classMemberInfos.get(0);
    EasyMock.expect(myView.getSelectedMemberInfos()).andReturn(Collections.singletonList(pyMemberInfo)).anyTimes();
    EasyMock.expect(myView.getSuperClassName()).andReturn("INVALID CLASS NAME").anyTimes();
    final Capture<String> errorMessageCapture = configureViewToCaptureError();
    final PyExtractSuperclassPresenterImpl sut = configureByClass(className);
    myMocksControl.replay();
    sut.launch();
    sut.okClicked();
    Assert.assertTrue("No error displayed for invalid class name", errorMessageCapture.hasCaptured());
}
Also used : PyClass(com.jetbrains.python.psi.PyClass) PyMemberInfoStorage(com.jetbrains.python.refactoring.classes.PyMemberInfoStorage) PyMemberInfo(com.jetbrains.python.refactoring.classes.membersManager.PyMemberInfo) PyElement(com.jetbrains.python.psi.PyElement)

Example 3 with PyElement

use of com.jetbrains.python.psi.PyElement in project intellij-community by JetBrains.

the class PyExtractSuperclassTest method doSimpleTest.

private void doSimpleTest(final String className, final String superclassName, final String expectedError, final boolean sameFile, boolean asAbstract, final String... membersName) throws Exception {
    try {
        String baseName = "/refactoring/extractsuperclass/" + getTestName(true);
        myFixture.configureByFile(baseName + ".before.py");
        final PyClass clazz = findClass(className);
        final List<PyMemberInfo<PyElement>> members = new ArrayList<>();
        for (String memberName : membersName) {
            final PyElement member = findMember(className, memberName);
            final PyMemberInfo<PyElement> memberInfo = MembersManager.findMember(clazz, member);
            memberInfo.setToAbstract(asAbstract);
            members.add(memberInfo);
        }
        new WriteCommandAction.Simple(myFixture.getProject()) {

            @Override
            protected void run() throws Throwable {
                //noinspection ConstantConditions
                final String url = sameFile ? myFixture.getFile().getVirtualFile().getUrl() : myFixture.getFile().getVirtualFile().getParent().getUrl();
                PyExtractSuperclassHelper.extractSuperclass(clazz, members, superclassName, url);
            }
        }.execute();
        myFixture.checkResultByFile(baseName + ".after.py");
    } catch (Exception e) {
        if (expectedError == null)
            throw e;
        assertEquals(expectedError, e.getMessage());
    }
}
Also used : PyClass(com.jetbrains.python.psi.PyClass) WriteCommandAction(com.intellij.openapi.command.WriteCommandAction) ArrayList(java.util.ArrayList) PyMemberInfo(com.jetbrains.python.refactoring.classes.membersManager.PyMemberInfo) PyElement(com.jetbrains.python.psi.PyElement)

Example 4 with PyElement

use of com.jetbrains.python.psi.PyElement in project intellij-community by JetBrains.

the class PyExtractSuperclassTest method testMultifileAppend.

public void testMultifileAppend() {
    // this is half-copy-paste of testMultifileNew. generalization won't make either easier to follow.
    String baseName = "/refactoring/extractsuperclass/multifile/";
    myFixture.configureByFiles(baseName + "source.py", baseName + "a/__init__.py", baseName + "a/b/__init__.py", baseName + "a/b/foo.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 {
            //TODO: Test via presenter
            //noinspection ConstantConditions
            final String path = base_dir.getPath() + "/a/b";
            PyExtractSuperclassHelper.extractSuperclass(clazz, members, superclassName, path + "/foo.py");
        }
    }.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());
    assertNotNull(vfile.findChild(PyNames.INIT_DOT_PY));
    vfile = vfile.findChild("foo.py");
    assertNotNull(vfile);
    PsiFile psi_file = psi_mgr.findFile(vfile);
    String result = psi_file.getText().trim();
    File expected_file = new File(getTestDataPath() + baseName, "target.append.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 5 with PyElement

use of com.jetbrains.python.psi.PyElement in project intellij-community by JetBrains.

the class PyPullUpTest method doPullUp.

private void doPullUp(final String className, final String superClassName, final boolean toAbstract, final String... memberNames) {
    final PyClass clazz = findClass(className);
    final PyClass superClass = findClass(superClassName);
    final Collection<PyMemberInfo<PyElement>> membersToMove = new ArrayList<>(memberNames.length);
    for (final String memberName : memberNames) {
        final PyElement member = findMember(className, memberName);
        final PyMemberInfo<PyElement> memberInfo = MembersManager.findMember(clazz, member);
        memberInfo.setToAbstract(toAbstract);
        membersToMove.add(memberInfo);
    }
    moveViaProcessor(clazz.getProject(), new PyPullUpProcessor(clazz, superClass, membersToMove));
}
Also used : PyClass(com.jetbrains.python.psi.PyClass) ArrayList(java.util.ArrayList) PyMemberInfo(com.jetbrains.python.refactoring.classes.membersManager.PyMemberInfo) PyElement(com.jetbrains.python.psi.PyElement)

Aggregations

PyElement (com.jetbrains.python.psi.PyElement)21 PyClass (com.jetbrains.python.psi.PyClass)12 PyMemberInfo (com.jetbrains.python.refactoring.classes.membersManager.PyMemberInfo)8 ArrayList (java.util.ArrayList)7 NotNull (org.jetbrains.annotations.NotNull)4 WriteCommandAction (com.intellij.openapi.command.WriteCommandAction)3 PsiElement (com.intellij.psi.PsiElement)3 VirtualFile (com.intellij.openapi.vfs.VirtualFile)2 PsiFile (com.intellij.psi.PsiFile)2 PsiManager (com.intellij.psi.PsiManager)2 MultiMap (com.intellij.util.containers.MultiMap)2 PyFunction (com.jetbrains.python.psi.PyFunction)2 File (java.io.File)2 Nullable (org.jetbrains.annotations.Nullable)2 LocalQuickFixOnPsiElement (com.intellij.codeInspection.LocalQuickFixOnPsiElement)1 HierarchyNodeDescriptor (com.intellij.ide.hierarchy.HierarchyNodeDescriptor)1 AbstractTreeNode (com.intellij.ide.util.treeView.AbstractTreeNode)1 ItemPresentation (com.intellij.navigation.ItemPresentation)1 Project (com.intellij.openapi.project.Project)1 PsiComment (com.intellij.psi.PsiComment)1