use of com.jetbrains.python.psi.PyClass in project intellij-community by JetBrains.
the class PyClassRefactoringTest method findClass.
protected PyClass findClass(final String name) {
final Project project = myFixture.getProject();
final Collection<PyClass> classes = PyClassNameIndex.find(name, project, false);
Assert.assertThat(String.format("Expected one class named %s", name), classes, Matchers.hasSize(1));
return classes.iterator().next();
}
use of com.jetbrains.python.psi.PyClass in project intellij-community by JetBrains.
the class PyClassNameMacro method calculateResult.
@Nullable
@Override
public Result calculateResult(@NotNull Expression[] params, ExpressionContext context) {
PsiElement place = context.getPsiElementAtStartOffset();
PyClass pyClass = PsiTreeUtil.getParentOfType(place, PyClass.class);
if (pyClass == null) {
return null;
}
String name = pyClass.getName();
return name == null ? null : new TextResult(name);
}
use of com.jetbrains.python.psi.PyClass in project intellij-community by JetBrains.
the class PyStdlibDocumentationLinkProvider method getStdlibUrlFor.
private String getStdlibUrlFor(PsiElement element, QualifiedName moduleName, Sdk sdk) {
StringBuilder urlBuilder = new StringBuilder(getExternalDocumentationRoot(sdk));
String qnameString = moduleName.toString();
if (qnameString.equals("ntpath") || qnameString.equals("posixpath")) {
qnameString = "os.path";
} else if (qnameString.equals("nt")) {
qnameString = "os";
} else if (qnameString.equals("cPickle")) {
qnameString = "pickle";
} else if (qnameString.equals("pyexpat")) {
qnameString = "xml.parsers.expat";
}
final String pyVersion = PythonDocumentationProvider.pyVersion(sdk.getVersionString());
List<String> modules = pyVersion != null && pyVersion.startsWith("3") ? py3LibraryModules : py2LibraryModules;
boolean foundModule = false;
for (String module : modules) {
if (qnameString.startsWith(module)) {
urlBuilder.append(module.toLowerCase());
urlBuilder.append(".html");
foundModule = true;
break;
}
}
if (foundModule && element instanceof PsiNamedElement && !(element instanceof PyFile)) {
urlBuilder.append('#').append(moduleName).append(".");
if (element instanceof PyFunction) {
final PyClass containingClass = ((PyFunction) element).getContainingClass();
if (containingClass != null) {
urlBuilder.append(containingClass.getName()).append('.');
}
}
urlBuilder.append(((PsiNamedElement) element).getName());
}
return urlBuilder.toString();
}
use of com.jetbrains.python.psi.PyClass in project intellij-community by JetBrains.
the class PyClassTreeChooserDialog method getClassesByName.
@NotNull
@Override
protected List<PyClass> getClassesByName(String name, boolean checkBoxState, String pattern, GlobalSearchScope searchScope) {
final Collection<PyClass> classes = PyClassNameIndex.find(name, getProject(), searchScope.isSearchInLibraries());
final List<PyClass> result = Lists.newArrayList();
for (PyClass c : classes) {
if (getFilter().isAccepted(c)) {
result.add(c);
}
}
return result;
}
use of com.jetbrains.python.psi.PyClass in project intellij-community by JetBrains.
the class PyTestFinder method findTestsForClass.
@NotNull
@Override
public Collection<PsiElement> findTestsForClass(@NotNull PsiElement element) {
PyDocStringOwner source = findSourceElement(element);
if (source == null)
return Collections.emptySet();
String sourceName = source.getName();
if (sourceName == null)
return Collections.emptySet();
List<Pair<? extends PsiNamedElement, Integer>> classesWithProximities = new ArrayList<>();
if (source instanceof PyClass) {
Collection<String> names = PyClassNameIndex.allKeys(element.getProject());
for (String eachName : names) {
if (eachName.contains(sourceName)) {
for (PyClass eachClass : PyClassNameIndex.find(eachName, element.getProject(), GlobalSearchScope.projectScope(element.getProject()))) {
if (PythonUnitTestUtil.isTestCaseClass(eachClass, null) || PythonDocTestUtil.isDocTestClass(eachClass)) {
classesWithProximities.add(new Pair<PsiNamedElement, Integer>(eachClass, TestFinderHelper.calcTestNameProximity(sourceName, eachName)));
}
}
}
}
} else {
Collection<String> names = PyFunctionNameIndex.allKeys(element.getProject());
for (String eachName : names) {
if (eachName.contains(sourceName)) {
for (PyFunction eachFunction : PyFunctionNameIndex.find(eachName, element.getProject(), GlobalSearchScope.projectScope(element.getProject()))) {
if (PythonUnitTestUtil.isTestCaseFunction(eachFunction) || PythonDocTestUtil.isDocTestFunction(eachFunction)) {
classesWithProximities.add(new Pair<PsiNamedElement, Integer>(eachFunction, TestFinderHelper.calcTestNameProximity(sourceName, eachName)));
}
}
}
}
}
return TestFinderHelper.getSortedElements(classesWithProximities, true);
}
Aggregations