Search in sources :

Example 6 with PyTargetExpression

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

the class PyAssignmentMappingTest method testTupleMapped.

public void testTupleMapped() throws Exception {
    Map<String, PsiElement> marks = loadTest();
    final int PAIR_NUM = 2;
    Assert.assertEquals(PAIR_NUM * 2, marks.size());
    PsiElement[] srcs = new PsiElement[PAIR_NUM];
    PsiElement[] dsts = new PsiElement[PAIR_NUM];
    for (int i = 0; i < PAIR_NUM; i += 1) {
        // ident -> target expr
        PsiElement dst = marks.get("<dst" + String.valueOf(i + 1) + ">").getParent();
        Assert.assertTrue(dst instanceof PyTargetExpression);
        dsts[i] = dst;
        // ident -> target expr
        PsiElement src = marks.get("<src" + String.valueOf(i + 1) + ">").getParent();
        Assert.assertTrue(src instanceof PyExpression);
        srcs[i] = src;
    }
    // tuple expr -> assignment
    PyAssignmentStatement stmt = (PyAssignmentStatement) srcs[0].getParent().getParent();
    List<Pair<PyExpression, PyExpression>> mapping = stmt.getTargetsToValuesMapping();
    Assert.assertEquals(PAIR_NUM, mapping.size());
    for (int i = 0; i < PAIR_NUM; i += 1) {
        Pair<PyExpression, PyExpression> pair = mapping.get(i);
        Assert.assertEquals(dsts[i], pair.getFirst());
        Assert.assertEquals(srcs[i], pair.getSecond());
    }
}
Also used : PyTargetExpression(com.jetbrains.python.psi.PyTargetExpression) PyExpression(com.jetbrains.python.psi.PyExpression) PyAssignmentStatement(com.jetbrains.python.psi.PyAssignmentStatement) PsiElement(com.intellij.psi.PsiElement) Pair(com.intellij.openapi.util.Pair)

Example 7 with PyTargetExpression

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

the class PyAssignmentMappingTest method testParenthesizedTuple.

public void testParenthesizedTuple() throws Exception {
    //PY-2648
    Map<String, PsiElement> marks = loadTest();
    final int PAIR_NUM = 2;
    Assert.assertEquals(PAIR_NUM * 2, marks.size());
    PsiElement[] srcs = new PsiElement[PAIR_NUM];
    PsiElement[] dsts = new PsiElement[PAIR_NUM];
    for (int i = 0; i < PAIR_NUM; i += 1) {
        // ident -> target expr
        PsiElement dst = marks.get("<dst" + String.valueOf(i + 1) + ">").getParent();
        Assert.assertTrue(dst instanceof PyTargetExpression);
        dsts[i] = dst;
        // ident -> target expr
        PsiElement src = marks.get("<src" + String.valueOf(i + 1) + ">").getParent();
        Assert.assertTrue(src instanceof PyExpression);
        srcs[i] = src;
    }
    // tuple expr -> assignment
    PyAssignmentStatement stmt = (PyAssignmentStatement) srcs[0].getParent().getParent().getParent();
    List<Pair<PyExpression, PyExpression>> mapping = stmt.getTargetsToValuesMapping();
    Assert.assertEquals(PAIR_NUM, mapping.size());
    for (int i = 0; i < PAIR_NUM; i += 1) {
        Pair<PyExpression, PyExpression> pair = mapping.get(i);
        Assert.assertEquals(dsts[i], pair.getFirst());
        Assert.assertEquals(srcs[i], pair.getSecond());
    }
}
Also used : PyTargetExpression(com.jetbrains.python.psi.PyTargetExpression) PyExpression(com.jetbrains.python.psi.PyExpression) PyAssignmentStatement(com.jetbrains.python.psi.PyAssignmentStatement) PsiElement(com.intellij.psi.PsiElement) Pair(com.intellij.openapi.util.Pair)

Example 8 with PyTargetExpression

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

the class PyAssignmentMappingTest method testTupleUnpack.

public void testTupleUnpack() throws Exception {
    Map<String, PsiElement> marks = loadTest();
    final int DST_NUM = 2;
    Assert.assertEquals(DST_NUM + 3, marks.size());
    PsiElement[] dsts = new PsiElement[DST_NUM];
    for (int i = 0; i < DST_NUM; i += 1) {
        // ident -> target expr
        PsiElement dst = marks.get("<dst" + String.valueOf(i + 1) + ">").getParent();
        Assert.assertTrue(dst instanceof PyTargetExpression);
        dsts[i] = dst;
    }
    PsiElement[] srcs = new PsiElement[DST_NUM];
    for (int i = 0; i < DST_NUM; i += 1) {
        // ident -> target expr
        PsiElement src = marks.get("<src" + String.valueOf(i + 1) + ">").getParent().getParent().getParent();
        Assert.assertTrue(src instanceof PyExpression);
        srcs[i] = src;
    }
    // ident -> target expr
    PsiElement src = marks.get("<src>").getParent();
    PyAssignmentStatement stmt = (PyAssignmentStatement) src.getParent().getParent();
    List<Pair<PyExpression, PyExpression>> mapping = stmt.getTargetsToValuesMapping();
    Assert.assertEquals(DST_NUM, mapping.size());
    for (int i = 0; i < DST_NUM; i += 1) {
        Pair<PyExpression, PyExpression> pair = mapping.get(i);
        Assert.assertEquals(dsts[i], pair.getFirst());
        Assert.assertEquals(srcs[i].getText(), pair.getSecond().getText());
    }
}
Also used : PyTargetExpression(com.jetbrains.python.psi.PyTargetExpression) PyExpression(com.jetbrains.python.psi.PyExpression) PyAssignmentStatement(com.jetbrains.python.psi.PyAssignmentStatement) PsiElement(com.intellij.psi.PsiElement) Pair(com.intellij.openapi.util.Pair)

Example 9 with PyTargetExpression

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

the class PyLineMarkerProvider method getAttributeMarker.

@Nullable
private static LineMarkerInfo<PsiElement> getAttributeMarker(PyTargetExpression element) {
    final String name = element.getReferencedName();
    if (name == null) {
        return null;
    }
    PyClass containingClass = PsiTreeUtil.getParentOfType(element, PyClass.class);
    if (containingClass == null)
        return null;
    for (PyClass ancestor : containingClass.getAncestorClasses(TypeEvalContext.codeAnalysis(element.getProject(), element.getContainingFile()))) {
        final PyTargetExpression ancestorAttr = ancestor.findClassAttribute(name, false, null);
        if (ancestorAttr != null) {
            return new LineMarkerInfo<PsiElement>(element, element.getTextRange().getStartOffset(), AllIcons.Gutter.OverridingMethod, Pass.LINE_MARKERS, new TooltipProvider("Overrides attribute in " + ancestor.getName()), ourSuperAttributeNavigator);
        }
    }
    return null;
}
Also used : PyClass(com.jetbrains.python.psi.PyClass) LineMarkerInfo(com.intellij.codeInsight.daemon.LineMarkerInfo) PyTargetExpression(com.jetbrains.python.psi.PyTargetExpression) Nullable(org.jetbrains.annotations.Nullable)

Example 10 with PyTargetExpression

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

the class PyRenameElementQuickFix method applyFix.

@Override
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
    PsiElement element = descriptor.getPsiElement();
    if (element instanceof PyReferenceExpression) {
        final PsiReference reference = element.getReference();
        if (reference == null)
            return;
        element = reference.resolve();
    }
    final PsiNameIdentifierOwner nameOwner = element instanceof PsiNameIdentifierOwner ? (PsiNameIdentifierOwner) element : PsiTreeUtil.getParentOfType(element, PsiNameIdentifierOwner.class, true);
    if (nameOwner != null) {
        final VirtualFile virtualFile = nameOwner.getContainingFile().getVirtualFile();
        if (virtualFile != null) {
            final Editor editor = FileEditorManager.getInstance(project).openTextEditor(new OpenFileDescriptor(project, virtualFile), true);
            if (ApplicationManager.getApplication().isUnitTestMode()) {
                renameInUnitTestMode(project, nameOwner, editor);
            } else {
                if (checkLocalScope(element) != null && (nameOwner instanceof PyNamedParameter || nameOwner instanceof PyTargetExpression)) {
                    new VariableInplaceRenamer(nameOwner, editor).performInplaceRename();
                } else {
                    PsiElementRenameHandler.invoke(nameOwner, project, ScopeUtil.getScopeOwner(nameOwner), editor);
                }
            }
        }
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) PsiNameIdentifierOwner(com.intellij.psi.PsiNameIdentifierOwner) PyTargetExpression(com.jetbrains.python.psi.PyTargetExpression) PsiReference(com.intellij.psi.PsiReference) OpenFileDescriptor(com.intellij.openapi.fileEditor.OpenFileDescriptor) PyNamedParameter(com.jetbrains.python.psi.PyNamedParameter) Editor(com.intellij.openapi.editor.Editor) VariableInplaceRenamer(com.intellij.refactoring.rename.inplace.VariableInplaceRenamer) PsiElement(com.intellij.psi.PsiElement) PyReferenceExpression(com.jetbrains.python.psi.PyReferenceExpression)

Aggregations

PyTargetExpression (com.jetbrains.python.psi.PyTargetExpression)16 PsiElement (com.intellij.psi.PsiElement)11 PyExpression (com.jetbrains.python.psi.PyExpression)7 Pair (com.intellij.openapi.util.Pair)6 PyAssignmentStatement (com.jetbrains.python.psi.PyAssignmentStatement)6 PyClass (com.jetbrains.python.psi.PyClass)3 PyFunction (com.jetbrains.python.psi.PyFunction)3 PyReferenceExpression (com.jetbrains.python.psi.PyReferenceExpression)3 VirtualFile (com.intellij.openapi.vfs.VirtualFile)2 PsiReference (com.intellij.psi.PsiReference)2 PyUtil (com.jetbrains.python.psi.PyUtil)2 Nullable (org.jetbrains.annotations.Nullable)2 LineMarkerInfo (com.intellij.codeInsight.daemon.LineMarkerInfo)1 Editor (com.intellij.openapi.editor.Editor)1 OpenFileDescriptor (com.intellij.openapi.fileEditor.OpenFileDescriptor)1 PsiFile (com.intellij.psi.PsiFile)1 PsiNameIdentifierOwner (com.intellij.psi.PsiNameIdentifierOwner)1 RenameProcessor (com.intellij.refactoring.rename.RenameProcessor)1 VariableInplaceRenamer (com.intellij.refactoring.rename.inplace.VariableInplaceRenamer)1 PyCustomMember (com.jetbrains.python.codeInsight.PyCustomMember)1