use of com.intellij.psi.PsiNamedElement in project intellij-community by JetBrains.
the class PyIterableVariableMacro method calculateLookupItems.
@Nullable
@Override
public LookupElement[] calculateLookupItems(@NotNull Expression[] params, ExpressionContext context) {
final List<LookupElement> results = new ArrayList<>();
final PsiElement element = context.getPsiElementAtStartOffset();
if (element != null) {
for (PsiNamedElement iterableElement : getIterableElements(element)) {
final String name = iterableElement.getName();
if (name != null) {
results.add(LookupElementBuilder.createWithSmartPointer(name, iterableElement));
}
}
}
return results.toArray(new LookupElement[results.size()]);
}
use of com.intellij.psi.PsiNamedElement in project intellij-community by JetBrains.
the class PyIterableVariableMacro method getVisibleNamedElements.
@NotNull
private static List<PsiNamedElement> getVisibleNamedElements(@NotNull PsiElement anchor) {
final List<PsiNamedElement> results = new ArrayList<>();
for (ScopeOwner owner = ScopeUtil.getScopeOwner(anchor); owner != null; owner = ScopeUtil.getScopeOwner(owner)) {
final Scope scope = ControlFlowCache.getScope(owner);
results.addAll(scope.getNamedElements());
StreamEx.of(scope.getImportedNameDefiners()).filter(definer -> !PyImplicitImportNameDefiner.class.isInstance(definer)).flatMap(definer -> StreamSupport.stream(definer.iterateNames().spliterator(), false)).select(PsiNamedElement.class).forEach(results::add);
}
return results;
}
use of com.intellij.psi.PsiNamedElement in project intellij-community by JetBrains.
the class PyIterableVariableMacro method getIterableElements.
@NotNull
protected List<PsiNamedElement> getIterableElements(@NotNull PsiElement element) {
final TypeEvalContext typeEvalContext = TypeEvalContext.userInitiated(element.getProject(), element.getContainingFile());
final List<PsiNamedElement> components = new ArrayList<>();
for (PsiNamedElement namedElement : getVisibleNamedElements(element)) {
if (namedElement instanceof PyTypedElement) {
final PyType type = typeEvalContext.getType((PyTypedElement) namedElement);
if (type != null && PyABCUtil.isSubtype(type, PyNames.ITERABLE, typeEvalContext)) {
components.add(namedElement);
}
}
}
return components;
}
use of com.intellij.psi.PsiNamedElement 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.intellij.psi.PsiNamedElement 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