From 860c8771aeb0745b26a8f8212bd8b512a022f9eb Mon Sep 17 00:00:00 2001 From: Lennart Spitzner Date: Fri, 12 May 2023 12:02:19 +0000 Subject: [PATCH] Fix issue with indentation after block-comments --- data/10-structured/op-precedence.blt | 4 +- data/11-extensions/datakinds.blt | 8 ++-- data/15-regressions.blt | 8 ++++ .../Brittany/Internal/S4_WriteBriDoc.hs | 44 ++++++++++++++++--- .../Internal/WriteBriDoc/Operators.hs | 2 +- 5 files changed, 53 insertions(+), 13 deletions(-) diff --git a/data/10-structured/op-precedence.blt b/data/10-structured/op-precedence.blt index ef32d59..bedef43 100644 --- a/data/10-structured/op-precedence.blt +++ b/data/10-structured/op-precedence.blt @@ -145,7 +145,7 @@ doop = #expected doop = some long invocation == loooooooooongman + (third nested expression) - 4 - && {- meow -} 5 - 6 > 7 `mod` loooooooooongwoman + && {- meow -} 5 - 6 > 7 `mod` loooooooooongwoman || ill just invoke a function with these args || foo && dooasdoiaosdoi ** oaisdoioasido @@ -165,7 +165,7 @@ doop = -- brittany { lconfig_fixityBasedAddAlignParens: True } doop = ( ( some long invocation == loooooooooongman + (third nested expression) - 4 - && {- meow -} 5 - 6 > 7 `mod` loooooooooongwoman + && {- meow -} 5 - 6 > 7 `mod` loooooooooongwoman ) || ill just invoke a function with these args || ( foo diff --git a/data/11-extensions/datakinds.blt b/data/11-extensions/datakinds.blt index 57ca3e5..8d74979 100644 --- a/data/11-extensions/datakinds.blt +++ b/data/11-extensions/datakinds.blt @@ -57,9 +57,9 @@ test = Proxy @'[{- comment -} 'True ] #test with-comment-4 test :: Proxy '[{- comment -} - 'True ] + 'True ] test = Proxy @'[{- comment -} - 'True ] + 'True ] #test with-comment-5 test @@ -69,9 +69,9 @@ test = Proxy @'[{- comment -} True] #test with-comment-6 test :: Proxy '[{- comment -} - True] + True] test = Proxy @'[{- comment -} - True] + True] #test explicit-list-type non-promoted type Foo = '[Bool, Bool, Bool] diff --git a/data/15-regressions.blt b/data/15-regressions.blt index 2613f72..caabd04 100644 --- a/data/15-regressions.blt +++ b/data/15-regressions.blt @@ -985,3 +985,11 @@ instance BMGuwigoc POtfuggj where #test bang-type data LabeledBool = LabeledBool !Int !Bool + +#test block-comment after let +func = do + let cipInuihz = Vuozczdm.yhEfun tuhiuRasohy + let {- CAZEB mecwbd "Caa ||" -} + doeeRbaviceQzymin a b = olivhuwqbaq + iqnz biwomeJhhujy _ _ | biwomeJhhujy < volpoqAsizmHdwpl = pure 0 + pure True diff --git a/source/library/Language/Haskell/Brittany/Internal/S4_WriteBriDoc.hs b/source/library/Language/Haskell/Brittany/Internal/S4_WriteBriDoc.hs index 40d2e50..ac1b65a 100644 --- a/source/library/Language/Haskell/Brittany/Internal/S4_WriteBriDoc.hs +++ b/source/library/Language/Haskell/Brittany/Internal/S4_WriteBriDoc.hs @@ -275,21 +275,53 @@ layoutBriDocM = \case layoutBriDocM bd BDFlushCommentsPrior loc bd -> do comms <- takeBefore loc + startCurY <- mGet <&> _lstate_curY printComments comms mModify (\s -> s + CommentCounter (length comms)) mModify $ \s -> s { _lstate_markerForDelta = Nothing - , _lstate_plannedSpace = case _lstate_markerForDelta s of + , _lstate_plannedSpace = + case _lstate_markerForDelta s of Nothing -> _lstate_plannedSpace s Just m -> let p1 = (srcLocLine m, srcLocCol m) p2 = (srcLocLine loc, srcLocCol loc) - -- traceShow (m, ExactPrint.pos2delta p1 p2) $ pure () - in case ExactPrint.pos2delta p1 p2 of - SameLine{} -> _lstate_plannedSpace s + in -- trace ("_lstate_plannedSpace = " ++ show (_lstate_plannedSpace s) + -- ++ ", _lstate_markerForDelta = " ++ show (_lstate_markerForDelta s) + -- ++ ", _lstate_curY = " ++ show (_lstate_curY s) + -- ++ ", p1 = " ++ show p1 + -- ++ ", p2 = " ++ show p2 + -- ++ ", startCurY = " ++ show startCurY + -- -- ++ ", delta = " ++ show (ExactPrint.pos2delta p1 p2) + -- ) $ + case ExactPrint.pos2delta p1 p2 of + SameLine iDelta -> case _lstate_plannedSpace s of + -- There is a delta after a marker. That should only happen + -- after a block-comment. And the marker-delta is SameLine, + -- so if layout doesn't force a newline we respect the marker + -- delta (i.e. respect whitespace immediately after a block + -- comment) or we just use the plan. + PlannedNone -> PlannedSameline iDelta + PlannedSameline i -> PlannedSameline (max (iDelta - 0) i) + p@PlannedNewline{} -> p + p@PlannedDelta{} -> p DifferentLine n _ -> case _lstate_plannedSpace s of - PlannedNone -> PlannedNone - PlannedSameline i -> PlannedDelta n (_lstate_curY s + i) + -- Marker with DifferentLine delta means that we want to + -- reproduce the newlines that were present in the input. + -- These will be emptylines in cases where it makes sense to + -- reproduce them, e.g. between statements in a do-block. + PlannedNone -> PlannedDelta n startCurY + -- ^^^^^^^^^ + -- + -- after a block-comment, _lstate_curY + -- matches the end of the comment. + -- But to not break layout we need to + -- restore the original. + -- E.g. consider > let {-comm-} + -- > x = 13 + -- + -- vvvvvvvvvvvvvvv + PlannedSameline i -> PlannedDelta n (startCurY + i) PlannedNewline{} -> PlannedNewline n PlannedDelta _ i -> PlannedDelta n i } diff --git a/source/library/Language/Haskell/Brittany/Internal/WriteBriDoc/Operators.hs b/source/library/Language/Haskell/Brittany/Internal/WriteBriDoc/Operators.hs index 65d2dfe..d61a306 100644 --- a/source/library/Language/Haskell/Brittany/Internal/WriteBriDoc/Operators.hs +++ b/source/library/Language/Haskell/Brittany/Internal/WriteBriDoc/Operators.hs @@ -152,7 +152,7 @@ layoutWriteComment absolute isBlock dp commentLines s = do -- TODO92 we don't mo mSet state { _lstate_plannedSpace = if isBlock then case _lstate_plannedSpace state of - PlannedNone -> PlannedSameline 1 + p@PlannedNone -> p p@PlannedSameline{} -> p PlannedNewline l -> if l <= y then PlannedSameline 1 else PlannedNewline (l - y)