Preserve formatting of disabled regions#714
Preserve formatting of disabled regions#714kukimik wants to merge 1 commit intotweag:masterfrom kukimik:708_673_cut_disabled
Conversation
|
This took me longer than expected. I've fixed another bug along the way: LINE pragma in a disabled region no longer leads to an AST change error. Some thoughts and remarks about this MR:
|
|
Thanks for your effort on this PR! We chose to go with a slightly different approach (formatting the non-disabled snippets individually, see #773). Your work here is very appreciated, and your new tests are actually reused (of course, you are attributed as a coauthor). |
|
@amesgen Ok, thanks! I haven't read through #773 yet, but the solution here wasn't very clean, so I'm pretty sure you've done better. I strongly agree with the decision to remove support for #601 . It was a major pain point while I was working on this PR. I've found this behaviour inconsistent and considered it a misfeature. Perhaps I should have complained to Mark about it instead of making workarounds. |
This is meant to close #673 and close #708.
The idea is to cut out the disabled regions in the preprocessing, and paste them back in the postprocessing, instead of commenting them out.
When cutting out the disabled regions we reduce the indentation in each of them. The disabling and enabling markers bounding the region are not cut out (they are not treated as a part of the disabled region), but only normalized. Ormolu then finds the right indentation level for the disabling marker, treating it like a normal comment. In the postprocessing we paste the disabled regions back between the markers. The indentation level of a region is adjusted basing on the indentation level of the disabling marker.
I admit that the whole solution is somewhat hacky (but so is the current one). The pasting will go wrong if a new
{- ORMOLU_DISABLE -}line somehow appears in the code as a result of formatting, or if an existing one disappears. I have not found a case when this may happen, but perhaps there should be an expliciterrorin postprocessing when the number of cut-out disabled regions does not match the number of{- ORMOLU_DISABLE -}markers.The readability of the cutting/pasting code can probably be improved, and there are possibly some other details to be tweaked, but I'd love to get some feedback first.