use of com.jetbrains.python.psi.PyFile in project intellij-community by JetBrains.
the class PyModuleFindUsagesHandler method findReferencesToHighlight.
@NotNull
@Override
public Collection<PsiReference> findReferencesToHighlight(@NotNull PsiElement target, @NotNull SearchScope searchScope) {
if (target instanceof PyImportedModule) {
target = ((PyImportedModule) target).resolve();
}
if (target instanceof PyFile && PyNames.INIT_DOT_PY.equals(((PyFile) target).getName())) {
List<PsiReference> result = new ArrayList<>();
result.addAll(super.findReferencesToHighlight(target, searchScope));
PsiElement targetDir = PyUtil.turnInitIntoDir(target);
if (targetDir != null) {
result.addAll(ReferencesSearch.search(targetDir, searchScope, false).findAll());
}
return result;
}
return super.findReferencesToHighlight(target, searchScope);
}
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));
}
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;
}
Aggregations