use of com.jetbrains.python.psi.PyClass in project intellij-community by JetBrains.
the class CreateClassQuickFix method applyFix.
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
PsiElement anchor = myAnchor.getElement();
if (anchor == null || !anchor.isValid()) {
return;
}
if (!(anchor instanceof PyFile)) {
anchor = PyPsiUtils.getParentRightBefore(anchor, anchor.getContainingFile());
assert anchor != null;
}
PyClass pyClass = PyElementGenerator.getInstance(project).createFromText(LanguageLevel.getDefault(), PyClass.class, "class " + myClassName + "(object):\n pass");
if (anchor instanceof PyFile) {
pyClass = (PyClass) anchor.add(pyClass);
} else {
pyClass = (PyClass) anchor.getParent().addBefore(pyClass, anchor);
}
pyClass = CodeInsightUtilCore.forcePsiPostprocessAndRestoreElement(pyClass);
final TemplateBuilder builder = TemplateBuilderFactory.getInstance().createTemplateBuilder(pyClass);
builder.replaceElement(pyClass.getSuperClassExpressions()[0], "object");
builder.replaceElement(pyClass.getStatementList(), PyNames.PASS);
final FileEditor editor = FileEditorManager.getInstance(project).getSelectedEditor(anchor.getContainingFile().getVirtualFile());
if (!(editor instanceof TextEditor)) {
return;
}
builder.run(((TextEditor) editor).getEditor(), false);
}
use of com.jetbrains.python.psi.PyClass in project intellij-community by JetBrains.
the class PyControlFlowBuilderTest method testSelf.
public void testSelf() {
final String testName = getTestName(false).toLowerCase();
configureByFile(testName + ".py");
final String fullPath = getTestDataPath() + testName + ".txt";
final PyClass pyClass = ((PyFile) myFile).getTopLevelClasses().get(0);
final ControlFlow flow = ControlFlowCache.getControlFlow(pyClass.getMethods()[0]);
check(fullPath, flow);
}
use of com.jetbrains.python.psi.PyClass in project intellij-community by JetBrains.
the class PyHierarchyNodeDescriptor method update.
@Override
public boolean update() {
boolean changes = super.update();
final CompositeAppearance oldText = myHighlightedText;
myHighlightedText = new CompositeAppearance();
NavigatablePsiElement element = (NavigatablePsiElement) getPsiElement();
if (element == null) {
final String invalidPrefix = IdeBundle.message("node.hierarchy.invalid");
if (!myHighlightedText.getText().startsWith(invalidPrefix)) {
myHighlightedText.getBeginning().addText(invalidPrefix, HierarchyNodeDescriptor.getInvalidPrefixAttributes());
}
return true;
}
final ItemPresentation presentation = element.getPresentation();
if (presentation != null) {
if (element instanceof PyFunction) {
final PyClass cls = ((PyFunction) element).getContainingClass();
if (cls != null) {
myHighlightedText.getEnding().addText(cls.getName() + ".");
}
}
myHighlightedText.getEnding().addText(presentation.getPresentableText());
myHighlightedText.getEnding().addText(" " + presentation.getLocationString(), HierarchyNodeDescriptor.getPackageNameAttributes());
}
myName = myHighlightedText.getText();
if (!Comparing.equal(myHighlightedText, oldText)) {
changes = true;
}
return changes;
}
use of com.jetbrains.python.psi.PyClass in project intellij-community by JetBrains.
the class PythonDocumentationMap method transformPattern.
@Nullable
private static String transformPattern(@NotNull String urlPattern, QualifiedName moduleQName, @Nullable PsiNamedElement element, String pyVersion) {
Map<String, String> macros = new HashMap<>();
macros.put("element.name", element == null ? null : element.getName());
PyClass pyClass = element == null ? null : PsiTreeUtil.getParentOfType(element, PyClass.class, false);
macros.put("class.name", pyClass == null ? null : pyClass.getName());
if (element != null) {
StringBuilder qName = new StringBuilder(moduleQName.toString()).append(".");
if (element instanceof PyFunction && ((PyFunction) element).getContainingClass() != null) {
qName.append(((PyFunction) element).getContainingClass().getName()).append(".");
}
qName.append(element.getName());
macros.put("element.qname", qName.toString());
} else {
macros.put("element.qname", "");
}
macros.put("function.name", element instanceof PyFunction ? element.getName() : "");
macros.put("module.name", moduleQName.toString());
macros.put("python.version", pyVersion);
final String pattern = transformPattern(urlPattern, macros);
if (pattern == null) {
return rootForPattern(urlPattern);
}
return pattern;
}
use of com.jetbrains.python.psi.PyClass in project intellij-community by JetBrains.
the class PySuperTypesHierarchyTreeStructure method buildChildren.
@NotNull
protected Object[] buildChildren(@NotNull HierarchyNodeDescriptor descriptor) {
final List<PyHierarchyNodeDescriptor> res = new ArrayList<>();
if (descriptor instanceof PyHierarchyNodeDescriptor) {
final PyHierarchyNodeDescriptor pyDescriptor = (PyHierarchyNodeDescriptor) descriptor;
final PsiElement element = pyDescriptor.getPsiElement();
if (element instanceof PyClass) {
final PyClass cls = (PyClass) element;
final PyClass[] superClasses = cls.getSuperClasses(null);
for (PyClass superClass : superClasses) {
res.add(new PyHierarchyNodeDescriptor(descriptor, superClass, false));
}
}
}
return res.toArray();
}
Aggregations