PSiElement resolving for completion contributor problem
Hello
I'm developing custom language plugin (https://github.com/xBlackCat/ice-framework-idea-plugin) and faced the problem that PsiElement for my completition contributor.
The problem is the following: to completion contributor invalid PsiElement is passed.
Here is a few screenshots from debug session on completion variants invocation:
1. As you can see, the cursor is set to ';' token. And after that I've tried to invoke autocomplete list
2. This is a debug session of the IDE from screenshot above. As you can see, getViewProvider().findElementAt(offset) returns invalid token 'PsiElement(id)' but if I get the file content and gets a char from the position I'll get correct character (';').

Take a look on getViewProvider() return value - it uses 'LightVirtualFile: \test.ice'.
In case the breakpoint reached not from the completion action the getViewProvider() uses myVirtualFile=file://P:/learn.projects/IceTest/src/ice/test.ice and returns a correct PsiElement(;) (but char at the position is not correct as the original file uses Windows EOL):

As I found out ViewProvider with LightVirtualFile contains invalid PSI tree. So there is a question:
how to force update PSI tree for ViewProvider with LightVirtualFile? Content of the real and LightVirtualFile are differs: after changing file in IDE, LightVirtualFile contains content of file from staritng IDE and somehow parses the tree wrong way.
Thank you
Please sign in to leave a comment.
By "invalid" do you mean "invalid as in !psiElementisValid()". Or do you mean "not what you expected"? If the latter, please read CompletionContributor javadoc, especially the section "What does the {@link CompletionParameters#getPosition()} return?".
By "invalid" I mean that Psi trees for real file and LightVirtual file are different even in case when the content of the files are equals.
It hard to demostrate in debug session as it could be seen by traveling through Psi tree during debug session.
At the last time I've did debug I saw that Psi tree of LV file contains parsing error node while the real file based Psi tree does not contain the error node anymore as I've edited the file in IDE.
It seems that LV file is not updated while real file text is editing. It the ability should be enabled some how or it should be done automatically?
In general my problems looks like: as you can see in the first screenshot, I expect that contributor should handle situation by rule: "inside SliceMethodDef and after leaf node ')' " but resolved node by #getPosition() is the first child node of SliceMethodDef node so the condition will never became true.
Sorry, I still don't understand what's happening. The fact that you see an identifier in the LightVirtualFile (a modified copy created for completion) is expected, and your completion contributor should have its logic stated in terms of files with that identifier inserted. The identifier can and does affect parse tree. If you absolutely don't need it (which I'd advise against), you can clear it in your CompletionContributor#beforeCompletion by calling context.setDummyIdentifier(""). The file copy will still be created, but it'd have same PSI structure as the real file.
For me the problem is not 'what returns' but 'where node should be'. I expecting a Psi node which is located between ')' and ';' nodes (see the first screenshot and selection in PsiViewer plugin) and I wonder why returned element located in the other part of tree (the node inside the first child of ICE_METHOD_DEF node on the first screenshot). So I've done some debug and found out what I've wrote in the first message.