tweag / ormolu

A formatter for Haskell source code
https://ormolu-live.tweag.io
Other
958 stars 83 forks source link

Allow disabling of formatting by adding a magic comment #435

Closed scott-fleischman closed 4 years ago

scott-fleischman commented 4 years ago

I am experimenting with using ormolu with "format on save" in my editor, and sometimes I wish to edit code without updating all of the styling.

One idea that came to mind is having some comment that I could put near the top of a file that ormolu can see and would choose not to style the file. I searched a bit and didn't see an existing feature like that.

It could be useful for two cases: 1) the case I am in, so I can add the line to a file and not have it updated 2) to allow some files to not fail the build if using ormolu in check mode

The cases in particular that I would like to preserve formatting are ones with grid-like vertical alignment that end up looking like a table. I don't foresee ormolu supporting this kind of style in the future.

It's possible one might want to disable a smaller portion of code, such as a top-level definition that is aligned in this way.

neongreen commented 4 years ago

The cases in particular that I would like to preserve formatting are ones with grid-like vertical alignment that end up looking like a table. I don't foresee ormolu supporting this kind of style in the future.

This sounds like a good usecase. Another usecase is "we have code that Ormolu breaks on, but we want to start using Ormolu in production anyway".

It could be nicely supported via the {-# ANN #-} mechanism.

mboes commented 4 years ago

Escape hatches are good. {-# ANN #-} could well work, as @neongreen suggests.

judah commented 4 years ago

I'd recommend against ANN comments for this sort of annotation. They can increase compilation times or cause more recompilation, since they're evaluated at compile time and require loading dependencies (similar to TemplateHaskell). http://neilmitchell.blogspot.com/2019/01/ignoring-hlint.html

Regular comments {- ORMOLU ... -} could work. Hlint now allows something similar, as described in the above link.