use of com.intellij.psi.PsiReference in project intellij-community by JetBrains.
the class PyMakeFunctionFromMethodQuickFix method updateUsage.
private static void updateUsage(@NotNull final PsiElement finalElement, @NotNull final PyReferenceExpression element, @NotNull final PsiFile usageFile, boolean addImport) {
final PyExpression qualifier = element.getQualifier();
if (qualifier == null)
return;
if (qualifier.getText().equals(PyNames.CANONICAL_SELF)) {
PyUtil.removeQualifier(element);
return;
}
if (qualifier instanceof PyCallExpression) {
// remove qualifier A().m()
if (addImport)
AddImportHelper.addImport((PsiNamedElement) finalElement, usageFile, element);
PyUtil.removeQualifier(element);
removeFormerImport(usageFile, addImport);
} else {
final PsiReference reference = qualifier.getReference();
if (reference == null)
return;
final PsiElement resolved = reference.resolve();
if (resolved instanceof PyTargetExpression) {
// qualifier came from assignment a = A(); a.m()
updateAssignment(element, resolved);
} else if (resolved instanceof PyClass) {
//call with first instance argument A.m(A())
PyUtil.removeQualifier(element);
updateArgumentList(element);
}
}
}
use of com.intellij.psi.PsiReference in project intellij-community by JetBrains.
the class PyAddPropertyForFieldQuickFix method applyFix.
public void applyFix(@NotNull final Project project, @NotNull final ProblemDescriptor descriptor) {
final PsiElement element = descriptor.getPsiElement();
if (element instanceof PyReferenceExpression) {
final PsiReference reference = element.getReference();
if (reference == null)
return;
final PsiElement resolved = reference.resolve();
if (resolved instanceof PyTargetExpression) {
PyTargetExpression target = (PyTargetExpression) resolved;
final PyClass containingClass = target.getContainingClass();
if (containingClass != null) {
final String name = target.getName();
if (name == null)
return;
String propertyName = StringUtil.trimStart(name, "_");
final Map<String, Property> properties = containingClass.getProperties();
final PyElementGenerator generator = PyElementGenerator.getInstance(project);
if (!properties.containsKey(propertyName)) {
final PyFunction property = generator.createProperty(LanguageLevel.forElement(containingClass), propertyName, name, AccessDirection.READ);
PyUtil.addElementToStatementList(property, containingClass.getStatementList(), false);
}
final PyExpression qualifier = ((PyReferenceExpression) element).getQualifier();
if (qualifier != null) {
String newElementText = qualifier.getText() + "." + propertyName;
final PyExpression newElement = generator.createExpressionFromText(LanguageLevel.forElement(containingClass), newElementText);
element.replace(newElement);
}
}
}
}
}
use of com.intellij.psi.PsiReference in project intellij-community by JetBrains.
the class PyMakePublicQuickFix 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();
}
if (element instanceof PyTargetExpression) {
final String name = ((PyTargetExpression) element).getName();
if (name == null)
return;
final VirtualFile virtualFile = element.getContainingFile().getVirtualFile();
if (virtualFile != null) {
final String publicName = StringUtil.trimLeading(name, '_');
new RenameProcessor(project, element, publicName, false, false).run();
}
}
}
use of com.intellij.psi.PsiReference in project intellij-community by JetBrains.
the class PyAddImportTest method doAddLocalImport.
/**
* Add local import statement
*
* @param name reference name in corresponding import element
* @param qualifier if not {@code null} form {@code from qualifier import name} will be used, otherwise {@code import name}
*/
private void doAddLocalImport(@NotNull final String name, @Nullable final String qualifier) {
myFixture.configureByFile(getTestName(true) + ".py");
WriteCommandAction.runWriteCommandAction(myFixture.getProject(), () -> {
final PsiReference reference = PyResolveTestCase.findReferenceByMarker(myFixture.getFile());
if (qualifier != null) {
AddImportHelper.addLocalFromImportStatement(reference.getElement(), qualifier, name);
} else {
AddImportHelper.addLocalImportStatement(reference.getElement(), name);
}
});
myFixture.checkResultByFile(getTestName(true) + ".after.py");
}
use of com.intellij.psi.PsiReference in project intellij-community by JetBrains.
the class RngHrefConverter method fromString.
@Override
public XmlFile fromString(@Nullable @NonNls String s, ConvertContext context) {
if (s != null) {
final GenericAttributeValue<XmlFile> element = (GenericAttributeValue<XmlFile>) context.getInvocationElement();
final PsiReference[] references = createReferences(element, element.getXmlAttributeValue(), context);
if (references.length > 0) {
PsiElement result = references[references.length - 1].resolve();
if (result instanceof XmlFile) {
return (XmlFile) result;
}
}
}
return null;
}
Aggregations