How to change case in a FormattingModelBuilder?
已回答
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.
请先登录再写评论。
Hello Ralph,
This is not possible in the way you are trying to do.
FormattingModelBuilderis intended to buildFormattingModelwhich 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.postFormatProcessorExtension 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
PostFormatProcessorincom.intellij.postFormatProcessorextension point in plugin configuration file. So you should do it a similar way as you registered your formatting model builder (com.intellij.lang.formatterEP). 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.