Fix block-comment delta position special case
parent
b116529005
commit
72c9e4c3ab
|
@ -660,7 +660,7 @@ instance ObtainAnnDeltaPos (EpAnn GHC.AnnsModule) where
|
||||||
loc <- obtainAnnPos l kw <|> obtainAnnPos annList kw
|
loc <- obtainAnnPos l kw <|> obtainAnnPos annList kw
|
||||||
let pos = (GHC.srcLocLine loc, GHC.srcLocCol loc)
|
let pos = (GHC.srcLocLine loc, GHC.srcLocCol loc)
|
||||||
pure $ ExactPrint.pos2delta
|
pure $ ExactPrint.pos2delta
|
||||||
(maximum $ (0, 1) :
|
(maximum $ (1, 1) :
|
||||||
[ ExactPrint.ss2posEnd $ GHC.anchor anch
|
[ ExactPrint.ss2posEnd $ GHC.anchor anch
|
||||||
| L anch _ <- case epaComms of
|
| L anch _ <- case epaComms of
|
||||||
EpaCommentsBalanced cs1 cs2 -> cs1 ++ cs2
|
EpaCommentsBalanced cs1 cs2 -> cs1 ++ cs2
|
||||||
|
|
|
@ -52,8 +52,8 @@ import Language.Haskell.Brittany.Internal.Components.BriDoc
|
||||||
-- import Language.Haskell.Brittany.Internal.S3_ToBriDocTools
|
-- import Language.Haskell.Brittany.Internal.S3_ToBriDocTools
|
||||||
|
|
||||||
|
|
||||||
ppBriDoc :: BriDocNumbered -> PPMLocal ()
|
ppBriDoc :: BriDocNumbered -> Bool -> PPMLocal ()
|
||||||
ppBriDoc briDoc = do
|
ppBriDoc briDoc flush = do
|
||||||
-- first step: transform the briDoc.
|
-- first step: transform the briDoc.
|
||||||
briDoc' <- MultiRWSS.withMultiStateS BDEmpty $ do
|
briDoc' <- MultiRWSS.withMultiStateS BDEmpty $ do
|
||||||
-- Note that briDoc is BriDocNumbered, but state type is BriDoc.
|
-- Note that briDoc is BriDocNumbered, but state type is BriDoc.
|
||||||
|
@ -107,6 +107,7 @@ ppBriDoc briDoc = do
|
||||||
$ MultiRWSS.withMultiStateS ([] :: [LEpaComment])
|
$ MultiRWSS.withMultiStateS ([] :: [LEpaComment])
|
||||||
$ do
|
$ do
|
||||||
layoutBriDocM briDoc'
|
layoutBriDocM briDoc'
|
||||||
|
when flush layoutFlushLine
|
||||||
case _lstate_plannedSpace state' of
|
case _lstate_plannedSpace state' of
|
||||||
PlannedNone -> if _lstate_curY state' == 0
|
PlannedNone -> if _lstate_curY state' == 0
|
||||||
then pure ()
|
then pure ()
|
||||||
|
@ -302,21 +303,22 @@ takeBefore loc = do
|
||||||
|
|
||||||
printComments :: LayoutConstraints m => [GHC.LEpaComment] -> m ()
|
printComments :: LayoutConstraints m => [GHC.LEpaComment] -> m ()
|
||||||
printComments comms = do
|
printComments comms = do
|
||||||
let addComment s anchor prior = do
|
let addComment isBlock s anchor prior = do
|
||||||
case anchor of
|
case anchor of
|
||||||
Anchor span UnchangedAnchor -> layoutWriteComment
|
Anchor span UnchangedAnchor -> layoutWriteComment
|
||||||
True
|
True
|
||||||
|
isBlock
|
||||||
(ExactPrint.ss2deltaEnd prior span)
|
(ExactPrint.ss2deltaEnd prior span)
|
||||||
1
|
1
|
||||||
(Text.pack s)
|
(Text.pack s)
|
||||||
Anchor _span (MovedAnchor dp) ->
|
Anchor _span (MovedAnchor dp) ->
|
||||||
layoutWriteComment False dp 1 (Text.pack s)
|
layoutWriteComment False isBlock dp 1 (Text.pack s)
|
||||||
comms `forM_` \case
|
comms `forM_` \(L anch (EpaComment tok prior) ) -> case tok of
|
||||||
L anch (EpaComment (EpaDocCommentNext s) prior) -> addComment s anch prior
|
EpaDocCommentNext s -> addComment False s anch prior
|
||||||
L anch (EpaComment (EpaDocCommentPrev s) prior) -> addComment s anch prior
|
EpaDocCommentPrev s -> addComment False s anch prior
|
||||||
L anch (EpaComment (EpaDocCommentNamed s) prior) -> addComment s anch prior
|
EpaDocCommentNamed s -> addComment False s anch prior
|
||||||
L anch (EpaComment (EpaDocSection _ s) prior) -> addComment s anch prior
|
EpaDocSection _ s -> addComment False s anch prior
|
||||||
L anch (EpaComment (EpaDocOptions s) prior) -> addComment s anch prior
|
EpaDocOptions s -> addComment False s anch prior
|
||||||
L anch (EpaComment (EpaLineComment s) prior) -> addComment s anch prior
|
EpaLineComment s -> addComment False s anch prior
|
||||||
L anch (EpaComment (EpaBlockComment s) prior) -> addComment s anch prior
|
EpaBlockComment s -> addComment True s anch prior
|
||||||
L _anch (EpaComment (EpaEofComment) _prior) -> pure ()
|
EpaEofComment -> pure ()
|
||||||
|
|
|
@ -82,7 +82,7 @@ processModule traceFunc conf inlineConf parsedModule = do
|
||||||
bdMay <- ppModuleHead modHead
|
bdMay <- ppModuleHead modHead
|
||||||
case bdMay of
|
case bdMay of
|
||||||
Nothing -> pure ()
|
Nothing -> pure ()
|
||||||
Just bd -> ppBriDoc bd
|
Just bd -> ppBriDoc bd True
|
||||||
MEPrettyModuleHead (L loc modHead) -> wrapNonDeclToBriDoc $ do
|
MEPrettyModuleHead (L loc modHead) -> wrapNonDeclToBriDoc $ do
|
||||||
case modHead of
|
case modHead of
|
||||||
HsModule epAnn _layoutInfo Nothing Nothing _ _ _ _ -> do
|
HsModule epAnn _layoutInfo Nothing Nothing _ _ _ _ -> do
|
||||||
|
@ -92,7 +92,7 @@ processModule traceFunc conf inlineConf parsedModule = do
|
||||||
docFlushRemaining
|
docFlushRemaining
|
||||||
(srcSpanFileName_maybe loc)
|
(srcSpanFileName_maybe loc)
|
||||||
$ docHandleComms epAnn docSeparator
|
$ docHandleComms epAnn docSeparator
|
||||||
ppBriDoc bd
|
ppBriDoc bd True
|
||||||
HsModule _ _layoutInfo Nothing Just{} _ _ _ _ -> error
|
HsModule _ _layoutInfo Nothing Just{} _ _ _ _ -> error
|
||||||
"brittany internal error: exports without module name"
|
"brittany internal error: exports without module name"
|
||||||
HsModule epAnn _layoutInfo (Just n) les _ _ _ _ -> do
|
HsModule epAnn _layoutInfo (Just n) les _ _ _ _ -> do
|
||||||
|
@ -102,7 +102,7 @@ processModule traceFunc conf inlineConf parsedModule = do
|
||||||
docFlushRemaining
|
docFlushRemaining
|
||||||
(srcSpanFileName_maybe loc)
|
(srcSpanFileName_maybe loc)
|
||||||
$ moduleNameExportBridoc epAnn n les
|
$ moduleNameExportBridoc epAnn n les
|
||||||
ppBriDoc bd
|
ppBriDoc bd True
|
||||||
MEImportDecl importDecl immediateAfterComms ->
|
MEImportDecl importDecl immediateAfterComms ->
|
||||||
wrapNonDeclToBriDoc $ do
|
wrapNonDeclToBriDoc $ do
|
||||||
(bd, _) <-
|
(bd, _) <-
|
||||||
|
@ -111,7 +111,7 @@ processModule traceFunc conf inlineConf parsedModule = do
|
||||||
( layoutImport importDecl
|
( layoutImport importDecl
|
||||||
: map commentToDoc immediateAfterComms
|
: map commentToDoc immediateAfterComms
|
||||||
)
|
)
|
||||||
ppBriDoc bd
|
ppBriDoc bd False
|
||||||
MEDecl decl immediateAfterComms -> do
|
MEDecl decl immediateAfterComms -> do
|
||||||
let declConfig = getDeclConfig conf inlineConf decl
|
let declConfig = getDeclConfig conf inlineConf decl
|
||||||
MultiRWSS.withMultiReader declConfig
|
MultiRWSS.withMultiReader declConfig
|
||||||
|
@ -225,7 +225,7 @@ ppToplevelDecl decl immediateAfterComms = do
|
||||||
(r, errorCount) <- briDocMToPPM
|
(r, errorCount) <- briDocMToPPM
|
||||||
$ docSeq (innerDoc : map commentToDoc immediateAfterComms)
|
$ docSeq (innerDoc : map commentToDoc immediateAfterComms)
|
||||||
if errorCount == 0 then pure (r, 0) else briDocMToPPM $ briDocByExact decl
|
if errorCount == 0 then pure (r, 0) else briDocMToPPM $ briDocByExact decl
|
||||||
ppBriDoc bd
|
ppBriDoc bd False
|
||||||
let commCntIn = connectedCommentCount decl
|
let commCntIn = connectedCommentCount decl
|
||||||
commCntOut <- mGet
|
commCntOut <- mGet
|
||||||
when (commCntIn /= commCntOut) $ if commCntOut < commCntIn
|
when (commCntIn /= commCntOut) $ if commCntOut < commCntIn
|
||||||
|
|
|
@ -39,7 +39,6 @@ moduleNameExportBridoc epAnn modName les = do
|
||||||
Nothing -> id
|
Nothing -> id
|
||||||
Just SameLine{} -> id
|
Just SameLine{} -> id
|
||||||
Just (DifferentLine 0 _) -> id
|
Just (DifferentLine 0 _) -> id
|
||||||
Just (DifferentLine 1 _) -> id
|
|
||||||
Just dp -> docAddEntryDelta dp
|
Just dp -> docAddEntryDelta dp
|
||||||
docHandleComms epAnn $ docHandleComms posModule $ runFilteredAlternative $ do
|
docHandleComms epAnn $ docHandleComms posModule $ runFilteredAlternative $ do
|
||||||
addAlternativeCond allowSingleLine $ docSeq
|
addAlternativeCond allowSingleLine $ docSeq
|
||||||
|
|
|
@ -16,6 +16,7 @@ module Language.Haskell.Brittany.Internal.WriteBriDoc.Operators
|
||||||
, layoutIndentLevelPop
|
, layoutIndentLevelPop
|
||||||
, layoutWriteNewline
|
, layoutWriteNewline
|
||||||
, layoutWriteComment
|
, layoutWriteComment
|
||||||
|
, layoutFlushLine
|
||||||
)
|
)
|
||||||
where
|
where
|
||||||
|
|
||||||
|
@ -111,11 +112,12 @@ layoutWriteNewline = do
|
||||||
layoutWriteComment
|
layoutWriteComment
|
||||||
:: (MonadMultiWriter Text.Builder.Builder m, MonadMultiState LayoutState m)
|
:: (MonadMultiWriter Text.Builder.Builder m, MonadMultiState LayoutState m)
|
||||||
=> Bool
|
=> Bool
|
||||||
|
-> Bool
|
||||||
-> GHC.DeltaPos
|
-> GHC.DeltaPos
|
||||||
-> Int
|
-> Int
|
||||||
-> Text
|
-> Text
|
||||||
-> m ()
|
-> m ()
|
||||||
layoutWriteComment absolute dp commentLines s = do -- TODO92 we don't move to comment pos at all!
|
layoutWriteComment absolute isBlock dp commentLines s = do -- TODO92 we don't move to comment pos at all!
|
||||||
let (y, x) = case dp of
|
let (y, x) = case dp of
|
||||||
GHC.SameLine c -> (0, c)
|
GHC.SameLine c -> (0, c)
|
||||||
GHC.DifferentLine l c -> (l, c)
|
GHC.DifferentLine l c -> (l, c)
|
||||||
|
@ -124,15 +126,17 @@ layoutWriteComment absolute dp commentLines s = do -- TODO92 we don't move to co
|
||||||
mTell $ Text.Builder.fromText s
|
mTell $ Text.Builder.fromText s
|
||||||
traceLocal ("layoutMoveToCommentPos", y, x, commentLines, _lstate_plannedSpace state, lstate_baseY state)
|
traceLocal ("layoutMoveToCommentPos", y, x, commentLines, _lstate_plannedSpace state, lstate_baseY state)
|
||||||
mSet state
|
mSet state
|
||||||
{ _lstate_plannedSpace = case _lstate_plannedSpace state of
|
{ _lstate_plannedSpace = if isBlock
|
||||||
PlannedNone -> PlannedDelta 1 (_lstate_curY state)
|
then PlannedSameline 0
|
||||||
PlannedSameline i -> PlannedDelta 1 (_lstate_curY state + i)
|
else case _lstate_plannedSpace state of
|
||||||
p@PlannedNewline{} -> p
|
PlannedNone -> PlannedDelta 1 (_lstate_curY state)
|
||||||
p@PlannedDelta{} -> p
|
PlannedSameline i -> PlannedDelta 1 (_lstate_curY state + i)
|
||||||
|
p@PlannedNewline{} -> p
|
||||||
|
p@PlannedDelta{} -> p
|
||||||
, _lstate_commentNewlines =
|
, _lstate_commentNewlines =
|
||||||
_lstate_commentNewlines state + y + commentLines - 1
|
_lstate_commentNewlines state + y + commentLines - 1
|
||||||
, _lstate_curY = if y == 0 then _lstate_curY state + x
|
, _lstate_curY = if y == 0 then _lstate_curY state + x + Text.length s
|
||||||
else x
|
else x + Text.length s
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -293,6 +297,13 @@ layoutAddSepSpace = do
|
||||||
-- (e.g. from an inserted comment restore-position task).
|
-- (e.g. from an inserted comment restore-position task).
|
||||||
}
|
}
|
||||||
|
|
||||||
|
layoutFlushLine
|
||||||
|
:: (MonadMultiWriter Text.Builder.Builder m, MonadMultiState LayoutState m)
|
||||||
|
=> m ()
|
||||||
|
layoutFlushLine = do
|
||||||
|
state <- mGet
|
||||||
|
when (_lstate_curY state > 0) layoutWriteEnsureNewlineBlock
|
||||||
|
|
||||||
-- moveToY :: MonadMultiState LayoutState m => Int -> m ()
|
-- moveToY :: MonadMultiState LayoutState m => Int -> m ()
|
||||||
-- moveToY y = mModify $ \state ->
|
-- moveToY y = mModify $ \state ->
|
||||||
-- let
|
-- let
|
||||||
|
|
Loading…
Reference in New Issue