|
| 1 | +-------------------------------------------------------------------------------- |
| 2 | +-- | Utilities for assocgating comments with things in a list. |
| 3 | +{-# LANGUAGE RecordWildCards #-} |
| 4 | +{-# LANGUAGE ScopedTypeVariables #-} |
| 5 | +module Language.Haskell.Stylish.Comments |
| 6 | + ( CommentGroup (..) |
| 7 | + , commentGroups |
| 8 | + , commentGroupHasComments |
| 9 | + , commentGroupSort |
| 10 | + ) where |
| 11 | + |
| 12 | + |
| 13 | +-------------------------------------------------------------------------------- |
| 14 | +import Data.Function (on) |
| 15 | +import Data.List (sortBy, sortOn) |
| 16 | +import Data.Maybe (isNothing, maybeToList) |
| 17 | +import qualified GHC.Hs as GHC |
| 18 | +import qualified GHC.Types.SrcLoc as GHC |
| 19 | +import qualified GHC.Utils.Outputable as GHC |
| 20 | + |
| 21 | + |
| 22 | +-------------------------------------------------------------------------------- |
| 23 | +import Language.Haskell.Stylish.Block |
| 24 | +import Language.Haskell.Stylish.GHC |
| 25 | + |
| 26 | + |
| 27 | +-------------------------------------------------------------------------------- |
| 28 | +data CommentGroup a = CommentGroup |
| 29 | + { cgBlock :: LineBlock |
| 30 | + , cgPrior :: [GHC.LEpaComment] |
| 31 | + , cgItems :: [(a, Maybe GHC.LEpaComment)] |
| 32 | + , cgFollowing :: [GHC.LEpaComment] |
| 33 | + } |
| 34 | + |
| 35 | + |
| 36 | +-------------------------------------------------------------------------------- |
| 37 | +instance GHC.Outputable a => Show (CommentGroup a) where |
| 38 | + show CommentGroup {..} = "(CommentGroup (" ++ |
| 39 | + show cgBlock ++ ") (" ++ |
| 40 | + showOutputable cgPrior ++ ") (" ++ |
| 41 | + showOutputable cgItems ++ ") (" ++ |
| 42 | + showOutputable cgFollowing ++ "))" |
| 43 | + |
| 44 | + |
| 45 | +-------------------------------------------------------------------------------- |
| 46 | +commentGroups |
| 47 | + :: forall a. |
| 48 | + (a -> Maybe GHC.RealSrcSpan) |
| 49 | + -> [a] |
| 50 | + -> [GHC.LEpaComment] |
| 51 | + -> [CommentGroup a] |
| 52 | +commentGroups getSpan allItems allComments = |
| 53 | + work Nothing (sortOn fst allItemsWithLines) (sortOn fst commentsWithLines) |
| 54 | + where |
| 55 | + allItemsWithLines :: [(LineBlock, a)] |
| 56 | + allItemsWithLines = do |
| 57 | + item <- allItems |
| 58 | + s <- maybeToList $ getSpan item |
| 59 | + pure (realSrcSpanToLineBlock s, item) |
| 60 | + |
| 61 | + commentsWithLines :: [(LineBlock, GHC.LEpaComment)] |
| 62 | + commentsWithLines = do |
| 63 | + comment <- allComments |
| 64 | + let s = GHC.anchor $ GHC.getLoc comment |
| 65 | + pure (realSrcSpanToLineBlock s, comment) |
| 66 | + |
| 67 | + work |
| 68 | + :: Maybe (CommentGroup a) |
| 69 | + -> [(LineBlock, a)] |
| 70 | + -> [(LineBlock, GHC.LEpaComment)] |
| 71 | + -> [CommentGroup a] |
| 72 | + work mbCurrent items comments = case takeNext items comments of |
| 73 | + Nothing -> maybeToList mbCurrent |
| 74 | + Just (b, next, items', comments') -> |
| 75 | + let (flush, current) = case mbCurrent of |
| 76 | + Just c | adjacent (cgBlock c) b |
| 77 | + , nextThingItem next |
| 78 | + , following@(_ : _) <- cgFollowing c -> |
| 79 | + ([c {cgFollowing = []}], CommentGroup b following [] []) |
| 80 | + Just c | adjacent (cgBlock c) b -> |
| 81 | + ([], c {cgBlock = cgBlock c <> b}) |
| 82 | + _ -> (maybeToList mbCurrent, CommentGroup b [] [] []) |
| 83 | + current' = case next of |
| 84 | + NextItem i -> current {cgItems = cgItems current <> [(i, Nothing)]} |
| 85 | + NextComment c |
| 86 | + | null (cgItems current) -> current {cgPrior = cgPrior current <> [c]} |
| 87 | + | otherwise -> current {cgFollowing = cgFollowing current <> [c]} |
| 88 | + NextItemWithComment i c -> |
| 89 | + current {cgItems = cgItems current <> [(i, Just c)]} in |
| 90 | + flush ++ work (Just current') items' comments' |
| 91 | + |
| 92 | + |
| 93 | + |
| 94 | +-------------------------------------------------------------------------------- |
| 95 | +takeNext |
| 96 | + :: [(LineBlock, a)] |
| 97 | + -> [(LineBlock, GHC.LEpaComment)] |
| 98 | + -> Maybe (LineBlock, NextThing a, [(LineBlock, a)], [(LineBlock, GHC.LEpaComment)]) |
| 99 | +takeNext [] [] = Nothing |
| 100 | +takeNext [] ((cb, c) : comments) = |
| 101 | + Just (cb, NextComment c, [], comments) |
| 102 | +takeNext ((ib, i) : items) [] = |
| 103 | + Just (ib, NextItem i, items, []) |
| 104 | +takeNext ((ib, i) : items) ((cb, c) : comments) |
| 105 | + | blockStart ib == blockStart cb = |
| 106 | + Just (ib <> cb, NextItemWithComment i c, items, comments) |
| 107 | + | blockStart ib < blockStart cb = |
| 108 | + Just (ib, NextItem i, items, (cb, c) : comments) |
| 109 | + | otherwise = |
| 110 | + Just (cb, NextComment c, (ib, i) : items, comments) |
| 111 | + |
| 112 | + |
| 113 | +-------------------------------------------------------------------------------- |
| 114 | +data NextThing a |
| 115 | + = NextComment GHC.LEpaComment |
| 116 | + | NextItem a |
| 117 | + | NextItemWithComment a GHC.LEpaComment |
| 118 | + |
| 119 | + |
| 120 | +-------------------------------------------------------------------------------- |
| 121 | +instance GHC.Outputable a => Show (NextThing a) where |
| 122 | + show (NextComment c) = "NextComment " ++ showOutputable c |
| 123 | + show (NextItem i) = "NextItem " ++ showOutputable i |
| 124 | + show (NextItemWithComment i c) = |
| 125 | + "NextItemWithComment " ++ showOutputable i ++ " " ++ showOutputable c |
| 126 | + |
| 127 | + |
| 128 | +-------------------------------------------------------------------------------- |
| 129 | +nextThingItem :: NextThing a -> Bool |
| 130 | +nextThingItem (NextComment _) = False |
| 131 | +nextThingItem (NextItem _) = True |
| 132 | +nextThingItem (NextItemWithComment _ _) = True |
| 133 | + |
| 134 | + |
| 135 | +-------------------------------------------------------------------------------- |
| 136 | +commentGroupHasComments :: CommentGroup a -> Bool |
| 137 | +commentGroupHasComments CommentGroup {..} = not $ |
| 138 | + null cgPrior && all (isNothing . snd) cgItems && null cgFollowing |
| 139 | + |
| 140 | + |
| 141 | +-------------------------------------------------------------------------------- |
| 142 | +commentGroupSort :: (a -> a -> Ordering) -> CommentGroup a -> CommentGroup a |
| 143 | +commentGroupSort cmp cg = cg |
| 144 | + { cgItems = sortBy (cmp `on` fst) (cgItems cg) |
| 145 | + } |
0 commit comments