ennocramer / floskell

Floskell is a flexible Haskell source code pretty printer.
BSD 3-Clause "New" or "Revised" License
178 stars 22 forks source link

Proposal: better CPP support and disabling regions #75

Closed baulig closed 11 months ago

baulig commented 1 year ago

It seems to me that Floskell supports some basic CPP constructs, but is still missing full hsc2hs support.

I have already fixed a few of the more trivial issues in my fork but this is still far from complete, and I would like to write some tests prior to submitting a PR for it.

Adding support for #const, #enum and multi-line directives was not too hard - for instance, Floskell now just silently skips over the following:

#enum CInt,         \
  , fs = CTL_FS     \
  , hw = CTL_HW     \
  , kern = CTL_KERN \
  , net = CTL_NET   \
  , vfs = CTL_VFS   \
  , mem = CTL_VM

How would you feel about adding an option to disable an entire region? I was using brittany before before it became unmaintained and unsupported, and it had a way of doing so by inserting special comments.

For instance, we could do something like this:

-- floskell-begin-disable-region
newtype NativeInt = NativeInt #{type int}
    deriving stock (Eq, Ord, Show)
    deriving newtype Storable
-- floskell-end-disable-region

This is also an example for something that isn't working yet; so far, I only looked at the line-based filter that's called right in the beginning and still need to figure out how to do that #{type int} thing.

ennocramer commented 1 year ago

This is fantastic. I would love to integrate these changes into Floskell. Would you be willing to separate this into a separate PR without the test suite changes for now?

One comment on the implementation. You're using let pos = length (biLines iter) + length (biComments iter) to compute the line number for the SrcSpan. I think this will lead to quadratic runtime in file with a large number of comments. It's probably better to add a dedicated line counter to BlockIter.

ennocramer commented 11 months ago

I have added your improvements wrt multi-line CPP directives, #enum directives, and disabled regions in floskell-0.11.0. At this point, I think full hsc2hs support is not viable, but it might be possible to accept and format some additional hsc2hs features.