Skip to content

Preserve formatting of disabled regions#714

Closed
kukimik wants to merge 1 commit intotweag:masterfrom
kukimik:708_673_cut_disabled
Closed

Preserve formatting of disabled regions#714
kukimik wants to merge 1 commit intotweag:masterfrom
kukimik:708_673_cut_disabled

Conversation

@kukimik
Copy link
Copy Markdown
Contributor

@kukimik kukimik commented Jun 17, 2021

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 explicit error in 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.

@mrkkrp mrkkrp changed the title cutting out disabled regions Preserve formatting of disabled regions Jun 26, 2021
@kukimik kukimik marked this pull request as draft August 2, 2021 16:49
@kukimik kukimik marked this pull request as ready for review August 23, 2021 20:33
@kukimik
Copy link
Copy Markdown
Contributor Author

kukimik commented Aug 23, 2021

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:

  • Ormolu.Processing.Common was moved to Ormolu.Processing.Disabling, as it only contains definitions related to disabling. This is analogous to Ormolu.Processing.Cpp.
  • The disabled regions could perhaps be pretty-printed in Ormolu.Parser.Result.prettyPrintParseResult (currently I just show them). I didn't find this necessary.
  • The preprocessing part is ugly. I've found it difficult to make it look better using the current structure of the code. I thought about restructuring it as a composition, more or less like: preprocess = removeLinePragmas . removeDisabledRegions . maskCPP . removeShebang, or using state to make it more readable. I decided that it's a big change, and I'd rather hear your opinion first.
  • The test cases in tests/Ormolu/Parser/ParseFailureSpec.hs could perhaps be specified using srcSpans instead of strings. The current implementation relies on the text representation of the srcSpan, which may change (but I believe it is unlikely to change), It is, at least in part, a result of me being lazy, but it is also short and readable. If you think this should be rewritten, let me know.

@amesgen
Copy link
Copy Markdown
Contributor

amesgen commented Sep 13, 2021

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).

@kukimik
Copy link
Copy Markdown
Contributor Author

kukimik commented Sep 14, 2021

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Error on "-}" in regions with disabled formatting DISABLED ormolu remove empty lines

3 participants