Add attribute mixedAST
to simplify mixed AST+PSI creation
#316
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
We are used to two different kinds of PSI implementations:
ASTDelegatePsiElement
StubBasedPsiElementBase
But really there is a third option - mix PSI and AST in a single class/object:
This is useful when a PSI element does not have a stub. In this case, additional AST object is not allocated, so we win in memory consumption, and the tree traversing will be fasted, so we win in CPU as well.
A PSI element can be mixed if it implements both
ASTNode
andPsiElement
interfaces. This is already done in the platform classCompositePsiElement
.When extending
CompositePsiElement
, a psi implementation slightly differs from the one extendingASTDelegatePsiElement
. In particular, token accessors usefindPsiChildByType
instead offindChildByType
and different constructor signature is needed:Grammar-Kit supports mixed AST at the moment, but currently it figures out whether a BNF rule uses mixedAST or not by reading
.class
file content of itsextends
attribute and comparing its chain of superclasses withcom.intellij.psi.impl.source.tree.CompositePsiElement
name (1, 2).Such approach requires extracting all possible superclasses and mixins of PSI nodes to a separate gradle module in order to compile it prior to Grammar-Kit invocation, which is a quite complex setup.
In this PR I've simplified it by introducing
mixedAST
attribute. If it is set totrue
(and the rule does not have a stub), Grammar-Kit will usemixedAST
style for generated PSI impls without trying to read any.class
files content at runtime. The attribute can be applied to a specific rule or globally (so a rule will be treated asmixedAST
unless it has a stub)