ormolu.el icon indicating copy to clipboard operation
ormolu.el copied to clipboard

Some support for CPP

Open mrkkrp opened this issue 5 years ago • 2 comments

CPP is the reality in many files. We could add some limited support for it by:

  • Passing the --tolerate-cpp option.
  • Commenting CPP macros in some magic way before formatting and restoring them afterwards.

To be clear, the CPP problem I think is an elephant in the room because for using the formatter on real code you're going to need a way to deal with it. On the other hand it cannot be 100% correctly supported, so we're probably doing the right thing by not supporting it in the tool itself.

mrkkrp avatar Oct 20 '19 21:10 mrkkrp

I think this is already supported via the ormolu-extra-args variable. If you need to enable it for a project, you can set that variable in a .dir-locals.el file.

I think passing --tolerate-cpp by default just means that ormolu-format-on-save-mode will cause anything actually containing CPP to be potentially mangled, rather than erroring in Ormolu and saving the unformatted file, and trying to do something like commenting CPP is something that should be handled by Ormolu itself if at all. The complexity of successfully re-inserting CPP into a reformatted context is high (which is likely why it's not already in Ormolu).

sellout avatar Feb 07 '20 17:02 sellout

For what it's worth, we enable CPP in the file itself only, and decided to only run ormolu on files not matching /{-# LANGUAGE CPP/ #-}.

The resulting cleanup lead to a bunch of files where we had it turned on but didn't use it, and even more files where we are using it wrong, and should either replace the CPP blocks with conditional modules in the cabal file, or get rid of the branching altogether. The rest can be moved into small modules that do only CPP, and export only things that don't require CPP any more when being used.

I consider the fact that ormolu makes it awkward to use CPP one of the benefits rather than a drawback. Using bad features should have clear and immediate penalties. :)

fisx avatar Feb 19 '20 09:02 fisx