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.

5 comments
Comment actions Permalink

Hello Ralph,

This is not possible in the way you are trying to do. FormattingModelBuilder is intended to build FormattingModel which provides information about:

Defines the formatting model for a file. A formatting model defines how a file is broken into non-whitespace blocks and different types of whitespace (alignment, indents and wraps) between them.

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

0
Comment actions Permalink

Excellent!  Thank you very much for your explanation and solution.

0
Comment actions Permalink

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?

0
Comment actions Permalink

Hi Ralph,

You have to register PostFormatProcessor in com.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

0
Comment actions Permalink

Thanks, Karol, you're right, I erroneously checked for PsiLiteral while excluding it at the same time.  Everything works now.

0

Please sign in to leave a comment.