Stubs and Search Scopes

Answered

Hi,

I'm developing a custom language plugin and implemented (named)stubs to speed up the performance of reference resolving.

As of now, the index contains the name of the referenced elements. However, the plugin is used in IntelliJ projects containing multiple Maven Modules.

Across the different Maven Modules, the same element names can be defined multiple times, but when resolving the reference inside a Maven Module A, I don't want to accidentally jump to the definition with the same name in Maven Module B. So I want to resolve only within the correct Maven Module.

I could pass the correct SearchScope when retrieving a value from the index, but as far as I know, I have to retrieve the containing VirtualFile for every reference, to get the containing Maven Module. I implemented stubs to avoid retrieving the containing VirtualFile so often, but now I am curious, if I can get the correct SearchScope without doing the expensive calls to get the containing file.

Is there probably a better approach for indexing I can use? Or can I get the correct scope without the containing file?

0
3 comments

Hi,

I'm not sure I understand the context fully. If modules A and B are isolated, and you don't want to get elements from A when the reference is in B, then try using PsiElement.getResolveScope(), when retrieving elements from the index.

0

Hi,

you are right, module A and B are isolated. However, module A can have module B as a dependency. In that case, I want to get elements from module B when the reference is in module A.

unfortunately even calling myIndex.get(key, project, element.getResolveScope()) does not return any elements, even when I see, that the index contains the key (correctly serialized) when I call myIndex.getAllKeys(project).

Do I have to pass the scope somewhere, when I add stubs to the index using indexStub(stub, sink)?
 

0

Update: I got it working now.  PsiElement.getResolveScope() helped a lot! In my usecase element.getResolveScope().union(element.getResolveScope().getModule().getModuleContentWithDependenciesScope()) creates the correct scope. Thanks!

1

Please sign in to leave a comment.