Pycharm find files importing current files


I am implementing CompletionContributor which requires (in order to get correct autocomplete suggestions) having a look at:

  1. Curent file - done.
  2. Files imported by the current file - done via resolving the references of
    PsiTreeUtil.findChildrenOfType(file, PyImportStatement.class) # and PyFromImportStatement
  3. 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


but this didn't return what I wanted. Any help would be highly appreciated.

Comment actions Permalink

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`.

Comment actions Permalink

Thanks Mikhail Golubev! Exactly what I needed, works like a charm.


Please sign in to leave a comment.