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());
}
}
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());
}
}
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());
}
}
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;
}
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);
}
}
}
}
}
Aggregations