Fix issue with indentation after block-comments

ghc92
Lennart Spitzner 2023-05-12 12:02:19 +00:00
parent 47bcdb045b
commit 860c8771ae
5 changed files with 53 additions and 13 deletions

View File

@ -985,3 +985,11 @@ instance BMGuwigoc POtfuggj where
#test bang-type #test bang-type
data LabeledBool = LabeledBool !Int !Bool 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

View File

@ -275,21 +275,53 @@ layoutBriDocM = \case
layoutBriDocM bd layoutBriDocM bd
BDFlushCommentsPrior loc bd -> do BDFlushCommentsPrior loc bd -> do
comms <- takeBefore loc comms <- takeBefore loc
startCurY <- mGet <&> _lstate_curY
printComments comms printComments comms
mModify (\s -> s + CommentCounter (length comms)) mModify (\s -> s + CommentCounter (length comms))
mModify $ \s -> s mModify $ \s -> s
{ _lstate_markerForDelta = Nothing { _lstate_markerForDelta = Nothing
, _lstate_plannedSpace = case _lstate_markerForDelta s of , _lstate_plannedSpace =
case _lstate_markerForDelta s of
Nothing -> _lstate_plannedSpace s Nothing -> _lstate_plannedSpace s
Just m -> Just m ->
let p1 = (srcLocLine m, srcLocCol m) let p1 = (srcLocLine m, srcLocCol m)
p2 = (srcLocLine loc, srcLocCol loc) p2 = (srcLocLine loc, srcLocCol loc)
-- traceShow (m, ExactPrint.pos2delta p1 p2) $ pure () in -- trace ("_lstate_plannedSpace = " ++ show (_lstate_plannedSpace s)
in case ExactPrint.pos2delta p1 p2 of -- ++ ", _lstate_markerForDelta = " ++ show (_lstate_markerForDelta s)
SameLine{} -> _lstate_plannedSpace 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 DifferentLine n _ -> case _lstate_plannedSpace s of
PlannedNone -> PlannedNone -- Marker with DifferentLine delta means that we want to
PlannedSameline i -> PlannedDelta n (_lstate_curY s + i) -- 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 PlannedNewline{} -> PlannedNewline n
PlannedDelta _ i -> PlannedDelta n i PlannedDelta _ i -> PlannedDelta n i
} }

View File

@ -152,7 +152,7 @@ layoutWriteComment absolute isBlock dp commentLines s = do -- TODO92 we don't mo
mSet state mSet state
{ _lstate_plannedSpace = if isBlock { _lstate_plannedSpace = if isBlock
then case _lstate_plannedSpace state of then case _lstate_plannedSpace state of
PlannedNone -> PlannedSameline 1 p@PlannedNone -> p
p@PlannedSameline{} -> p p@PlannedSameline{} -> p
PlannedNewline l -> PlannedNewline l ->
if l <= y then PlannedSameline 1 else PlannedNewline (l - y) if l <= y then PlannedSameline 1 else PlannedNewline (l - y)