Pycharm find files importing current files

Answered

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

getContainingFile().getReferences()

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

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

1
Comment actions Permalink

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

0

Please sign in to leave a comment.