packages feed

tilia-0.2.0.0: src/Tilia/Equivalence.hs

{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE OverloadedLabels #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE PatternSynonyms #-}

-- | Whether formatting changed what a module says.
module Tilia.Equivalence
  ( syntaxDifference,
    commentDifference,
  )
where

import Control.Applicative ((<|>))
import Data.ByteString (ByteString)
import Data.ByteString qualified as BS
import Data.Char (isSpace)
import Data.Choice (pattern Is)
import Data.Data
import Data.IORef (IORef, atomicModifyIORef', newIORef, readIORef)
import Data.IntMap.Strict qualified as IntMap
import Data.List (sortOn, unsnoc)
import Data.List.NonEmpty (NonEmpty (..))
import Data.List.NonEmpty qualified as NE
import Data.Map.Strict (Map)
import Data.Map.Strict qualified as Map
import Data.Maybe (catMaybes, isNothing, listToMaybe, mapMaybe)
import Data.Set (Set)
import Data.Set qualified as Set
import Data.Text (Text)
import Data.Text qualified as T
import Data.Text.Encoding qualified as T
import GHC.Data.FastString (FastString)
import GHC.Hs (HsModule (..))
import GHC.Hs.Decls (DerivClauseTys (..), DocDecl (..), HsDecl (..), LHsDecl)
import GHC.Hs.Doc (LHsDoc, WithHsDocIdentifiers (..))
import GHC.Hs.DocString
  ( HsDocString (..),
    HsDocStringChunk (..),
    HsDocStringDecorator (..),
  )
import GHC.Hs.Expr (HsExpr (..), LHsExpr)
import GHC.Hs.Extension (GhcPs)
import GHC.Hs.ImpExp
  ( IE (..),
    ImportDeclQualifiedStyle,
    LIE,
    LImportDecl,
    isImportDeclQualified,
  )
import GHC.Hs.Type (HsType (..), LHsContext, LHsSigType)
import GHC.Types.Name (Name)
import GHC.Types.Name.Occurrence (OccName)
import GHC.Types.SrcLoc (unLoc)
import GHC.Unit.Types (Unit)
import Language.Haskell.Syntax.Extension (XRec)
import Language.Haskell.Syntax.Module.Name (ModuleName)
import System.IO.Unsafe (unsafeDupablePerformIO, unsafePerformIO)

import Tilia.Comments
  ( Comment (..),
    CommentStyle (..),
    asOrdinary,
    bracketed,
    carriedOnFrom,
    carriesOn,
    commentPragma,
    commentTrailing,
    splitTrigger,
    triggerEscaped,
  )
import Tilia.Imports (normalizeImports, travellingComments)
import Tilia.Parser (ParsedModule (..), moduleHeaderSpan, writtenInHeader)
import Tilia.Pragma (Pragma (..), languageNames, pragmaFlags)
import Tilia.Pragma.Ghc (InForce (..), inForceAfter)
import Tilia.Source (comments, sourceLines)
import Tilia.Span (Span (..), covers, startPoint)
import Tilia.Span.Ghc (spanOf, spansOf)
import Tilia.Utils (tshow)

----------------------------------------------------------------------------
-- Syntax

-- | Where two ASTs start to differ.
syntaxDifference :: Data a => a -> a -> Maybe Text
syntaxDifference = differ []

-- | The constructors on the way down to where the walk has got to,
-- innermost first.
type Path = [Constr]

differ :: forall a. Data a => Path -> a -> a -> Maybe Text
differ path x y = case classify (typeOf x) of
  Incidental -> Nothing
  Special
    | Just outcome <- asStandaloneDoc path x y -> outcome
    | Just outcome <- asExportItems path x y -> outcome
    | Just outcome <- asDeclarations path x y -> outcome
    | Just outcome <- asDerivingClause path x y -> outcome
    | Just outcome <- asQualifiedStyle path x y -> outcome
    | Just outcome <- asDocString path x y -> outcome
    | Just outcome <- asContext path x y -> outcome
    | Just outcome <- asImports path x y -> outcome
    | otherwise -> structurally
  Ordinary -> structurally
  where
    structurally = case dataTypeRep (dataTypeOf x) of
      AlgRep _
        | toConstr x /= toConstr y ->
            maybe
              (Just disagreement)
              (uncurry (differ path))
              (withoutSilentDocs x y)
        | settledByConstructor (toConstr x) -> Nothing
        | otherwise ->
            firstOf
              ( zipWith
                  (cellDiffer (toConstr x : path))
                  (gmapQ Cell x)
                  (gmapQ Cell y)
              )
      NoRep
        | opaque x y -> Nothing
        | otherwise ->
            Just (describe path (T.pack (typeNameOf x) <> " changed"))
      _
        | toConstr x == toConstr y -> Nothing
        | otherwise -> Just disagreement
    disagreement =
      describe path (named (toConstr x) <> " became " <> named (toConstr y))

-- | What is known about a type before either value of it is looked at.
data Verdict
  = -- | Records only how or where something was written. See 'incidental'.
    Incidental
  | -- | One of the types the @as…@ functions below compare by hand.
    Special
  | -- | Compared by its constructor and then field by field.
    Ordinary

-- | Which of the three a type is, worked out once.
classify :: TypeRep -> Verdict
classify rep = unsafeDupablePerformIO $ do
  known <- readIORef classified
  case Map.lookup rep known of
    Just verdict -> pure verdict
    Nothing -> do
      let verdict = worked
      atomicModifyIORef' classified (\m -> (Map.insert rep verdict m, ()))
      pure verdict
  where
    worked
      | incidental rep = Incidental
      | rep `Set.member` comparedByHand = Special
      | otherwise = Ordinary

classified :: IORef (Map TypeRep Verdict)
classified = unsafePerformIO (newIORef Map.empty)
{-# NOINLINE classified #-}

-- | The types compared by hand, which is to say the ones the @as…@ chain in
-- 'differ' can match.
comparedByHand :: Set TypeRep
comparedByHand =
  Set.fromList
    [ typeRep (Proxy @(Maybe (LHsDoc GhcPs))),
      typeRep (Proxy @[LIE GhcPs]),
      typeRep (Proxy @[LHsDecl GhcPs]),
      typeRep (Proxy @(DerivClauseTys GhcPs)),
      typeRep (Proxy @ImportDeclQualifiedStyle),
      typeRep (Proxy @HsDocString),
      typeRep (Proxy @(Maybe (LHsContext GhcPs))),
      typeRep (Proxy @(LHsContext GhcPs)),
      typeRep (Proxy @(XRec GhcPs [LHsExpr GhcPs])),
      typeRep (Proxy @[LImportDecl GhcPs])
    ]

-- | Constructors whose fields say only how they were written.
--
-- @HsStarTy@ carries a flag for whether the @*@ was typed as @★@. Both are
-- the same kind; which one the author reached for is spelling.
settledByConstructor :: Constr -> Bool
settledByConstructor c = showConstr c == "HsStarTy"

named :: Constr -> Text
named = T.pack . showConstr

-- | The tail of the path, and what was found at the end of it.
describe :: Path -> Text -> Text
describe path leaf =
  T.intercalate " > " (fmap named (reverse (take 5 path)) <> [leaf])

firstOf :: [Maybe a] -> Maybe a
firstOf = listToMaybe . catMaybes

-- | One field of a value, with its type hidden.
data Cell = forall d. Data d => Cell d

cellDiffer :: Path -> Cell -> Cell -> Maybe Text
cellDiffer path (Cell a) (Cell b) = case cast b of
  Just b' -> differ path a b'
  Nothing -> Just (describe path "fields of different types")

-- | Two lists compared one element at a time.
--
-- Handed back to 'differ' whole they would arrive at the same @as…@ function
-- again and never stop, which is why the lengths are settled here and only
-- the elements go back round.
elementwise :: Data b => Path -> Text -> [b] -> [b] -> Maybe Text
elementwise path what before after
  | length before /= length after = Just (describe path what)
  | otherwise = firstOf (zipWith (differ path) before after)

-- | Does this documentation comment say anything?
saysNothing :: HsDocString -> Bool
saysNothing = null . docWords

-- | A documentation comment, taken as absent where it says nothing.
--
-- 'Nothing' where the two are not a @Maybe (LHsDoc GhcPs)@, which sends
-- 'differ' on to the next @as…@ function. Otherwise the answer, itself
-- 'Nothing' where the two say the same thing.
asStandaloneDoc :: Data a => Path -> a -> a -> Maybe (Maybe Text)
asStandaloneDoc path x y = case (cast x, cast y) of
  (Just before, Just after) -> Just (compared (kept before) (kept after))
  _ -> Nothing
  where
    kept :: Maybe (LHsDoc GhcPs) -> Maybe (LHsDoc GhcPs)
    kept d = if any (saysNothing . hsDocString . unLoc) d then Nothing else d
    compared before after = case (before, after) of
      (Nothing, Nothing) -> Nothing
      (Just b, Just a) -> differ (toConstr before : path) b a
      _ ->
        Just
          ( describe
              path
              (named (toConstr before) <> " became " <> named (toConstr after))
          )

-- | Two types without the Haddock that says nothing around either, where
-- one of them has one.
--
-- Asked only where the two differ in their constructors, which is the one
-- way such a Haddock shows.
withoutSilentDocs ::
  Data a =>
  -- | What was written.
  a ->
  -- | What was printed.
  a ->
  Maybe (HsType GhcPs, HsType GhcPs)
withoutSilentDocs x y = case (cast x, cast y) of
  (Just before, Just after)
    | silent before || silent after -> Just (bare before, bare after)
  _ -> Nothing
  where
    silent :: HsType GhcPs -> Bool
    silent = \case
      HsDocTy _ _ doc -> saysNothing (hsDocString (unLoc doc))
      _ -> False
    bare t = case t of
      HsDocTy _ inner _ | silent t -> bare (unLoc inner)
      _ -> t

-- | An export list, minus the documentation that says nothing.
asExportItems :: Data a => Path -> a -> a -> Maybe (Maybe Text)
asExportItems path x y = case (cast x, cast y) of
  (Just before, Just after) ->
    Just
      ( elementwise
          path
          "the module exports a different list"
          (kept before)
          (kept after)
      )
  _ -> Nothing
  where
    kept :: [LIE GhcPs] -> [LIE GhcPs]
    kept = filter (not . emptyExport . unLoc)
    emptyExport = \case
      IEDoc _ doc -> saysNothing (hsDocString (unLoc doc))
      _ -> False

-- | A block of declarations, minus the documentation that says nothing.
asDeclarations :: Data a => Path -> a -> a -> Maybe (Maybe Text)
asDeclarations path x y = case (cast x, cast y) of
  (Just before, Just after) ->
    Just
      ( elementwise
          path
          "a different number of declarations"
          (kept before)
          (kept after)
      )
  _ -> Nothing
  where
    kept :: [LHsDecl GhcPs] -> [LHsDecl GhcPs]
    kept = filter (not . emptyDecl . unLoc)
    emptyDecl = \case
      DocD _ d -> case d of
        DocCommentNext doc -> saysNothing (hsDocString (unLoc doc))
        DocCommentPrev doc -> saysNothing (hsDocString (unLoc doc))
        _ -> False
      _ -> False

-- | Does this type record only how or where something was written?
incidental :: TypeRep -> Bool
incidental rep = case splitTyConApp rep of
  (con, args)
    | qualified con == "GHC.Types.SrcLoc.GenLocated" -> False
    | notation con -> True
    | structural con -> not (null args) && all incidental args
    | otherwise -> False
  where
    qualified con = tyConModule con <> "." <> tyConName con

-- | Types that exist to record punctuation, position or spelling.
notation :: TyCon -> Bool
notation con =
  tyConModule con == "GHC.Parser.Annotation"
    || tyConModule con == "GHC.Types.SrcLoc"
    || ("GHC.Hs." `isPrefix` tyConModule con && "Ann" `isPrefix` tyConName con)
    || qualified `Set.member` alsoNotation
  where
    qualified = tyConModule con <> "." <> tyConName con
    isPrefix p t = take (length p) t == p

-- | The stragglers, named in full.
alsoNotation :: Set String
alsoNotation =
  Set.fromList
    [ -- Where a layout block's column was, which is the whole of what
      -- reformatting changes.
      "GHC.Hs.Extension.EpLayout",
      "Language.Haskell.Syntax.Extension.EpLayout",
      -- The text GHC keeps beside a literal or a pragma so that it can
      -- reproduce what was typed: the spaces inside @{-# INLINE   f #-}@,
      -- whether an integer was written in hex, how a multi-line string was
      -- indented. The value itself is in the next field along.
      "GHC.Types.SourceText.SourceText",
      -- Whether a linear arrow was written @%1 ->@ or @⊸@. The multiplicity
      -- it stands for is a different field, and is compared.
      "GHC.Hs.Type.EpLinear"
    ]

-- | Containers that are transparent to the question.
structural :: TyCon -> Bool
structural con =
  tyConName con
    `elem` ["Maybe", "List", "NonEmpty", "Tuple2", "Tuple3", "Tuple4", "Tuple5"]

-- | Which side of the module name @qualified@ was written on.
asQualifiedStyle :: Data a => Path -> a -> a -> Maybe (Maybe Text)
asQualifiedStyle path x y = case (cast x, cast y) of
  (Just before, Just after) -> Just (compared before after)
  _ -> Nothing
  where
    compared ::
      ImportDeclQualifiedStyle -> ImportDeclQualifiedStyle -> Maybe Text
    compared before after
      | isImportDeclQualified before == isImportDeclQualified after = Nothing
      | otherwise = Just (describe path "the import stopped being qualified")

-- | A @deriving@ clause, however it was punctuated.
asDerivingClause :: Data a => Path -> a -> a -> Maybe (Maybe Text)
asDerivingClause path x y = case (cast x, cast y) of
  (Just before, Just after) ->
    Just (differ path (derived before) (derived after))
  _ -> Nothing
  where
    derived :: DerivClauseTys GhcPs -> [LHsSigType GhcPs]
    derived = \case
      DctSingle _ t -> [t]
      DctMulti _ ts -> ts

-- | A module's imports, compared as the set they are, or 'Nothing' where
-- the two are not imports.
asImports ::
  Data a =>
  -- | The constructors above the two, which a difference is described by.
  Path ->
  -- | One of them.
  a ->
  -- | The other.
  a ->
  Maybe (Maybe Text)
asImports path x y = case (cast x, cast y) of
  (Just before, Just after) ->
    Just (alongside (normalised before) (normalised after))
  _ -> Nothing
  where
    normalised :: [LImportDecl GhcPs] -> [LImportDecl GhcPs]
    normalised =
      fmap fst
        . concat
        . normalizeImports (Is #implicitPrelude) Map.empty [] [] []
    alongside before after
      | length before /= length after =
          Just (describe path "the module imports a different set of modules")
      | otherwise = firstOf (zipWith (differ path) before after)

-- | A context, compared for the constraints it holds.
asContext :: Data a => Path -> a -> a -> Maybe (Maybe Text)
asContext path x y = compared optional <|> compared written <|> compared quoted
  where
    compared ::
      forall b c. (Typeable b, Data c) => (b -> c) -> Maybe (Maybe Text)
    compared strip = case (cast x, cast y) of
      (Just before, Just after) ->
        Just (differ path (strip before) (strip after))
      _ -> Nothing
    optional :: Maybe (LHsContext GhcPs) -> [HsType GhcPs]
    optional = maybe [] written
    written :: LHsContext GhcPs -> [HsType GhcPs]
    written = fmap (unbracket . unLoc) . unLoc
    quoted :: XRec GhcPs [LHsExpr GhcPs] -> [HsExpr GhcPs]
    quoted = fmap (unparenthesised . unLoc) . unLoc
    unbracket = \case
      HsParTy _ t -> unbracket (unLoc t)
      t -> t
    unparenthesised = \case
      HsPar _ e -> unparenthesised (unLoc e)
      e -> e

-- | A Haddock, compared for what it documents.
asDocString :: Data a => Path -> a -> a -> Maybe (Maybe Text)
asDocString path x y = case (cast x, cast y) of
  (Just before, Just after)
    | summarised before == summarised after -> Just Nothing
    | otherwise ->
        Just (Just (describe path "a documentation comment changed"))
  _ -> Nothing
  where
    summarised :: HsDocString -> (Text, [ByteString])
    summarised d = (kindOf d, docWords d)
    kindOf = \case
      MultiLineDocString dec _ -> decorator dec
      NestedDocString dec _ -> decorator dec
      GeneratedDocString _ -> "generated"
    decorator = \case
      HsDocStringNext -> "pointer"
      HsDocStringPrevious -> "pointer"
      HsDocStringNamed n -> "named " <> T.pack n
      HsDocStringGroup n -> "group " <> tshow n

-- | What a doc string says, with the whitespace thrown away: what
-- 'Data.Char.isSpace' takes for it, as Haddock and the printer do.
docWords :: HsDocString -> [ByteString]
docWords =
  concatMap chunkWords . \case
    MultiLineDocString _ cs -> fmap unLoc (NE.toList cs)
    NestedDocString _ c -> [unLoc c]
    GeneratedDocString c -> [c]
  where
    chunkWords (HsDocStringChunk bytes)
      | BS.all (< 0x80) bytes =
          filter (not . BS.null) (BS.splitWith isAsciiSpace bytes)
      | otherwise = fmap T.encodeUtf8 (T.words (T.decodeUtf8 bytes))
    isAsciiSpace w = w == 32 || (9 <= w && w <= 13)

-- | Compare two values of a type that will not be taken apart.
opaque :: forall a b. (Data a, Data b) => a -> b -> Bool
opaque x y = case dataTypeName (dataTypeOf x) of
  "FastString" -> by @FastString
  "OccName" -> by @OccName
  "ModuleName" -> by @ModuleName
  "Name" -> by @Name
  "Unit" -> by @Unit
  "Data.ByteString.ByteString" -> by @ByteString
  name ->
    error $
      "Tilia.Equivalence: "
        <> name
        <> " does not expose its structure and is not one of the types this\
           \ knows how to compare. Decide whether it carries meaning and add\
           \ it to `opaque`, or to `alsoNotation` if it records only\
           \ punctuation, position or spelling."
  where
    by :: forall t. (Typeable t, Eq t) => Bool
    by = case (cast x, cast y) of
      (Just p, Just q) -> p == (q :: t)
      _ -> False

typeNameOf :: Data a => a -> String
typeNameOf = dataTypeName . dataTypeOf

----------------------------------------------------------------------------
-- Comments

-- | Did every comment survive, and if not, which one and how?
--
-- Ordinary comments are compared as they will be printed, in order: the
-- text is normalised on the way in, so a comment that came out unchanged
-- reads back identically, and one that was mangled or moved past its
-- neighbor does not, unless it trails a line and moved past comments on
-- lines of their own written below it.
--
-- Except in the header, where the order says nothing. The pragmas are
-- sorted and so are the imports, and a comment written against either
-- travels with it, so the two streams are put in a settled order there
-- before being compared, with the pragmas of the header that hold a
-- comment. What is still asked of that stretch is that the same comments
-- come out of it, and those inside the export list, which nothing sorts,
-- in order.
--
-- Documentation comments are counted rather than compared. The printer may
-- legitimately rewrite one—a @-- ^ x@ that moves in front of what it
-- documents has to become @-- | x@—so the text is not expected to survive,
-- but the comment is.
--
-- The pragmas of the header are also read as GHC reads them: the flags of
-- the options have to come out in the same order, and what GHC's own flag
-- reader puts in force after all of them has to stay the same.
commentDifference ::
  -- | The module as written.
  ParsedModule ->
  -- | The module as printed.
  ParsedModule ->
  Maybe Text
commentDifference parsedBefore parsedAfter
  | not (Set.null lost) = Just ("lost the pragma " <> pragmaList lost)
  | not (Set.null gained) = Just ("invented the pragma " <> pragmaList gained)
  | optionsBefore /= optionsAfter =
      Just
        ( "changed the flags of the options pragmas from "
            <> flagList optionsBefore
            <> " to "
            <> flagList optionsAfter
        )
  | haddockBefore /= haddockAfter =
      Just
        ( "changed the OPTIONS_HADDOCK pragmas from "
            <> flagList haddockBefore
            <> " to "
            <> flagList haddockAfter
        )
  | flagsBefore /= flagsAfter,
    Just why <-
      inForceDifference (inForceAfter flagsBefore) (inForceAfter flagsAfter) =
      Just why
  | docsBefore /= docsAfter =
      Just $
        "the module's "
          <> tshow docsBefore
          <> " documentation comments became "
          <> tshow docsAfter
  | otherwise =
      diverge belowHeaderBefore belowHeaderAfter
        <|> diverge (settled headerCommentsBefore) (settled headerCommentsAfter)
        <|> diverge
          (exported moduleBefore headerCommentsBefore)
          (exported moduleAfter headerCommentsAfter)
  where
    moduleBefore = pmModule parsedBefore
    moduleAfter = pmModule parsedAfter
    before = commentsAsRead parsedBefore
    after = commentsAsRead parsedAfter
    (headerCommentsBefore, belowHeaderBefore) = byHeader parsedBefore before
    (headerCommentsAfter, belowHeaderAfter) = byHeader parsedAfter after

    byHeader m cs =
      ( filter inside common
          <> filter commented (takeWhile (pragmaWritten m . commentSpan) cs),
        filter (not . inside) common
      )
      where
        common = ordinary cs
        inside = inHeader m cs
    pragmaWritten = writtenInHeader . pmHeaderEnd
    exported m =
      filter (\c -> any (`covers` commentSpan c) (spanOf =<< hsmodExports m))
    settled = sortOn bodyKey

    inHeader m cs = case lastRearrangedLine m cs of
      Nothing -> const False
      Just lastLine -> \c ->
        spanStartLine (commentSpan c) <= lastLine
          && all
            ((startPoint (commentSpan c) <) . startPoint)
            (spanOf =<< listToMaybe (hsmodDecls (pmModule m)))

    lost = pragmasOf before `Set.difference` pragmasOf after
    headerBefore = headerPragmas parsedBefore
    headerAfter = headerPragmas parsedAfter
    flagsBefore = concatMap flagsOf headerBefore
    flagsAfter = concatMap flagsOf headerAfter
    flagsOf p = pragmaFlags (pragmaName p) (pragmaBody p)
    optionsBefore = filter (not . T.isPrefixOf "-X") flagsBefore
    optionsAfter = filter (not . T.isPrefixOf "-X") flagsAfter
    haddockBefore =
      [pragmaBody p | p <- headerBefore, pragmaName p == "OPTIONS_HADDOCK"]
    haddockAfter =
      [pragmaBody p | p <- headerAfter, pragmaName p == "OPTIONS_HADDOCK"]
    flagList flags = "`" <> T.unwords flags <> "`"
    gained = pragmasOf after `Set.difference` pragmasOf before
    docsBefore = length (documentation before)
    docsAfter = length (documentation after)

    documentation = filter isDocumentation
    isDocumentation c = commentStyle c == DocComment || triggerEscaped c

    ordinary =
      filter (\c -> not (isDocumentation c) && isNothing (commentPragma c))
    commented = maybe False pragmaCommented . commentPragma

    diverge [] [] = Nothing
    diverge (b : _) [] = Just ("lost " <> quoted b)
    diverge [] (a : _) = Just ("gained " <> quoted a)
    diverge (b : bs) (a : as)
      | bodyKey b == bodyKey a = diverge bs as
      | Just (bs', as') <- crossed (b : bs) (a : as) = diverge bs' as'
      | otherwise = Just (quoted b <> " became " <> quoted a)

    crossed written printed =
      listToMaybe
        [ (drop (j + k) written, drop (j + k) printed)
        | j <- [1 .. length (takeWhile commentTrailing written)],
          let lifted = drop j written,
          let shared = length (takeWhile id (zipWith alike printed lifted)),
          k <- [shared, shared - 1 .. 1],
          all (not . commentTrailing) (take k lifted),
          fmap bodyKey (take j (drop k printed))
            == fmap bodyKey (take j written)
        ]

    alike x y = bodyKey x == bodyKey y

    quoted c = "`" <> T.intercalate "\\n" (NE.toList (commentBody c)) <> "`"
    pragmaList =
      T.intercalate ", "
        . fmap (\(n, b) -> "{-# " <> n <> " " <> b <> " #-}")
        . Set.toList

-- | A module's comments as they are compared, read as the printer reads
-- them: a Haddock in the tree whole, wherever it moves, and every other
-- comment as 'asOrdinary' reads it.
--
-- A Haddock written as @--@ lines that hold only spaces is printed as the
-- comments the next pass reads, so it is read as them as well.
commentsAsRead :: ParsedModule -> [Comment]
commentsAsRead m = concatMap readIn (comments (pmSource m))
  where
    readIn c
      | commentStyle c == DocComment,
        Set.notMember (startPoint (commentSpan c)) (pmLooseHaddocks m),
        not (onlySpaces c) =
          [c]
      | otherwise = asOrdinary (sourceLines (pmSource m)) c
    onlySpaces c = case commentBody c of
      first :| rest ->
        not (bracketed c)
          && maybe False (T.all isSpace . snd) (splitTrigger first)
          && all (T.all isSpace . T.drop 2 . T.stripStart) rest

-- | The pragmas of a module's header, which are all the pragmas GHC reads,
-- in the order they are written.
headerPragmas :: ParsedModule -> [Pragma]
headerPragmas m =
  mapMaybe commentPragma (takeWhile written (comments (pmSource m)))
  where
    written = writtenInHeader (pmHeaderEnd m) . commentSpan

-- | How what the flags of a module's header settle changed, if it did.
inForceDifference :: InForce -> InForce -> Maybe Text
inForceDifference before after
  | inForceEdition before /= inForceEdition after =
      Just
        ( "changed the edition in force from "
            <> edition before
            <> " to "
            <> edition after
        )
  | inForceSafety before /= inForceSafety after =
      Just
        ( "changed the Safe Haskell mode from "
            <> tshow (inForceSafety before)
            <> " to "
            <> tshow (inForceSafety after)
        )
  | null switched = Nothing
  | otherwise =
      Just
        ( "changed how the header switches "
            <> T.intercalate ", " (fmap tshow switched)
        )
  where
    switched =
      [ e
      | e <- Map.keys (inForceSwitches before <> inForceSwitches after),
        Map.lookup e (inForceSwitches before)
          /= Map.lookup e (inForceSwitches after)
      ]
    edition = maybe "the default" tshow . inForceEdition

-- | A comment's lines, as they are compared.
bodyKey :: Comment -> NonEmpty Text
bodyKey c = case commentBody c of
  (l :| []) -> case T.stripPrefix "--" stripped of
    Just rest -> "--" <> T.stripStart rest :| []
    Nothing -> stripped :| []
    where
      stripped = T.stripStart l
  ls -> ls

-- | The last line the formatter may reorder: the end of the last import, or
-- of the module header where there are no imports, and of the comments
-- under it that travel with it, either carrying on a remark at its end or
-- written right under the last import with an empty line or nothing under
-- them.
--
-- 'Nothing' where the module has neither header nor imports.
lastRearrangedLine ::
  -- | The module, whose comments as lexed tell which travel with its last
  -- import.
  ParsedModule ->
  -- | Its comments as they are compared.
  [Comment] ->
  Maybe Int
lastRearrangedLine parsed cs =
  rearranged . spanEndLine
    <$> (spansOf (hsmodImports m) <> moduleHeaderSpan m)
  where
    m = pmModule parsed
    rearranged lastLine =
      maximum (carriedPast lastLine : fmap (spanEndLine . commentSpan) under)
    under = foldMap NE.toList $ do
      (_, final) <- unsnoc (hsmodImports m)
      s <- spanOf final
      IntMap.lookup (spanStartLine s) travelling
    travelling =
      travellingComments
        (sourceLines (pmSource parsed))
        (comments (pmSource parsed))
        m
    carriedPast lastLine =
      maybe lastLine (spanEndLine . commentSpan . snd) . unsnoc $
        takeWhile (\c -> any (carries c) (carriedOn c)) run
      where
        remarks = [c | c <- cs, spanEndLine (commentSpan c) == lastLine]
        run = following (lastLine + 1) cs
        carriedOn = carriedOnFrom (remarks <> run)
        carries c (line, remark) =
          line <= lastLine && carriesOn remark (commentSpan c) next
    next = spanOf =<< listToMaybe (hsmodDecls m)
    following n = \case
      c : more
        | spanStartLine (commentSpan c) < n -> following n more
        | spanStartLine (commentSpan c) == n ->
            c : following (spanEndLine (commentSpan c) + 1) more
      _ -> []

-- | The pragmas a module carries, however they were written.
--
-- A @{-# LANGUAGE A, B #-}@ counts as two, because that is what the
-- formatter turns it into, and the name is upper-cased and the body trimmed
-- so that two spellings of the same pragma are the same pragma.
pragmasOf :: [Comment] -> Set (Text, Text)
pragmasOf = Set.fromList . concatMap entries . mapMaybe commentPragma
  where
    entries p
      | pragmaName p == "LANGUAGE" =
          [("LANGUAGE", extension) | extension <- languageNames (pragmaBody p)]
      | otherwise = [(pragmaName p, pragmaBody p)]