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

Inline comment to a record constructor leads to non idempotent behavior #426

Open
Lev135 opened this issue Nov 6, 2022 · 2 comments · May be fixed by #431
Open

Inline comment to a record constructor leads to non idempotent behavior #426

Lev135 opened this issue Nov 6, 2022 · 2 comments · May be fixed by #431

Comments

@Lev135
Copy link
Contributor

Lev135 commented Nov 6, 2022

Initial formatting

data Foo
  = Foo { foo :: Int } -- ^ foo
  | Bar

after the first run

data Foo
  = Foo
  { foo :: Int
  } -- ^ foo
  | Bar

after the second run

data Foo
  = Foo
  { foo :: Int
  }
  -- ^ foo
  | Bar

It was discovered by me at the same time as #425, but I think that this is another issue

@Lev135
Copy link
Contributor Author

Lev135 commented Nov 8, 2022

It seems for me that I found a problem: for input

data Foo
  = Foo { foo :: Int } -- ^ foo

blockStart of a comment -- ^ foo equals blockStart of a data constructor Foo { foo :: Int }, therefore NextItemWithComment is returned from takeNext function. At the same time, for

data Foo
  = Foo
  { foo :: Int
  } -- ^ foo

NextItem is returned and then NextComment and then work from commentGroups add this comment not to the second part of cgItems, but to the following section.

The question is open: what's our choice to fix this. In other words, what is the comment for a record constructor, is should it be attached to the constructor as inline comment, or after it as a following comment, splitting CommentGroup into several parts. In both cases, we need some changes. Anyway, I don't think current (non idempotential) behavior is acceptable, so we need to choose one of the styles and update data printer.

I'll try to implement one of these cases or maybe even both to compare results, when I have enough time. Your opinion is wellcome!

@Lev135
Copy link
Contributor Author

Lev135 commented Nov 8, 2022

Oh, I found a very nice solution! It's just to replace a check in takeNext from beginning of the previos block to the end of it:

 takeNext ((ib, i) : items) ((cb, c) : comments)
-    | blockStart ib == blockStart cb =
+    | blockEnd ib == blockStart cb =
         Just (ib <> cb, NextItemWithComment i c, items, comments)
-    | blockStart ib < blockStart cb =
+    | blockEnd ib < blockStart cb =
         Just (ib, NextItem i, items, (cb, c) : comments)
     | otherwise =
         Just (cb, NextComment c, (ib, i) : items, comments)

I don't understand, why it was blockStart ib here. Maybe I miss something critical, but no test was breaken. I'll open a pull request soon, but it's based on #427, so that should be elaborated and merged before this one.

Sorry for being too hurry sometimes and writing some not thoughtful enough comments (and then deliting them).

@Lev135 Lev135 changed the title Reformatting of haddoc comments is not idempotent Inline comment to a record constructor lead to non idempotent behavior Nov 9, 2022
@Lev135 Lev135 changed the title Inline comment to a record constructor lead to non idempotent behavior Inline comment to a record constructor leads to non idempotent behavior Jan 1, 2023
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 a pull request may close this issue.

1 participant