Formatter losing indent with code selection
Hello,
question about formatter - when formatting whole code all looks ok and reformatting a file changes nothing.

“aa” is a statement consisting of “aa” expression and “\n” as END_STMT token - this has NORMAL indent as seen above.
Problem is that once I select “aa” and reformat it - it loses that indent and results into:

My guess is, that it is somehow related to selection is only at expression level (which has NONE indent) without that “\n” end_stmt which together has NORMAL indent, but as “aa” is still a child Block of “aa\n” shouldn't it be the same?
Even weirder is, that when I delete “ss” line, it works well and does not remove that Indent.
I have no idea, how to debug that case, when there's a selection or what might go wrong - I've even tried to remove all alignments to be sure it's not somehow aligned to “ss” expression.
Does anyone have an idea, why selection results into different formatting or how to debug it?
Problem is, that since I'm using postfix templates they (probably after removing that postfix f.e.: “.null”) it runs a formatter on that “aa” expression and thus breaks the code before it applies that postfix template which then as well results in broken syntax.
Please sign in to leave a comment.
Here are two guesses you can follow up on. First, “aa” and “ss” seem to be underlined as errors. Why is that? Formatting can behave differently when there are errors in the file, not picking up the right indent. Can you create code where “aa” and “ss” are replaced with something syntactically correct and try again?
On a similar note, could you debug your block-building code after re-reading the (recently updated) docs here:
https://plugins.jetbrains.com/docs/intellij/code-formatting.html#building-the-block-tree
Pay attention to the difference between formatting the whole file and the file with the selection. I would also debug into Incomplete() and getChildAttributes() if they are called. In the end, I think you just need to debug how your blocks are built, and it's best to do it using a small example like the one you showed.
I'm afraid without code, it's really hard to give further advice.
Errors are just from Annotators “Variable not defined”, disabling that or adding declaration doesn't affect this case.
I'll check Incomplete() method I haven't touched that at all when implementing Formatter, but current default implementation inside AbstractBlock in both cases (with or without selection) returns false for all blocks.
getChildAttributes does not get called at all - which seems is ok for formatting a file as this is called f.e. after pressing Enter to decide what Indent should be on a new line.
I'll dive into in and post an update should I figure something, thank you and I'll definitely check the docs again.