packages feed

tilia-0.2.0.0: src/Tilia/Format.hs

{-# LANGUAGE DataKinds #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}

-- | Formatting a file, with everything the project can tell us about it.
module Tilia.Format
  ( FormatError (..),
    describeFormatError,
    formatErrorExitCode,
    refused,
    formattingFailed,
    Session (..),
    AstCheck (..),
    PlanSource (..),
    planSourceFor,
    newSession,
    Compiled (..),
    compiledSession,
    fixityNotesOf,
    formatSource,
    rewritten,
  )
where

import Control.Applicative ((<|>))
import Control.Monad (when)
import Control.Monad.IO.Class (liftIO)
import Control.Monad.Trans.Except (ExceptT, runExceptT, throwE)
import Data.Choice (Choice, fromBool, isTrue)
import Data.Foldable (traverse_)
import Data.IORef (IORef, atomicModifyIORef', newIORef, readIORef)
import Data.Map.Strict (Map)
import Data.Map.Strict qualified as Map
import Data.Maybe (mapMaybe)
import Data.Set (Set)
import Data.Set qualified as Set
import Data.Text (Text)
import Data.Text qualified as T
import Data.Version (Version, versionBranch)
import GHC.Driver.Session (OnOff (..))
import GHC.LanguageExtensions.Type (Extension (Cpp, ImplicitPrelude))
import System.Directory (canonicalizePath)
import System.FilePath ((</>))

import Tilia.Cabal.Package
  ( PackageProblem (..),
    PackageReader,
    describePackageProblem,
    newPackageReader,
  )
import Tilia.Cabal.Plan
  ( PlanComponent,
    fetchUninstalled,
    loadPlan,
    macrosOf,
    plannedCompiler,
    plannedFlags,
    readGivenPlan,
    versionMacros,
  )
import Tilia.Cabal.Project (ProjectRoot (..), findProjectRoot)
import Tilia.Cabal.Target
  ( Component,
    TargetProblem,
    componentInPlan,
    describeTargetProblem,
    packageFilesOf,
    projectModules,
  )
import Tilia.Cpp
  ( CppError (..),
    answeredLeaves,
    answeredLinearLeaves,
    contextBeforeHaddock,
    correspondingBranches,
    countLeaves,
    describeCppError,
    everyBranch,
    formatWithCpp,
    joinedByCpp,
    usesCpp,
    withoutRuledOut,
  )
import Tilia.Cpp.Expansion (changedByCpp, hashLines)
import Tilia.Cpp.Macros (Macros)
import Tilia.Doc (printDoc)
import Tilia.Equivalence (commentDifference, syntaxDifference)
import Tilia.Fixity
  ( OpName,
    Unknown (..),
    operatorSpelling,
    spellDisagreement,
    spellUnreadIn,
    unknownOperators,
  )
import Tilia.Fixity.Debug (FixityNotes, fixityNotes)
import Tilia.Fixity.HiFile (HiFile)
import Tilia.Fixity.Plan
  ( Resolver (..),
    Route (FromInterface, FromSource),
    newResolverFrom,
    newResolverVia,
    scopeFor,
  )
import Tilia.MagicComments
  ( Misplacement (..),
    misplacedMagicComment,
    turnedOffThroughout,
  )
import Tilia.Palette (Color (Operator, Place), Palette, paint)
import Tilia.Parser
  ( ParseError,
    ParsedModule,
    ParserConfig (..),
    describeParseError,
    parseConfiguration,
    parseModule,
    pmGathered,
    pmModule,
  )
import Tilia.Pragma
  ( Defaults (..),
    defaultsOf,
    effectiveExtensions,
    movesPositions,
  )
import Tilia.Render (RenderConfig (..), defaultRenderConfig, renderModule)
import Tilia.Settings
  ( Settings (..),
    SettingsProblem,
    describeSettingsProblem,
    readSettings,
    settingsFile,
  )
import Tilia.Source (Written (..), linesOf)
import Tilia.Utils (collected, spellList, tshow)

-- | Why a file could not be formatted.
data FormatError
  = -- | No @cabal.project@ or @.cabal@ file above it.
    NoProject FilePath
  | -- | A project, but no build plan we could read or produce, and why.
    NoBuildPlan FilePath Text
  | -- | A project whose own modules could not be told, and why.
    NoOwnModules FilePath TargetProblem
  | -- | The project's @tilia.toml@ cannot be used, and why.
    InvalidSettings FilePath SettingsProblem
  | -- | The extensions in force in the file could not be settled, and why.
    NoPackage FilePath PackageProblem
  | -- | The file is not Haskell we can parse.
    NotParsed ParseError
  | -- | The file carries @{-# LINE #-}@ or @{-# COLUMN #-}@ pragmas, or lines
    -- GHC's lexer reads past, such as @#line@ ones.
    PositionPragmas FilePath
  | -- | A line holding code that cpp joins to the line before it, and its
    -- number.
    JoinedByCpp FilePath Int
  | -- | A macro the file defines as a context heading a part a Haddock
    -- documents, the macro's line, and the macro.
    ContextBeforeHaddock FilePath Int Text
  | -- | The file uses the preprocessor in a way we cannot handle.
    CppUnsupported FilePath CppError
  | -- | A @{- TILIA_OFF -}@ or @{- TILIA_ON -}@ at this line cannot be
    -- honoured, and why.
    MisplacedMagicComment FilePath Int Misplacement
  | -- | An operator the file uses has a fixity we could not establish, as
    -- the file writes it.
    UnknownFixity FilePath [((Maybe Text, OpName), Unknown)]
  | -- | The file could not be read at all.
    Unreadable FilePath Text
  | -- | The file could not be written back once formatted.
    Unwritable FilePath Text
  | -- | Formatting the file changed its AST.
    NotEquivalent FilePath Text
  | -- | Formatting is not idempotent.
    NotIdempotent FilePath Text
  | -- | Formatting the file threw an exception, shown.
    Threw FilePath Text

-- | Say what went wrong.
describeFormatError :: Palette -> FormatError -> Text
describeFormatError palette = \case
  NoProject path ->
    "no project above "
      <> file path
      <> ": expected a cabal.project or a .cabal file"
  NoBuildPlan root reason ->
    "no build plan for " <> file root <> ": " <> reason
  NoOwnModules root problem ->
    "cannot tell which modules the project in "
      <> file root
      <> " declares: "
      <> describeTargetProblem problem
  InvalidSettings path problem ->
    "cannot use " <> file path <> ": " <> describeSettingsProblem problem
  NoPackage path problem ->
    "cannot tell what "
      <> file path
      <> " is written in: "
      <> describePackageProblem problem
  NotParsed e -> "cannot parse " <> located (describeParseError e)
  PositionPragmas path ->
    "will not format "
      <> file path
      <> ": it uses {-# LINE #-} or {-# COLUMN #-} pragmas or #line, #pragma"
      <> " or #! lines, and no reformatting can leave those true"
  JoinedByCpp path n ->
    "will not format "
      <> file path
      <> ": CPP joins the code at line "
      <> tshow n
      <> " to the line above it"
  ContextBeforeHaddock path n macro ->
    "will not format "
      <> file path
      <> ": the macro "
      <> macro
      <> " at line "
      <> tshow n
      <> " stands for a context before a documented part"
  CppUnsupported path why -> case why of
    ConfigurationNotParsed{} ->
      "will not format " <> located (describeCppError why)
    _ -> "will not format " <> file path <> ": " <> describeCppError why
  MisplacedMagicComment path n why ->
    "will not format "
      <> file path
      <> ": "
      <> case why of
        NotBetweenDeclarations ->
          "the {- TILIA_OFF -} or {- TILIA_ON -} at line "
            <> tshow n
            <> " is not on a line of its own between top-level declarations"
        AroundDirective ->
          "the region that {- TILIA_OFF -} starts at line "
            <> tshow n
            <> " includes a CPP directive which is not supported"
  UnknownFixity path unknown ->
    "will not format "
      <> file path
      <> ": "
      <> T.intercalate ", and " (fmap saying (collected (fmap named unknown)))
    where
      named ((qualifier, op), why) =
        (why, paint palette Operator (operatorSpelling qualifier op))
      saying (why, ops) =
        (if single then "the fixity of " else "the fixities of ")
          <> spellList ops
          <> " "
          <> because
        where
          single = length ops == 1
          because = case why of
            NotRead missing ->
              "may be declared in " <> spellUnreadIn palette missing
            Ambiguous offers ->
              (if single then "is " else "are ")
                <> spellDisagreement palette offers
  Unreadable path why -> "cannot read " <> file path <> ": " <> why
  Unwritable path why -> "cannot write " <> file path <> ": " <> why
  NotEquivalent path why ->
    "formatting " <> file path <> " changed the program: " <> why
  NotIdempotent path why ->
    "formatting " <> file path <> " is not idempotent: " <> why
  Threw path why ->
    "formatting " <> file path <> " threw an exception: " <> why
  where
    file = paint palette Place . T.pack
    located t = case T.breakOn ":" t of
      (where', rest) -> paint palette Place where' <> rest

-- | The exit status a failure should leave behind, distinct for each error
-- constructor.
formatErrorExitCode :: FormatError -> Int
formatErrorExitCode = \case
  NoProject{} -> 2
  NoBuildPlan{} -> 3
  NoOwnModules{} -> 4
  InvalidSettings{} -> 5
  NoPackage _ problem -> case problem of
    NoPackageFile -> 6
    PackageUnreadable{} -> 7
    PackageMalformed{} -> 8
    FileUnclaimed{} -> 9
  NotParsed{} -> 10
  PositionPragmas{} -> 11
  JoinedByCpp{} -> 12
  ContextBeforeHaddock{} -> 13
  CppUnsupported _ why -> case why of
    MalformedConditional{} -> 14
    TooManyConfigurations -> 15
    ConfigurationNotParsed{} -> 16
    DirectiveInQuotedText{} -> 17
    MacroInQuotedText{} -> 18
    BeforeLeadingComma{} -> 19
    RuledOutBranch{} -> 20
    AbortingAlternative{} -> 21
    HaddockReadDifferently{} -> 22
  MisplacedMagicComment{} -> 23
  UnknownFixity{} -> 24
  Unreadable{} -> 25
  Unwritable{} -> 26
  NotEquivalent{} -> 27
  NotIdempotent{} -> 28
  Threw{} -> 29

-- | Did we decline to format the file, rather than fail to?
refused :: FormatError -> Bool
refused = \case
  NoProject{} -> False
  NoBuildPlan{} -> False
  NoOwnModules{} -> False
  InvalidSettings{} -> False
  NoPackage{} -> False
  NotParsed{} -> False
  PositionPragmas{} -> True
  JoinedByCpp{} -> True
  ContextBeforeHaddock{} -> True
  CppUnsupported{} -> True
  MisplacedMagicComment{} -> True
  UnknownFixity{} -> True
  Unreadable{} -> False
  Unwritable{} -> False
  NotEquivalent{} -> False
  NotIdempotent{} -> False
  Threw{} -> False

-- | Did formatting itself fail, rather than the file or the project keep
-- it from starting?
formattingFailed :: FormatError -> Bool
formattingFailed = \case
  NoProject{} -> False
  NoBuildPlan{} -> False
  NoOwnModules{} -> False
  InvalidSettings{} -> False
  NoPackage{} -> False
  NotParsed{} -> False
  PositionPragmas{} -> False
  JoinedByCpp{} -> False
  ContextBeforeHaddock{} -> False
  CppUnsupported{} -> False
  MisplacedMagicComment{} -> False
  UnknownFixity{} -> False
  Unreadable{} -> False
  Unwritable{} -> False
  NotEquivalent{} -> True
  NotIdempotent{} -> True
  Threw{} -> True

-- | What a run works out once and then uses for every file.
data Session = Session
  { -- | The project.
    sessionRoot :: ProjectRoot,
    -- | The modules the project's own packages declare, each with the names
    -- of those that declare it.
    sessionOwnModules :: Map Text (Set Text),
    -- | What the project sets in its @tilia.toml@.
    sessionSettings :: Settings,
    -- | What can be asked about the modules a file imports.
    sessionResolver :: Resolver,
    -- | What the plan settles about the questions a file's conditionals
    -- ask, so that a branch it rules out is not read as part of the file.
    sessionMacros :: Macros,
    -- | What each file's package puts in force.
    sessionPackage :: PackageReader,
    -- | Whether to check AST equivalence, and in which configurations.
    sessionCheckAst :: AstCheck,
    -- | Whether to check idempotence.
    sessionCheckIdempotence :: Choice "checkIdempotence",
    -- | An account of how each file's fixities were determined, or 'Nothing'
    -- where none was asked for.
    sessionFixityNotes :: Maybe (IORef (Map FilePath FixityNotes))
  }

-- | Whether to check AST equivalence, and in which configurations of a
-- module that uses the preprocessor.
data AstCheck
  = -- | Not at all.
    NoAstCheck
  | -- | In one configuration for each branch of each conditional, as a run
    -- does.
    EachCppBranch
  | -- | In every configuration, up to as many as given, and past that in
    -- the ones varying a single conditional, once both sides have as many.
    EachCppConfiguration Int
  deriving (Eq, Show)

-- | The AST check a run makes where it is asked for one.
astCheckFor :: Choice "checkAst" -> AstCheck
astCheckFor checkAst =
  if isTrue checkAst then EachCppBranch else NoAstCheck

-- | Where a session's build plan comes from.
data PlanSource
  = -- | The project's own, solved again or fetched for with Cabal.
    PlanFromCabal [PlanComponent]
  | -- | A plan file trusted as up to date, so that we can avoid running
    -- Cabal to solve it.
    GivenPlan FilePath

-- | Where a session's build plan comes from: the plan file given, if any,
-- or else Cabal, solving for the components the action finds.
planSourceFor :: Maybe FilePath -> IO [Component] -> IO PlanSource
planSourceFor givenPlan components = case givenPlan of
  Just plan -> pure (GivenPlan plan)
  Nothing -> PlanFromCabal . mapMaybe componentInPlan <$> components

-- | Settle everything that does not depend on the file being formatted.
newSession ::
  -- | The project.
  ProjectRoot ->
  -- | Where the build plan comes from.
  PlanSource ->
  -- | Whether to read from and write to the cache.
  Choice "useCache" ->
  -- | Whether to download sources that are missing.
  Choice "download" ->
  -- | Check AST equivalence.
  Choice "checkAst" ->
  -- | Check idempotence.
  Choice "checkIdempotence" ->
  -- | Record how every file's fixities were settled, to be read afterwards
  -- with 'fixityNotesOf'.
  Choice "debugFixity" ->
  IO (Either FormatError Session)
newSession
  project
  planSource
  caching
  downloading
  checkAst
  checkIdempotence
  debugFixity = runExceptT $ do
    let root = prPath project
    (settings, packages, ownModules) <- fromProject project
    (plan, routes) <- case planSource of
      PlanFromCabal components -> do
        plan <-
          orElse (NoBuildPlan root)
            =<< liftIO (loadPlan caching downloading components root)
        pure (plan, [FromInterface, FromSource])
      GivenPlan path -> do
        plan <- orElse (NoBuildPlan root) =<< liftIO (readGivenPlan root path)
        routes <-
          if isTrue downloading
            then
              [FromInterface, FromSource]
                <$ liftIO (fetchUninstalled caching root plan)
            else pure [FromInterface]
        pure (plan, routes)
    packageReader <-
      liftIO
        (newPackageReader (plannedCompiler plan) (plannedFlags plan) packages)
    resolver <- liftIO (newResolverVia caching routes packageReader root plan)
    notes <-
      if isTrue debugFixity
        then Just <$> liftIO (newIORef Map.empty)
        else pure Nothing
    pure
      Session
        { sessionRoot = project,
          sessionOwnModules = ownModules,
          sessionSettings = settings,
          sessionResolver = resolver,
          sessionMacros = macrosOf plan,
          sessionPackage = packageReader,
          sessionCheckAst = astCheckFor checkAst,
          sessionCheckIdempotence = checkIdempotence,
          sessionFixityNotes = notes
        }

-- | What compiling a file's component establishes, which a session
-- otherwise works out with Cabal, the package database, and the sources of
-- dependencies.
data Compiled = Compiled
  { -- | The extensions the component puts in force, which an edition a
    -- module names adds to, as which of them its own edition brought is not
    -- known.
    compiledExtensions :: [Extension],
    -- | The version of the compiler.
    compiledWith :: Version,
    -- | The version of each package the component depends on, by name.
    compiledPackages :: Map Text Version,
    -- | The interface of a module the component can import, by name.
    compiledInterface :: Text -> IO (Maybe HiFile)
  }

-- | Settle everything that does not depend on the file being formatted from
-- what compiling its component establishes, and from the project's @.cabal@
-- files which of its modules are its own.
compiledSession ::
  -- | The file.
  FilePath ->
  -- | What compiling its component establishes.
  Compiled ->
  -- | Check AST equivalence.
  Choice "checkAst" ->
  -- | Check idempotence.
  Choice "checkIdempotence" ->
  IO (Either FormatError Session)
compiledSession file Compiled{..} checkAst checkIdempotence = runExceptT $ do
  project <-
    maybe (throwE (NoProject file)) pure =<< liftIO (findProjectRoot file)
  (settings, _, ownModules) <- fromProject project
  resolver <- liftIO (newResolverFrom compiledInterface)
  pure
    Session
      { sessionRoot = project,
        sessionOwnModules = ownModules,
        sessionSettings = settings,
        sessionResolver = resolver,
        sessionMacros =
          versionMacros
            (Just (numbers compiledWith))
            (fmap numbers compiledPackages),
        sessionPackage =
          \_ ->
            pure
              (Right (defaultsOf False Set.empty (On <$> compiledExtensions))),
        sessionCheckAst = astCheckFor checkAst,
        sessionCheckIdempotence = checkIdempotence,
        sessionFixityNotes = Nothing
      }
  where
    numbers = fmap toInteger . versionBranch

-- | What the run made of every file's operators, by file, empty unless the
-- session keeps an account of it.
fixityNotesOf :: Session -> IO (Map FilePath FixityNotes)
fixityNotesOf session = case sessionFixityNotes session of
  Nothing -> pure Map.empty
  Just ref -> readIORef ref

-- | Format source that has already been read.
formatSource ::
  -- | What the run has worked out already.
  Session ->
  -- | The file the source came from.
  FilePath ->
  -- | The source.
  Text ->
  -- | Result.
  IO (Either FormatError Text)
formatSource session path source
  | turnedOffThroughout source = pure (Right source)
  | otherwise = formatTurnedOn session path source

-- | 'formatSource' for a module no @{- TILIA_OFF -}@ turns off throughout.
formatTurnedOn ::
  -- | What the run has worked out already.
  Session ->
  -- | The file the source came from.
  FilePath ->
  -- | The source.
  Text ->
  IO (Either FormatError Text)
formatTurnedOn session path source = runExceptT $ do
  package <- orElse (NoPackage path) =<< liftIO (sessionPackage session path)
  importer <- liftIO (canonicalizePath path)
  let resolver = sessionResolver session
      config = ParserConfig package
      settings = sessionSettings session
      misplaced (n, why) = throwE (MisplacedMagicComment path n why)
      extensionsAndCpp text =
        let declared = effectiveExtensions package text
         in (declared, usesCpp declared text)
      renderConfigWith extensions =
        defaultRenderConfig
          { rcOwnModules = sessionOwnModules session,
            rcExtensions = extensions,
            rcSettings = settings
          }
      renderConfigFor extensions taken parsed = do
        let implicitPrelude =
              fromBool (Set.member ImplicitPrelude extensions)
        scope <-
          liftIO $
            scopeFor
              resolver
              (Just importer)
              implicitPrelude
              (fmap pmModule parsed)
        liftIO $ case sessionFixityNotes session of
          Nothing -> pure ()
          Just ref -> do
            told <-
              fixityNotes
                implicitPrelude
                (askModule resolver (Just importer))
                scope
                parsed
            atomicModifyIORef'
              ref
              (\m -> (Map.insertWith (\_ old -> old) path told m, ()))
        let unknownAmong configurations =
              unknownOperators configurations scope (fmap pmGathered parsed)
            unknown = case unknownAmong Nothing of
              [] -> []
              found -> maybe found (unknownAmong . Just . fmap pmModule) taken
        case unknown of
          [] -> pure (renderConfigWith extensions){rcScope = Just scope}
          _ -> throwE (UnknownFixity path unknown)
      formatting (extensions, cpp) already text
        | isTrue cpp = do
            render <- case everyBranch
              config
              path
              (withoutRuledOut (sessionMacros session) text) of
              Nothing -> pure (renderConfigWith extensions)
              Just branches -> do
                traverse_ misplaced (misplacedMagicComment text branches)
                traverse_
                  (throwE . uncurry (ContextBeforeHaddock path))
                  (contextBeforeHaddock text branches)
                renderConfigFor
                  extensions
                  ( everyBranch
                      config
                      path
                      ( withoutRuledOut
                          (sessionMacros session <> defaultsMacros package)
                          text
                      )
                  )
                  branches
            printed <-
              orElse
                (CppUnsupported path)
                (formatWithCpp config render path text)
            pure (printed, Nothing)
        | otherwise = do
            parsed <-
              maybe
                (orElse NotParsed (parseModule config path text))
                pure
                already
            traverse_ misplaced (misplacedMagicComment text (pure parsed))
            render <- renderConfigFor extensions Nothing (pure parsed)
            pure (printDoc settings (renderModule render parsed), Just parsed)
  let inForce@(extensions, _) = extensionsAndCpp source
  when (movesPositions extensions source) $
    throwE (PositionPragmas path)
  when (Set.member Cpp extensions) $
    traverse_
      (throwE . JoinedByCpp path)
      (joinedByCpp config source)
  (formatted, tree) <- formatting inForce Nothing source
  let (changed, printedTree) =
        rewritten
          config
          astCheck
          extensions
          path
          (source, tree)
          formatted
      astCheck = sessionCheckAst session
  traverse_ (throwE . NotEquivalent path) changed
  when (isTrue (sessionCheckIdempotence session)) $ do
    (settled, _) <-
      formatting (extensionsAndCpp formatted) printedTree formatted
    when (settled /= formatted) $
      throwE (NotIdempotent path (whyNotIdempotent formatted settled))
  pure formatted

-- | Why a second formatting pass did not come out like the first.
whyNotIdempotent ::
  -- | What the first pass printed.
  Text ->
  -- | What the second pass printed.
  Text ->
  Text
whyNotIdempotent before after =
  case [n | (n, one, two) <- zip3 [1 :: Int ..] first second, one /= two] of
    (n : _) -> "line " <> tshow n <> " differs"
    [] ->
      "the second pass came out "
        <> tshow (length second)
        <> " lines long where the first came out "
        <> tshow (length first)
  where
    first = T.lines before
    second = T.lines after

-- | What formatting changed about the program.
rewritten ::
  -- | How to parse both sides.
  ParserConfig ->
  -- | Whether to compare the two, and in which configurations where the
  -- file uses the preprocessor.
  AstCheck ->
  -- | The extensions in force.
  Set Extension ->
  -- | The file, for the parser's messages.
  FilePath ->
  -- | What was read, and the tree it was printed from where it has one.
  (Text, Maybe ParsedModule) ->
  -- | What was printed.
  Text ->
  -- | What formatting changed, and the tree of what was printed.
  (Maybe Text, Maybe ParsedModule)
rewritten config astCheck extensions path (before, printedFrom') after
  | NoAstCheck <- astCheck = (Nothing, Nothing)
  | not readByCpp = case parseModule config path after of
      Left e ->
        ( Just ("the formatted output does not parse: " <> describeParseError e),
          Nothing
        )
      Right a' -> case printedFrom'
        <|> whatParsed (parseModule config path before) of
        Nothing -> (Nothing, Just a')
        Just b' -> (comparing b' a', Just a')
  | otherwise = (hashLinesDifference before after <|> underCpp, Nothing)
  where
    readByCpp = Set.member Cpp extensions && any changedByCpp [before, after]
    comparing b' a' =
      syntaxDifference (pmModule b') (pmModule a')
        <|> commentDifference b' a'
    whatParsed = either (const Nothing) Just
    underCpp = case astCheck of
      NoAstCheck -> Nothing
      EachCppBranch -> case correspondingBranches before after of
        Left _ ->
          Just "the input or output could not be split into configurations"
        Right pairs -> firstJust [compareBranch b a | (b, a) <- pairs]
      EachCppConfiguration limit ->
        case (countLeaves before, countLeaves after) of
          (Left why, _) -> unsplit "input" why
          (_, Left why) -> unsplit "output" why
          (Right went, Right came)
            | went /= came ->
                Just
                  ( "formatting changed how many configurations there are,"
                      <> " from "
                      <> tshow went
                      <> " to "
                      <> tshow came
                  )
            | otherwise -> case (enumerate before, enumerate after) of
                (Left why, _) -> unsplit "input" why
                (_, Left why) -> unsplit "output" why
                (Right b, Right a) -> alongside b a
            where
              enumerate
                | went <= toInteger limit = answeredLeaves
                | otherwise = answeredLinearLeaves
    unsplit side why =
      Just ("the " <> side <> "'s configurations: " <> describeCppError why)
    alongside went came =
      firstJust $
        [ maybe
            (Just "a configuration of the input the output does not have")
            (compareBranch (Just b) . Just)
            (Map.lookup answers output)
        | (answers, b) <- went
        ]
          <> [ Just "a configuration of the output the input does not have"
             | any ((`Map.notMember` input) . fst) came
             ]
      where
        input = Map.fromList went
        output = Map.fromList came
    compareBranch Nothing Nothing = Nothing
    compareBranch Nothing (Just _) =
      Just "a configuration no longer aborts preprocessing"
    compareBranch (Just _) Nothing =
      Just "a configuration now aborts preprocessing"
    compareBranch (Just b) (Just a) =
      ("in one configuration, " <>) <$> difference b a
    difference b a = case (configuration before b, configuration after a) of
      (Left e, _) -> unread e
      (_, Left e) ->
        Just ("the formatted output does not parse: " <> describeParseError e)
      (Right b', Right a') -> comparing b' a'
    configuration whole t =
      parseConfiguration
        config
        path
        (linesOf (Written whole))
        (linesOf (Written t))
        t
    unread e = case astCheck of
      EachCppConfiguration _ ->
        Just ("the input does not parse: " <> describeParseError e)
      _ -> Nothing
    firstJust = foldr (<|>) Nothing

-- | How formatting changed the lines whose first character is a hash, which
-- cpp acts on or GHC reads past, if it did.
hashLinesDifference ::
  -- | What was read.
  Text ->
  -- | What was printed.
  Text ->
  Maybe Text
hashLinesDifference before after = go (hashLines before) (hashLines after)
  where
    go (b : bs) (a : as)
      | b == a = go bs as
      | otherwise = Just ("the line `" <> b <> "` became `" <> a <> "`")
    go (b : _) [] = Just ("lost the line `" <> b <> "`")
    go [] (a : _) = Just ("gained the line `" <> a <> "`")
    go [] [] = Nothing

-- | What a session takes from the project itself: its settings, the
-- @.cabal@ files of its packages and the modules they declare.
fromProject ::
  ProjectRoot ->
  ExceptT FormatError IO (Settings, [FilePath], Map Text (Set Text))
fromProject project = do
  settings <-
    orElse (InvalidSettings (root </> settingsFile))
      =<< liftIO (readSettings root)
  packages <- orElse (NoOwnModules root) =<< liftIO (packageFilesOf project)
  ownModules <- orElse (NoOwnModules root) =<< liftIO (projectModules packages)
  pure (settings, packages, ownModules)
  where
    root = prPath project

-- | Give up with the given error where there is one to give up over.
orElse :: (e -> FormatError) -> Either e a -> ExceptT FormatError IO a
orElse f = either (throwE . f) pure