How to change case in a FormattingModelBuilder?
Answered
For my custom language plugin, I've created custom Block and FormattingModelBuilder classes that change indenting and spacing of my language.
But how can I change the case of some blocks? I've found no way to change text using a Block instance.
I've tried creating an independent case-setting method invoked in FormattingModelBuilder.createModel, but createModel is called on startup, not when Format Code is selected.
Please sign in to leave a comment.
Hello Ralph,
This is not possible in the way you are trying to do.
FormattingModelBuilder
is intended to buildFormattingModel
which provides information about:So it's only about inserting whitespaces between non-whitespace tokens.
The good news is that you can modify non-whitespace tokens by implementing
com.intellij.postFormatProcessor
Extension Point.You can find some examples to base on here: https://jb.gg/ipe?extensions=com.intellij.postFormatProcessor
Excellent! Thank you very much for your explanation and solution.
OK, now I have the PostFormatProcessor, but how do I make use of it? First In the IDE? And then how do I invoke it from FormattingModelBuilder?
Hi Ralph,
You have to register
PostFormatProcessor
incom.intellij.postFormatProcessor
extension point in plugin configuration file. So you should do it a similar way as you registered your formatting model builder (com.intellij.lang.formatter
EP). There is no need to invoke the post-processor manually - it will be done by the platform code.We also extended docs to mention this EP: https://plugins.jetbrains.com/docs/intellij/code-formatting.html#non-whitespace-modifications
Thanks, Karol, you're right, I erroneously checked for PsiLiteral while excluding it at the same time. Everything works now.