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