Search in sources :

Example 6 with PyElement

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

the class PyPushDownTest method doProcessorTest.

private void doProcessorTest(final String className, final String expectedError, final String... memberNames) throws Exception {
    try {
        String baseName = "/refactoring/pushdown/" + getTestName(true);
        myFixture.configureByFile(baseName + ".before.py");
        final PyClass clazz = findClass(className);
        final List<PyMemberInfo<PyElement>> members = new ArrayList<>();
        for (String memberName : memberNames) {
            final PyElement member = findMember(className, memberName);
            members.add(MembersManager.findMember(clazz, member));
        }
        final PyPushDownProcessor processor = new PyPushDownProcessor(myFixture.getProject(), members, clazz);
        moveViaProcessor(myFixture.getProject(), processor);
        myFixture.checkResultByFile(baseName + ".after.py");
    } catch (Exception e) {
        if (expectedError == null)
            throw e;
        assertTrue(e.getMessage(), e.getMessage().contains(expectedError));
    }
}
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)

Example 7 with PyElement

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

the class PyPushDownTest method testMultiFileImports.

// Tests that pushing down methods moves imports as well (PY-10963)
public void testMultiFileImports() {
    final String[] modules = { "child_module", "parent_module" };
    configureMultiFile(ArrayUtil.mergeArrays(modules, "shared_module"));
    final PyClass parentClass = findClass("Parent");
    final PyMemberInfo<PyElement> methodToMove = MembersManager.findMember(parentClass, findMember("Parent", ".should_be_pushed"));
    moveViaProcessor(myFixture.getProject(), new PyPushDownProcessor(myFixture.getProject(), Collections.singletonList(methodToMove), parentClass));
    checkMultiFile(modules);
}
Also used : PyClass(com.jetbrains.python.psi.PyClass) PyElement(com.jetbrains.python.psi.PyElement)

Example 8 with PyElement

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

the class AutoImportQuickFix method showHint.

public boolean showHint(Editor editor) {
    if (!PyCodeInsightSettings.getInstance().SHOW_IMPORT_POPUP || HintManager.getInstance().hasShownHintsThatWillHideByOtherHint(true) || myImports.isEmpty()) {
        return false;
    }
    final PsiElement element = getStartElement();
    PyPsiUtils.assertValid(element);
    if (element == null || !element.isValid()) {
        return false;
    }
    final PyElement pyElement = as(element, PyElement.class);
    if (pyElement == null || !myInitialName.equals(pyElement.getName())) {
        return false;
    }
    final PsiReference reference = findOriginalReference(element);
    if (reference == null || isResolved(reference)) {
        return false;
    }
    if (element instanceof PyQualifiedExpression && ((PyQualifiedExpression) element).isQualified()) {
        // we cannot be qualified
        return false;
    }
    final String message = ShowAutoImportPass.getMessage(myImports.size() > 1, ImportCandidateHolder.getQualifiedName(myInitialName, myImports.get(0).getPath(), myImports.get(0).getImportElement()));
    final ImportFromExistingAction action = new ImportFromExistingAction(element, myImports, myInitialName, myUseQualifiedImport, false);
    action.onDone(() -> myExpended = true);
    HintManager.getInstance().showQuestionHint(editor, message, element.getTextOffset(), element.getTextRange().getEndOffset(), action);
    return true;
}
Also used : PyQualifiedExpression(com.jetbrains.python.psi.PyQualifiedExpression) LocalQuickFixOnPsiElement(com.intellij.codeInspection.LocalQuickFixOnPsiElement) PyElement(com.jetbrains.python.psi.PyElement)

Example 9 with PyElement

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

the class TypeSafeMovingStrategy method moveTyped.

/**
   * While types are already checked at runtime, this method could move everything in type-safe manner.
   */
private void moveTyped() {
    final Collection<T> elementsCollection = MembersManager.fetchElements(myMemberInfoCollection);
    final Collection<? extends PyElement> references = myManager.getElementsToStoreReferences(elementsCollection);
    // Store references to add required imports
    for (final PyElement element : references) {
        //"self" is not reference we need to move
        PyClassRefactoringUtil.rememberNamedReferences(element, PyNames.CANONICAL_SELF);
    }
    // Move
    final Collection<PyElement> newElements = myManager.moveMembers(myFrom, myMemberInfoCollection, myTo);
    // Restore references to add appropriate imports
    for (final PyElement element : newElements) {
        PyClassRefactoringUtil.restoreNamedReferences(element);
    }
}
Also used : PyElement(com.jetbrains.python.psi.PyElement)

Example 10 with PyElement

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

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