Pycharm find files importing current files
Answered
I am implementing CompletionContributor which requires (in order to get correct autocomplete suggestions) having a look at:
- Curent file - done.
- Files imported by the current file - done via resolving the references of
PsiTreeUtil.findChildrenOfType(file, PyImportStatement.class) # and PyFromImportStatement
- Files that import current file.
I am struggling with point 3 though. I basically need to find usages of current file in other files. I tried
getContainingFile().getReferences()
but this didn't return what I wanted. Any help would be highly appreciated.
Please sign in to leave a comment.
Hi Martin!
The easiest way is to call `PyPsiIndexUtil#findUsages(file, false)`. It should return PSI elements referencing the file wrapped in `UsageInfo` instances. You can retain only those from import statements by checking their `UsageInfo#getReference` results, import elements should return `PyImportReference`.
Thanks Mikhail Golubev! Exactly what I needed, works like a charm.