Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Comment in empty export list moved out #906

Closed
brandonchinn178 opened this issue Jul 11, 2022 · 2 comments · Fixed by #1013
Closed

Comment in empty export list moved out #906

brandonchinn178 opened this issue Jul 11, 2022 · 2 comments · Fixed by #1013
Labels
comments Issues related to comment placement style Nitpicking and things related to purely visual aspect for formatting.

Comments

@brandonchinn178
Copy link
Collaborator

Describe the bug

module Foo
  ( {- comment -}
  )
where

gets reformatted to

module Foo
  (
  )
where

{- comment -}

Related: #901

To Reproduce

Expected behavior
It should keep the comment in the export list

Environment

  • OS name + version: MacOS
  • Version of the code: HEAD

Additional context
This isn't an issue if there's anything in the export list, before or after the comment. So it's normally not an issue, since one usually exports at least one thing. But it's messing up my new library, which generates an export item at the spot specified by the comment. I currently workaround with a hack involving named sections, which is fine for my purposes, since haddocks aren't generated for tests:

https://github.com/brandonchinn178/tasty-autocollect/blob/f4890360a4870d2740e01d2c54f424b07c0ef88d/test/Test/Tasty/Ext/TodoTest.hs#L5

@amesgen amesgen added style Nitpicking and things related to purely visual aspect for formatting. comments Issues related to comment placement labels Jul 11, 2022
@brandonchinn178
Copy link
Collaborator Author

brandonchinn178 commented Jun 9, 2023

@amesgen hm this seems to break with block comments:

  • Input:
module Foo
  ( {- asdf -}
  )
where
  • Format without checking idempotence:
module Foo
{- asdf -}(
          )
where
  • Fixed point:
module Foo
{- asdf -} (
           )
where

It's also a bit unfortunate that styling drastically changes when there is an export or not:

  • With export:
module Foo
  ( -- asdf
    asdf,
  )
where
  • Without export:
module Foo
  (
  -- asdf
  )
where

@amesgen
Copy link
Member

amesgen commented Jun 10, 2023

Thanks, that is unfortunate, I will have a look at this, especially as your PR #933 handles this properly. For some context from my POV:

  • The current comment layout logic has several very nice properties:
    • You basically don't have to think about comments while writing printing code; they are emitted automatically via located, which is very convenient and a significant reduction in cognitive load.
    • The comment printing logic is separated from printing the AST.
    • It generally produces very good to decent output, and when it does not, it is usually easy to put the comment in a slightly different place for it to work.
  • OTOH, almost all currently open bugs are related to comments (in particular idempotence), and many style requests are also related to comments (one of the most prominent ones: formatting does not preserve position of comments inside explicit imports #469).
  • It is probably possible to fix most of these issues by adding lots of special cases, and also starting to interleave comment and AST printing. Any individual such fix is probably not a big deal, but together, they could introduce significant complexity, in particular to the comparatively low severity of the issues they are resolving.
  • "Proper" solutions could include:
    • Fix these problems by identifying a self-contained change the existing comment printing logic.
    • Switch to a completely different way of printing comments, e.g. using the comment-to-AST association present in the GHC AST via TTG/EPA (trees-that-grow/exact-print-annotations); also see Refactor comment handling logic #324 for some considerations.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
comments Issues related to comment placement style Nitpicking and things related to purely visual aspect for formatting.
Projects
None yet
2 participants