Search in sources :

Example 46 with PsiReference

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);
        }
    }
}
Also used : PsiNamedElement(com.intellij.psi.PsiNamedElement) PsiReference(com.intellij.psi.PsiReference) PsiElement(com.intellij.psi.PsiElement)

Example 47 with PsiReference

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);
                }
            }
        }
    }
}
Also used : PsiReference(com.intellij.psi.PsiReference) PsiElement(com.intellij.psi.PsiElement)

Example 48 with PsiReference

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();
        }
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) PyTargetExpression(com.jetbrains.python.psi.PyTargetExpression) RenameProcessor(com.intellij.refactoring.rename.RenameProcessor) PsiReference(com.intellij.psi.PsiReference) PsiElement(com.intellij.psi.PsiElement) PyReferenceExpression(com.jetbrains.python.psi.PyReferenceExpression)

Example 49 with PsiReference

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");
}
Also used : PsiReference(com.intellij.psi.PsiReference)

Example 50 with PsiReference

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;
}
Also used : XmlFile(com.intellij.psi.xml.XmlFile) PsiReference(com.intellij.psi.PsiReference) PsiElement(com.intellij.psi.PsiElement)

Aggregations

PsiReference (com.intellij.psi.PsiReference)564 PsiElement (com.intellij.psi.PsiElement)327 NotNull (org.jetbrains.annotations.NotNull)97 Nullable (org.jetbrains.annotations.Nullable)55 TextRange (com.intellij.openapi.util.TextRange)54 PsiFile (com.intellij.psi.PsiFile)52 ArrayList (java.util.ArrayList)46 Test (org.junit.Test)40 WorkspacePath (com.google.idea.blaze.base.model.primitives.WorkspacePath)36 BuildFile (com.google.idea.blaze.base.lang.buildfile.psi.BuildFile)32 IdentifierPSINode (org.ballerinalang.plugins.idea.psi.IdentifierPSINode)25 LeafPsiElement (com.intellij.psi.impl.source.tree.LeafPsiElement)23 XmlTag (com.intellij.psi.xml.XmlTag)22 VirtualFile (com.intellij.openapi.vfs.VirtualFile)21 XmlAttributeValue (com.intellij.psi.xml.XmlAttributeValue)20 PsiClass (com.intellij.psi.PsiClass)17 XmlAttribute (com.intellij.psi.xml.XmlAttribute)17 LinkedList (java.util.LinkedList)17 LookupElement (com.intellij.codeInsight.lookup.LookupElement)16 Project (com.intellij.openapi.project.Project)16