Search in sources :

Example 16 with PyElement

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

the class MembersManager method getAllDependencies.

/**
   * Returns all elements this member depends on.
   *
   * @param classWhereMemberDeclared class where member declared
   * @param member                   member itself
   * @param destinationClass         where this member would be moved (or null if new class is unknown)
   * @return collection of elements this member depends on excluding those, would be available in destination class
   */
@NotNull
public static Collection<? extends PyElement> getAllDependencies(@NotNull final PyClass classWhereMemberDeclared, @NotNull final PyElement member, @Nullable final PyClass destinationClass) {
    final PyMemberInfo<PyElement> memberInfo = findMember(classWhereMemberDeclared, member);
    final Collection<? extends PyElement> elementsToCheckDependency = memberInfo.getMembersManager().getElementsToStoreReferences(Collections.singleton(member));
    final MultiMap<PyClass, PyElement> dependencies = new MultiMap<>();
    final Collection<PyElement> result = new HashSet<>();
    for (final MembersManager<? extends PyElement> manager : MANAGERS) {
        for (final PyElement elementToCheckDependency : elementsToCheckDependency) {
            dependencies.putAllValues(manager.getDependencies(elementToCheckDependency));
        }
    }
    if (destinationClass != null) {
        final Iterator<PyClass> classesIterator = dependencies.keySet().iterator();
        while (classesIterator.hasNext()) {
            final PyClass memberClass = classesIterator.next();
            if (memberClass.equals(destinationClass) || ArrayUtil.contains(memberClass, destinationClass.getSuperClasses(null))) {
                // IF still would be available
                classesIterator.remove();
            }
        }
    }
    for (final MembersManager<? extends PyElement> manager : MANAGERS) {
        result.addAll(manager.getDependencies(dependencies));
    }
    result.addAll(dependencies.values());
    return result;
}
Also used : PyClass(com.jetbrains.python.psi.PyClass) MultiMap(com.intellij.util.containers.MultiMap) PyElement(com.jetbrains.python.psi.PyElement) NotNull(org.jetbrains.annotations.NotNull)

Example 17 with PyElement

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

the class PyElseUnwrapperBase method doUnwrap.

@Override
protected void doUnwrap(PsiElement element, Context context) throws IncorrectOperationException {
    PyElement elseBranch;
    if (element instanceof PyIfStatement && ((PyIfStatement) element).getElsePart() != null) {
        elseBranch = ((PyIfStatement) element).getElsePart();
    } else {
        elseBranch = (PyElement) element;
    }
    unwrapElseBranch(elseBranch, element.getParent(), context);
}
Also used : PyIfStatement(com.jetbrains.python.psi.PyIfStatement) PyElement(com.jetbrains.python.psi.PyElement)

Example 18 with PyElement

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

the class PyMembersRefactoringSupport method getSelectedMemberInfos.

public static PyMemberInfoStorage getSelectedMemberInfos(PyClass clazz, PsiElement element1, PsiElement element2) {
    final PyMemberInfoStorage infoStorage = new PyMemberInfoStorage(clazz);
    for (PyMemberInfo<PyElement> member : infoStorage.getClassMemberInfos(clazz)) {
        final PyElement function = member.getMember();
        member.setChecked(PsiTreeUtil.isAncestor(function, element1, false) || PsiTreeUtil.isAncestor(function, element2, false));
    }
    return infoStorage;
}
Also used : PyElement(com.jetbrains.python.psi.PyElement)

Example 19 with PyElement

use of com.jetbrains.python.psi.PyElement 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 20 with PyElement

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

the class PydevDocumentationProvider method createDoc.

@Nullable
public static String createDoc(final PsiElement element, final PsiElement originalElement) {
    final PyReferenceExpression expression = PsiTreeUtil.getNonStrictParentOfType(originalElement, PyReferenceExpression.class);
    // Indicates that we are inside console, not a lookup element!
    if (expression == null) {
        return null;
    }
    PydevConsoleReference consoleRef = PyUtil.as(expression.getReference(), PydevConsoleReference.class);
    if (consoleRef == null) {
        //shouldn't really happen!
        return null;
    }
    PyElement documentationElement = consoleRef.getDocumentationElement();
    if (documentationElement == null) {
        return null;
    }
    return new PyDocumentationBuilder(documentationElement, null).build();
}
Also used : PydevConsoleReference(com.jetbrains.python.console.completion.PydevConsoleReference) PyDocumentationBuilder(com.jetbrains.python.documentation.PyDocumentationBuilder) PyReferenceExpression(com.jetbrains.python.psi.PyReferenceExpression) PyElement(com.jetbrains.python.psi.PyElement) Nullable(org.jetbrains.annotations.Nullable)

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