Open adesutherland opened 5 years ago
The ANTLR plugin does something similar in ANTLRv4ExternalAnnotator. It retrieves the file contents, gives it to ANTLR, builds a list of issues and creates annotations based on a TextRange
built from a token's start and stop index.
So you're right, you currently have to parse the file a second time.
If you have existing walkers, I suppose the best option to reuse them is implement an ExternalAnnotator
and make the adaptor attach the ANTLR parse tree to the PsiFile
. This way, you could use your walkers in doAnnotate
. I'd have to add a new feature to the library to allow that.
I you don't have existing walkers, you can implement an Annotator
, and IntelliJ will recursively walk the file for you. You will then be working on instances of PsiElement
(probably even of ANTLRPsiNode
). You won't have access to the original ANTLR parse tree nodes. I guess it would also be possible to make the adaptor attach the original parse tree node to each instance of ANTLRPsiNode
.
Understand. As a feature suggestion I think it would be good to have the adaptor attach the ANTLR Parse Tree to the PsiFile. If I have time I will see if I can make the change.
Thanks again.
I'm converting this issue to an enhancement then, just to keep track of the feature :)
Totally understand why we should use the PSI tree, for example, to get all the inbuilt features like renaming and so on. And the ANTLR like Xpath feature works well. In sum, no complaints.
However, I do have a number of ANTLR walkers (for example that do more fine grained work around identifier type safety and other analysis). I really can't be bothered to re-do for PSI trees (and also not sure how to make a walker in PSI - I guess I could learn!).
So I think I could quite easily pump the whole file through the ANTLR Lexer and Parser in an external annotator, and could get row and column numbers to sync, and hence add some warning annotations etc. Although I can't find an example of how to access the file's contents the virtual file system seems to be the answer.
So finally (!) the question is this - can I access either the actual ANTLR (not PSI) parse tree from an annotator, or perhaps the token stream? I obviously have looked but can't decide if there is a suitable extension point or not. For example somehow when the PSI tree is built.
I am thinking it is not possible at the moment and I need to reparse the file. Is this right?