Collection<PsiElement> (@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 (PythonUnitTestDetectorsBasedOnSettings.isTestClass(eachClass, ThreeState.UNSURE, 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 (PythonUnitTestDetectorsBasedOnSettings.isTestFunction( eachFunction, ThreeState.UNSURE, null) || PythonDocTestUtil.isDocTestFunction(eachFunction)) { classesWithProximities.add( new Pair<PsiNamedElement, Integer>(eachFunction, TestFinderHelper.calcTestNameProximity(sourceName, eachName))); } } } } } return TestFinderHelper.getSortedElements(classesWithProximities, true); }