use of com.jetbrains.python.psi.PyFile in project intellij-community by JetBrains.
the class PyModuleNameIndex method find.
@NotNull
public static List<PyFile> find(@NotNull String name, @NotNull Project project, boolean includeNonProjectItems) {
final List<PyFile> results = new ArrayList<>();
final GlobalSearchScope scope = includeNonProjectItems ? PyProjectScopeBuilder.excludeSdkTestsScope(project) : GlobalSearchScope.projectScope(project);
final Collection<VirtualFile> files = FileBasedIndex.getInstance().getContainingFiles(NAME, name, scope);
for (VirtualFile virtualFile : files) {
final PsiFile psiFile = PsiManager.getInstance(project).findFile(virtualFile);
if (psiFile instanceof PyFile) {
if (!PyUserSkeletonsUtil.isUnderUserSkeletonsDirectory(psiFile)) {
results.add((PyFile) psiFile);
}
}
}
return results;
}
use of com.jetbrains.python.psi.PyFile in project intellij-community by JetBrains.
the class PythonBuiltinReferenceResolveProvider method resolveName.
@NotNull
@Override
public List<RatedResolveResult> resolveName(@NotNull PyQualifiedExpression element, @NotNull TypeEvalContext context) {
final String referencedName = element.getReferencedName();
if (referencedName == null) {
return Collections.emptyList();
}
final List<RatedResolveResult> result = new ArrayList<>();
final PyBuiltinCache builtinCache = PyBuiltinCache.getInstance(PyPsiUtils.getRealContext(element));
// resolve to module __doc__
if (PyNames.DOC.equals(referencedName)) {
result.addAll(Optional.ofNullable(builtinCache.getObjectType()).map(type -> type.resolveMember(referencedName, element, AccessDirection.of(element), PyResolveContext.noImplicits().withTypeEvalContext(context))).orElse(Collections.emptyList()));
}
// ...as a builtin symbol
final PyFile builtinsFile = builtinCache.getBuiltinsFile();
if (builtinsFile != null && !PyUtil.isClassPrivateName(referencedName)) {
for (RatedResolveResult resolveResult : builtinsFile.multiResolveName(referencedName)) {
result.add(new ImportedResolveResult(resolveResult.getElement(), resolveResult.getRate(), null));
}
}
return result;
}
use of com.jetbrains.python.psi.PyFile in project intellij-community by JetBrains.
the class PyDunderAllReference method getVariants.
@NotNull
@Override
public Object[] getVariants() {
final List<LookupElement> result = new ArrayList<>();
PyFile containingFile = (PyFile) getElement().getContainingFile().getOriginalFile();
final List<String> dunderAll = containingFile.getDunderAll();
final Set<String> seenNames = new HashSet<>();
if (dunderAll != null) {
seenNames.addAll(dunderAll);
}
containingFile.processDeclarations(new PsiScopeProcessor() {
@Override
public boolean execute(@NotNull PsiElement element, @NotNull ResolveState state) {
if (element instanceof PsiNamedElement && !(element instanceof LightNamedElement)) {
final String name = ((PsiNamedElement) element).getName();
if (name != null && PyUtil.getInitialUnderscores(name) == 0 && !seenNames.contains(name)) {
seenNames.add(name);
result.add(LookupElementBuilder.createWithSmartPointer(name, element).withIcon(element.getIcon(0)));
}
} else if (element instanceof PyImportElement) {
final String visibleName = ((PyImportElement) element).getVisibleName();
if (visibleName != null && !seenNames.contains(visibleName)) {
seenNames.add(visibleName);
result.add(LookupElementBuilder.createWithSmartPointer(visibleName, element));
}
}
return true;
}
@Override
public <T> T getHint(@NotNull Key<T> hintKey) {
return null;
}
@Override
public void handleEvent(@NotNull Event event, @Nullable Object associated) {
}
}, ResolveState.initial(), null, containingFile);
return ArrayUtil.toObjectArray(result);
}
use of com.jetbrains.python.psi.PyFile in project intellij-community by JetBrains.
the class PyContainingFileRenamerFactory method isApplicable.
@Override
public boolean isApplicable(PsiElement element) {
if (!(element instanceof PyClass)) {
return false;
}
ScopeOwner scopeOwner = PsiTreeUtil.getParentOfType(element, ScopeOwner.class);
if (scopeOwner instanceof PyFile) {
String className = ((PyClass) element).getName();
String fileName = FileUtil.getNameWithoutExtension(scopeOwner.getName());
return fileName.equalsIgnoreCase(className);
}
return false;
}
use of com.jetbrains.python.psi.PyFile in project intellij-community by JetBrains.
the class UnresolvedRefAddFutureImportQuickFix method applyFix.
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
PsiElement element = descriptor.getPsiElement();
PyFile file = (PyFile) element.getContainingFile();
PyElementGenerator elementGenerator = PyElementGenerator.getInstance(project);
PyFromImportStatement statement = elementGenerator.createFromText(LanguageLevel.forElement(element), PyFromImportStatement.class, "from __future__ import with_statement");
file.addBefore(statement, file.getStatements().get(0));
}
Aggregations