Skip to content

Support leading arrows - #209

Merged
brandonchinn178 merged 20 commits into
fourmolu:mainfrom
3kyro:joe-align-leading-arrow
Aug 19, 2022
Merged

brandonchinn178 merged 20 commits into
fourmolu:mainfrom
3kyro:joe-align-leading-arrow

Conversation

@3kyro

@3kyro 3kyro commented Jun 21, 2022 •

Copy link
Copy Markdown
Contributor

This is the rebasing branch for #95 . Very much work in progress
Remaining items:

  • Rebase Meat/Type
  • Fix/add tests
  • Update Changelog
  • Update README

@github-actions

Copy link
Copy Markdown

👋 @3kyro
Thank you for raising your pull request.
Please make sure you have followed our contributing guidelines in DEVELOPER.md. We will review it as soon as possible!

Reviewer: Please verify the following things have been done, if applicable.

  • CHANGELOG.md has been updated
  • "Configuration > Available options" section in README.md has been updated
  • "Configuration > Specifying configuration" section in README.md has been updated
  • fourmolu.yaml updated to stay in sync with config in README.md
  • Tests have been added

@ocharles

ocharles commented Jun 22, 2022 •

Copy link
Copy Markdown

I tried this and get things like:

commandCallbacks :: :: [Callback UpdateIssue (Maybe (M.Issue Result)) State]

@3kyro

3kyro commented Jun 22, 2022

Copy link
Copy Markdown
Contributor Author

I tried this and get things like:

commandCallbacks :: :: [Callback UpdateIssue (Maybe (M.Issue Result)) State]

Hey, sorry but I'm still in the process of rebasing the original PR, so it's bound to be broken in some way 😃

@3kyro
3kyro marked this pull request as draft June 22, 2022 14:40
@ocharles

Copy link
Copy Markdown

Got it 👍 Good luck! Can't wait to play with this one - I'll wait until you're ready 😄

@3kyro
3kyro force-pushed the joe-align-leading-arrow branch 2 times, most recently from e80a57c to 52d4f39 Compare June 26, 2022 21:10
@3kyro

3kyro commented Jul 3, 2022

Copy link
Copy Markdown
Contributor Author

@expipiplus1 I've also created a PR that targets your original branch :)

@3kyro
3kyro marked this pull request as ready for review July 4, 2022 15:00
@expipiplus1

Copy link
Copy Markdown
Contributor

Thanks, probably best to stick to this PR! Not sure of the value of me merging into my branch at this point haha

@arybczak

arybczak commented Aug 2, 2022

Copy link
Copy Markdown

Ping. Is this still Wip as the title suggests?

@brandonchinn178 brandonchinn178 added the new config option Adds or would add new configuration option label Aug 5, 2022
@3kyro 3kyro changed the title Wip leading arrows leading arrows Aug 11, 2022
@3kyro 3kyro changed the title leading arrows leading arrows rebased branch Aug 11, 2022
@3kyro
3kyro force-pushed the joe-align-leading-arrow branch from 3190f1a to 70f309a Compare August 11, 2022 20:18
@3kyro

3kyro commented Aug 11, 2022

Copy link
Copy Markdown
Contributor Author

Thanks, probably best to stick to this PR! Not sure of the value of me merging into my branch at this point haha

@brandonchinn178 I think this PR can be reviewed now. I've added a small set of tests in 9e72445 that should probably be expanded a bit (maybe @expipiplus1 could help :) )

cc @arybczak

@brandonchinn178 brandonchinn178 linked an issue Aug 12, 2022 that may be closed by this pull request
@brandonchinn178 brandonchinn178 mentioned this pull request Aug 12, 2022
@brandonchinn178 brandonchinn178 changed the title leading arrows rebased branch Support leading arrows Aug 12, 2022

@brandonchinn178 brandonchinn178 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR! Still looking through, left some initial thoughts

Comment thread CHANGELOG.md Resolved
Comment thread README.md Resolved
Comment thread README.md Resolved
Comment thread data/fourmolu/leading-arrows/input.hs Resolved
Comment thread data/fourmolu/leading-arrows/input.hs Resolved
GT

functionName
:: (C1, C2, C3, C4, C5)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there's value in keeping :: next to the identifier: #86 (comment)

Similar to what happened with diff-friendly-import-export, it would be nice if leading-arrows didn't change two things: the placement of :: and the placement of ->. I think it would be more consistent, style-wise, to do something like

foo ::
  a
  -> String
  -> Bool

foo2 ::
  Show a =>
  a
  -> String
  -> Bool

foo3 ::
  ( Show a
  , Eq a
  ) =>
  a
  -> String
  -> Bool

foo4 ::
  forall a.
  ( Show a
  , Eq a
  ) =>
  a
  -> String
  -> Bool

My reasoning:

  • The main part of the type a -> String -> Bool is visually separate from the constraints and is formatted the same, whether there are constraints or not
  • Keeping the fat arrow with the multiline constraints also avoids a lonely ) on its own line
  • Similarly, constraints are formatted the same whether there's an explicit forall or not
  • Avoids double indenting when constraints are on multiple lines:
    -- the constraints are all an extra three spaces over (5 spaces total from `foo`)
    foo
      :: ( MonadLogger m
         , MonadIO m
         , MonadCatch m
         , MonadReader MyEnv m
         )
    
    -- vs. only two spaces from `foo`
    foo ::
      ( MonadLogger m
      , MonadIO m
      , MonadCatch m
      , MonadReader MyEnv m
      )

Thoughts?

@expipiplus1 expipiplus1 Aug 12, 2022 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👎 to this IMO. This design has been in the making for more than a year and it would be a real shame to delay it any further over what seems to me like quite a controversial last-minute suggestion.

While I think that a strong argument can be made for keeping :: on the same line as the function name (to aid greppability), I think it would be horrible to end up with something like the following:

foo ::
  C =>
  D =>
  E =>
  f
  -> g
  -> h

This isn't some weird case nobody would ever write, I've observed this pattern in real-world code.

I definitely wouldn't object to a future PR, adding a no-::-or-=> option alongside true and false

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This design has been in the making for more than a year and it would be a real shame to delay it any further over what seems to me like quite a controversial last-minute suggestion.

Where was it discussed for this year? I have seen zero discussion about this, so from my perspective, this is a big design decision that I'm being asked to merge with zero feedback.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#95

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry the the brusque tone! Thank you for the work you're doing.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good with me!

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That sounds like an excellent idea, @3kyro :)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Leaving :: or => dangling will diverge from the style align type stuff on the left. I don't think I've ever seen such code in the wild. Personally, I would be very surprised (and, honestly, annoyed) if applying a "leading-arrows" config to my codebase would produce a diffsplosion introducing the shuffle.

function-arrows: leading/trailing is nice, though.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The lack of :: on the same line as the symbol being defined has actually made me reconsider whether I want this. I appreciate all the thoughts about how to get that, but I will probably not end up using leading-arrows after all. That said, one more suggestion from a colleague at work was to do things like:

foo :: ()
  => X
  -> Y
  -> Z

which I thought was quite clever. You basically always have a => with this style.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know if this is of topic, of this pull request, but I really like @ocharles suggestion, however it has a drawback, namely that it will introduce syntax to a program. I'm not sure if this is against the goal of the project. You could claim that the user just have to all it themselves, they want even prettier aligning of arrows. This means that the following are both valid formatted functions:

uglyFun :: 
     a 
  -> c

prettyFun :: ()
  => a 
  -> c

The solution do however require that 'preamble' (foralls and constraints) and args are treated differently. namely that the preamble can be on one line while the args are on multiple lines and visa versa, essentially that all of these examples are valid results depending on the indentation of the user:

fun :: forall a b. ( C1, C2 ) => a -> c

fun :: forall a b. ( C1, C2 ) 
  => a -> c

fun :: forall a b. ( C1, C2 ) 
  => a 
  -> c


fun :: forall a b. 
  ( C1, C2 ) 
  => a -> c

fun :: forall a b. 
  ( C1
  , C2
  ) 
  => a -> c


fun :: forall a b. 
  ( C1, C2 ) 
  => a 
  -> c

fun :: forall a b. 
  ( C1
  , C2
  ) 
  => a 
  -> c

fun :: 
  forall a b. 
  ( C1
  , C2
  ) 
  => a 
  -> c

Comment thread src/Ormolu/Printer/Internal.hs Resolved
Comment thread src/Ormolu/Printer/Combinators.hs Resolved
Comment thread src/Ormolu/Printer/Combinators.hs Resolved
Comment thread src/Ormolu/Printer/Meat/Type.hs Resolved
Comment thread src/Ormolu/Printer/Meat/Type.hs Resolved
Comment thread src/Ormolu/Printer/Meat/Type.hs Resolved
Comment thread src/Ormolu/Printer/Meat/Type.hs Resolved
@3kyro
3kyro force-pushed the joe-align-leading-arrow branch from baa4477 to ea453be Compare August 17, 2022 13:18
@3kyro
3kyro force-pushed the joe-align-leading-arrow branch from ea453be to 4209dc4 Compare August 17, 2022 13:24
@3kyro
3kyro force-pushed the joe-align-leading-arrow branch from 586b9b5 to 8151c59 Compare August 17, 2022 22:10
@3kyro

3kyro commented Aug 17, 2022 •

Copy link
Copy Markdown
Contributor Author

@brandonchinn178 @expipiplus1 I think I've addressed the initial comments, keeping the existing behavior for function-arrows:leading as discussed. Tomorrow I'll format our sufficiently large work codebase to have a look for edge cases as well.

@3kyro
3kyro force-pushed the joe-align-leading-arrow branch from 697ae5a to 9bd0e35 Compare August 17, 2022 23:03
@3kyro
3kyro requested a review from expipiplus1 August 17, 2022 23:44

@brandonchinn178 brandonchinn178 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for getting through all the comments. Just a few more. The trickiest part of this PR is making sure merging Ormolu stuff back isn't going to be a pain. I think if we can resolve the last few comments and utilize scPrevTypeCtx more instead of returning + passing PrevTypeCtx, we'll be able to reduce the diff even more.

Comment on lines +49 to +51
instance Eq Int where
(==) :: Int -> Int -> Bool
(==) _ _ = False

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Continuing conversation from #209 (comment)

Can the "test where type is on one line" be a normal function instead of a class instance? Or possibly one of both? And also add a comment saying "testing one-line type"?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added singleLineFun

Comment thread src/Ormolu/Printer/Meat/Type.hs Outdated
Comment on lines +81 to +87
a <- case tele of
HsForAllInvis _ bndrs -> p_forallBndrs' ForAllInvis p_hsTyVarBndr bndrs
HsForAllVis _ bndrs -> p_forallBndrs' ForAllVis p_hsTyVarBndr bndrs
a' <-
getPrinterOpt poFunctionArrows >>= \case
LeadingArrows | multilineArgs -> pure a
_ -> p_after False a $> TypeCtxStart

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of threading the PrevTypeCtx everywhere, why not have p_forallBndrs' call a new setPrevTypeCtx function and p_after call getPrevTypeCtx?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added setPrvTypeCtx

p_rdrName name
space
txt "::"
trailingArrowType -- TODO, the space in tAT wasn't here before

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what does this TODO mean?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure, maybe @expipiplus1 can help? Seems to not be relevant anymore though so I've removed it.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

honestly no idea, it definitely looks as though there was a space there before... Makes sense to remove it, I guess it was something stale from dev


-- | Like 'p_hsType' but indent properly following a forall
p_hsType :: HsType GhcPs -> R ()
p_hsType t = p_hsType' (hasDocStrings t) PipeStyle t

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how did it work without layout == Multiline here?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe it's needed now for splitting foralls and contraints which can't be handled by interArgBreak as before.

Comment thread src/Ormolu/Printer/Meat/Type.hs Outdated
TypeCtxArgument -> txt "->" >> space

-- | like p_after but only when we're rendering leading arrows
p_leadingAfter :: Bool -> PrevTypeCtx -> R ()

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

seems like we can inline this?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

Comment thread src/Ormolu/Printer/Meat/Type.hs Outdated
then newline
else breakpoint
p_hsTypeR = p_hsType' multilineArgs docStyle
p_hsTypeR prevTypeCtx m = withPrevTypeCtx prevTypeCtx $ do

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh if this is the only place withPrevTypeCtx is used, let's just inline it here

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Inlinening it here would require exporting the R constructor from Ormolu.Printer.Internal though. Maybe we can leave it in the Internal module like all the other functions that manipulate the underlying monads of R?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually implementing setPrevTypeCtx makes withPrevTypeCtx obsolete

@3kyro
3kyro requested review from brandonchinn178 and expipiplus1 and removed request for brandonchinn178 and expipiplus1 August 18, 2022 09:55
@brandonchinn178

Copy link
Copy Markdown
Collaborator

Looks great! Is this ready to merge, or are you still testing?

@3kyro

3kyro commented Aug 19, 2022

Copy link
Copy Markdown
Contributor Author

Looks great! Is this ready to merge, or are you still testing?

I think it can be merged. It would be best to squash the commits though as they are a bit messy due to the rebase.

@3kyro

3kyro commented Aug 19, 2022

Copy link
Copy Markdown
Contributor Author

@brandonchinn178 Do you have an estimate on when we can expect a release? We're quite eager to switch to fourmolu after this 🙂

@brandonchinn178

Copy link
Copy Markdown
Collaborator

I'll make a release within a week. Thanks for the hard work!

@3kyro
3kyro deleted the joe-align-leading-arrow branch September 20, 2022 19:32
@expipiplus1

Copy link
Copy Markdown
Contributor

Thanks @brandonchinn178 and @3kyro!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

new config option Adds or would add new configuration option

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Leading arrows in types

9 participants