language injection into string literals

Hi,

I've used the grammarkit to build a custom language integration for the R language. Now I would love to allow for language injection into string literals. I've already prepared my grammar definition to generate a psiclass for string literals. However, I've no real idea how to implement the LangInjectPsiElement interface.

The javadoc of PsiLanguageInjectionHost states that for string literal injection the interface does need to be implemented at all. So maybe it's just about tagging by psiclass in some fancy way and registering it in some extension point?

Could somebody provide a minimalistic example about how to do enable language injection for string literals in custom language plugins? Or lead me to an easy/good example?  

Thanks,
Holger Brandl

6 comments
Comment actions Permalink

Hello Holger,

I've just pushed simple language injection support to Grammar-Kit.
Please have a look at this commit:
https://github.com/JetBrains/Grammar-Kit/commit/cf5a6b3e9cc3b3b691be6865b7e499a33200e3d2

Regards,
Gregory Shrago

0
Comment actions Permalink

Hello Gregory,

wow this was a quick and elaborate answer. :-) Thank you very much! Based on your nice example code, I've completed the language injection feature for my R-integration in less than 2hours.

I've one small (unrelated) comment concerning the GrammarKit: Initially, the generated parser of my plugin seemed to be pretty slow for larger documents. So I used yourkit profiler to narrow it down to the call of addVariant in com.r4intellij.lang.parser.GeneratedParserUtilBase#consumeToken. In my case addVariant was eating up half of the parsing time.
Because I use a custom completion provider, I didn't seem to need the variants, so I commented addVariant out which dramatically speeded up the generated parser.
Maybe this problem is due to my grammar definition, but I think it's because of the while loop in addVarant which might eat up lot of time when having many variants (like in a longer document).

Best,
Holger

0
Comment actions Permalink

Hello Holger,

GeneratedParserUtilBase#addVariant collects values for error reporting as well as for optional parser-based completion.
I'm sure you don't want to switch syntactic errors off so I've tuned performance a bit (Yourkit reports ~10 times faster processing).
Please try the latest GeneratedParserUtilBase:
https://github.com/JetBrains/Grammar-Kit/blob/master/support/org/intellij/grammar/parser/GeneratedParserUtilBase.java

Regards,
Gregory Shrago

0
Comment actions Permalink

Hi Gregory,

I'm looking at yout commit and if I get it right, you did basically two things:
1/ allow injection to a string literal, by BngStringImpl implements PsiLanguageInjectionHost
2/ allow the string literal to be modified from another editor window, by attaching AbstratcElementManipulator to BnfStringLiteralExpressionImpl in config.xml

It seems to me, that the first step should be enough to support language injection, isn't it? I mean in terms of syntax highlighting, parsing, validations, ...
Or do I always need the second step to be implemented as well? If so, why is this needed (if I don't need editing in another editor window)

Thanks for info

0
Comment actions Permalink

Jan,

You're right, the 2 step is optional but very useful. Lots of IntelliJ platform functionality depends on ElementManipulator API: reference providers, inspections. In case manipulator is available you can get this functionality (and whatever shows up in future) in your PSI for free.
It also helps to avoid code duplication by keeping "range-in-element" logic in one place.

Gregory Shrago

0
Comment actions Permalink

Hello Gregory,

I\ve tried your updated version of GPUB, and indeed it is much faster than before. :-)  Now It's really the different parser routines generated by the GrammarKit which consume most of the parsing time (which is how it should be). So possible perfomance bottlenecks are caused by inefficient grammar rule definitions, and by running yourkit against my parser again, I could identify (and fix!) some bad examples in my own grammar.

Thanks again for your fast support,
Holger Brandl

0

Please sign in to leave a comment.