Search in sources :

Example 1 with PyReferenceExpression

use of com.jetbrains.python.psi.PyReferenceExpression 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 2 with PyReferenceExpression

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

the class InstructionBuilder method buildInstructions.

public static List<Instruction> buildInstructions(ControlFlowBuilder builder, List<PyTypeAssertionEvaluator.Assertion> assertions) {
    List<Instruction> result = Lists.newArrayList();
    for (PyTypeAssertionEvaluator.Assertion def : assertions) {
        final PyReferenceExpression e = def.getElement();
        final QualifiedName qname = e.asQualifiedName();
        final String name = qname != null ? qname.toString() : e.getName();
        result.add(ReadWriteInstruction.assertType(builder, e, name, def.getTypeEvalFunction()));
    }
    return result;
}
Also used : QualifiedName(com.intellij.psi.util.QualifiedName) Instruction(com.intellij.codeInsight.controlflow.Instruction) PyReferenceExpression(com.jetbrains.python.psi.PyReferenceExpression)

Example 3 with PyReferenceExpression

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

Example 4 with PyReferenceExpression

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

the class PyRenameUnresolvedRefQuickFix method applyFix.

@Override
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
    final PsiElement element = descriptor.getPsiElement();
    if (!(element instanceof PyReferenceExpression)) {
        return;
    }
    final PyReferenceExpression referenceExpression = (PyReferenceExpression) element;
    ScopeOwner parentScope = ScopeUtil.getScopeOwner(referenceExpression);
    if (parentScope == null)
        return;
    List<PyReferenceExpression> refs = collectExpressionsToRename(referenceExpression, parentScope);
    LookupElement[] items = collectLookupItems(parentScope);
    final String name = referenceExpression.getReferencedName();
    ReferenceNameExpression refExpr = new ReferenceNameExpression(items, name);
    TemplateBuilderImpl builder = (TemplateBuilderImpl) TemplateBuilderFactory.getInstance().createTemplateBuilder(parentScope);
    for (PyReferenceExpression expr : refs) {
        if (!expr.equals(referenceExpression)) {
            builder.replaceElement(expr, name, name, false);
        } else {
            builder.replaceElement(expr, name, refExpr, true);
        }
    }
    Editor editor = getEditor(project, element.getContainingFile(), parentScope.getTextRange().getStartOffset());
    if (editor != null) {
        Template template = builder.buildInlineTemplate();
        TemplateManager.getInstance(project).startTemplate(editor, template);
    }
}
Also used : ScopeOwner(com.jetbrains.python.codeInsight.controlflow.ScopeOwner) LookupElement(com.intellij.codeInsight.lookup.LookupElement) Editor(com.intellij.openapi.editor.Editor) PsiElement(com.intellij.psi.PsiElement) PyReferenceExpression(com.jetbrains.python.psi.PyReferenceExpression)

Example 5 with PyReferenceExpression

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

the class PyRenameUnresolvedRefQuickFix method collectExpressionsToRename.

private static List<PyReferenceExpression> collectExpressionsToRename(@NotNull final PyReferenceExpression expression, @NotNull final ScopeOwner parentScope) {
    final List<PyReferenceExpression> result = new ArrayList<>();
    PyRecursiveElementVisitor visitor = new PyRecursiveElementVisitor() {

        @Override
        public void visitPyReferenceExpression(PyReferenceExpression node) {
            if (node.textMatches(expression) && !isValidReference(node.getReference())) {
                result.add(node);
            }
            super.visitPyReferenceExpression(node);
        }
    };
    parentScope.accept(visitor);
    return result;
}
Also used : PyRecursiveElementVisitor(com.jetbrains.python.psi.PyRecursiveElementVisitor) PyReferenceExpression(com.jetbrains.python.psi.PyReferenceExpression)

Aggregations

PyReferenceExpression (com.jetbrains.python.psi.PyReferenceExpression)8 PsiElement (com.intellij.psi.PsiElement)5 PyTargetExpression (com.jetbrains.python.psi.PyTargetExpression)3 Editor (com.intellij.openapi.editor.Editor)2 VirtualFile (com.intellij.openapi.vfs.VirtualFile)2 PsiReference (com.intellij.psi.PsiReference)2 ScopeOwner (com.jetbrains.python.codeInsight.controlflow.ScopeOwner)2 Nullable (org.jetbrains.annotations.Nullable)2 Instruction (com.intellij.codeInsight.controlflow.Instruction)1 LookupElement (com.intellij.codeInsight.lookup.LookupElement)1 OpenFileDescriptor (com.intellij.openapi.fileEditor.OpenFileDescriptor)1 PsiNameIdentifierOwner (com.intellij.psi.PsiNameIdentifierOwner)1 QualifiedName (com.intellij.psi.util.QualifiedName)1 RenameProcessor (com.intellij.refactoring.rename.RenameProcessor)1 VariableInplaceRenamer (com.intellij.refactoring.rename.inplace.VariableInplaceRenamer)1 PydevConsoleReference (com.jetbrains.python.console.completion.PydevConsoleReference)1 PyDocumentationBuilder (com.jetbrains.python.documentation.PyDocumentationBuilder)1 PyElement (com.jetbrains.python.psi.PyElement)1 PyNamedParameter (com.jetbrains.python.psi.PyNamedParameter)1 PyParameter (com.jetbrains.python.psi.PyParameter)1