Implementing a ReferenceContributor for missing function references

Answered

Hi,

I'd like to implement a plugin based on the reference contributor, which would resolve functions that are not imported with import statements. My use case is Jupyter Notebooks or Databricks Notebooks, where files are 'imported' as shown below:

helper.py:

def foo():
print('Hello world')

main.py

# COMMAND ----------
# MAGIC %run ./helper

# COMMAND ----------
foo()

 

The foo element in main.py is missing the reference and I'd like to implement a ReferenceContributor, which will parse the comment above and provide that reference. I'm trying to implement this with PsiReferenceContributor, but the problem I've faced is that no matter what PsiElementPattern I use, I only get comments and literals back.

3 comments
Avatar
Andrey Vlasovskikh
Comment actions Permalink

You could register a reference contributor for `PsiComment` and then provide a reference via your `PsiReferenceProvider` for specific text ranges in your comment. Feel free to take a look at a similar example where we provide references to individual directories and files for your file path within `PyStringLiteralExpression`: https://github.com/JetBrains/intellij-community/blob/master/python/python-psi-impl/src/com/jetbrains/python/codeInsight/PySoftFileReferenceContributor.kt

0
Comment actions Permalink

I'm going to try this, thanks @.... If I understand correctly, this would provide a reference for the comment itself, but not the actual function call, correct? I'm hoping to allow function references so that I get code completion, signature validation, etc.

Is there a mechanism to parse the comment and 'auto import' a file so that functions from that file are 'visible' in the current file?

0
Avatar
Andrey Vlasovskikh
Comment actions Permalink

Daniel Siwiec This will provide a reference for a text range you specify in your comment *to* a Python element you're interested in. So you can, for example, make your `./helper` a reference that resolves (`PsiReference.resolve()`) to your helper.py file. Then since you need to expose all public names of your file as it was `from helper import *`, you can look at how star-imports are implemented in our codebase to provide a range of names.

0

Please sign in to leave a comment.