Get auto complete working in a custom LightVirtualFile instance

We would like our custom LightVirtualFile instances to offer auto complete that matches the open project (or even a particular module). We have observed this happens automatically in WebStorm but for IntelliJ we only get basic language completion.

We have also observed that scratch files are able to do this (and Kotlin scratch files even have a module selector) but after studying the code it's still not clear to us how this works. Also, we noticed this is not the case with PyCharm (as reported here: https://youtrack.jetbrains.com/issue/PY-24670?_ga=2.10049011.771698166.1588872331-888628167.1586353309)

 

Is there a language-agnostic way to make this work? Any pointers would be of great help!

10 comments
Comment actions Permalink

Is this about your own custom completion? And it works in WS, but not in IJ? Could you please give specific examples?

0
Comment actions Permalink

No, not custom completion. The standard completion but on custom LightVirtualFile instances aka files that do not exist on the users drive.

 

Yes it works in WS already, we open our virtual file in an editor and the user gets autocomplete based on the contents of the files in the open project. 

0
Comment actions Permalink

For what language(s) does it work in WS, but not in IJ? What versions of IDEs? Exact same set of plugins?

0
Comment actions Permalink

Works for TypeScript in WebStorm but I assume it works in WS because WS works in a fundamentally different way without the concept of modules like IJ.

 

Also I don't believe its a bug that it doesn't work in IJ - I am looking for what I need to implement in the virtual file to make IJ think its part of one of the modules in the open project. This is what the kotlin plugin appears to be able to do with its scratch files and the module selector dropdown UI

0
Comment actions Permalink

So "dumb" completion for e.g. keywords works, but completion/resolving for symbols doesn't?

0
Comment actions Permalink

If you want to check how TypeScript suggests completion for all modules from scratch files, you may look at `com.intellij.lang.javascript.psi.resolve.JSResolveUtil#getResolveScope`. Basically, it uses com.intellij.psi.impl.ResolveScopeManager#getDefaultResolveScope. Other languages probably use it too. To put your file in resolve scope, you may use an extension point in com.intellij.psi.ResolveScopeEnlarger. com.intellij.openapi.roots.AdditionalLibraryRootsProvider may also be useful, but it's project-level only.

0
Comment actions Permalink

Thanks Konstantin, we would like it to work the other way round though - for completions to be suggested in the LightVirtualFiles from the project files, will ResolveScopeManager still help in this case?

0
Comment actions Permalink

Yes, it will help, depending on a FileType.

0
Comment actions Permalink

We can see there are scopes such as  ModuleScope and ProjectAndLibrariesScope but we can't see how to 'apply' a scope to a VirtualFile using the ResolveScopeManager. 

 

Also reviewing these scopes in the IJ code they seem to have more to do with searching/find usages than auto complete.

0

Please sign in to leave a comment.