packages feed

tilia-0.2.0.0: src/Tilia/Render.hs

-- | Turning a parsed module into a document.
module Tilia.Render
  ( RenderConfig (..),
    defaultRenderConfig,
    renderModule,
    renderConfiguration,
  )
where

import Control.Applicative ((<|>))
import Control.Monad (guard)
import Data.Char (isSpace)
import Data.IntMap.Strict qualified as IntMap
import Data.IntSet (IntSet)
import Data.IntSet qualified as IntSet
import Data.List.NonEmpty qualified as NE
import Data.Map.Strict (Map)
import Data.Map.Strict qualified as Map
import Data.Maybe (isNothing, listToMaybe, mapMaybe)
import Data.Set qualified as Set
import Data.Text (Text)
import Data.Text qualified as T
import GHC.Hs (HsModule (..), XModulePs (..))
import GHC.Hs.Extension (GhcPs)
import GHC.Types.SrcLoc (getLoc)

import Tilia.Comments
  ( Comment (..),
    asOrdinary,
    bracketed,
    carriedOnFrom,
    commentRuns,
    commentTrailing,
    isDivider,
    widenTrigger,
  )
import Tilia.Comments.Attach (attachComments)
import Tilia.Doc.Combinators hiding (break)
import Tilia.Fixity (capturedUses)
import Tilia.Gathered (Gathered)
import Tilia.Imports (travellingComments)
import Tilia.MagicComments (preservedLines)
import Tilia.Parser (ParsedModule (..))
import Tilia.Render.Context
import Tilia.Render.Declaration (decls)
import Tilia.Render.Expression (hsExpr, untypedSplice)
import Tilia.Render.Haddock (haddockSpans)
import Tilia.Render.Header
  ( HeaderPragma (..),
    ModuleCtx (..),
    hsModule,
    takeHeaderPragmas,
    takeStackHeader,
  )
import Tilia.Render.Signature (sigDecl)
import Tilia.Settings (Settings (..))
import Tilia.Source (Lines, blankAt, comments, lineAt, sourceLines)
import Tilia.Span
import Tilia.Span.Ghc (spanOf, spanOfSrcSpan)

-- | Render a parsed module, comments and all.
renderModule ::
  -- | How to render it.
  RenderConfig ->
  -- | The module, parsed.
  ParsedModule ->
  Doc
renderModule render parsed = attachComments loose doc
  where
    (doc, loose) = renderConfiguration render parsed

-- | Render one configuration of a module, and return the comments the
-- syntax tree does not carry rather than attach them.
renderConfiguration ::
  -- | How to render it.
  RenderConfig ->
  -- | The configuration, parsed.
  ParsedModule ->
  (Doc, [Comment])
renderConfiguration render parsed =
  ( prologue (pmPrologue parsed)
      <> stackHeader
      <> hsModule ctx mctx pragmas hsMod,
    loose
  )
  where
    hsMod = pmModule parsed
    (haddocks, loose') =
      splitHaddocks
        (sourceLines (pmSource parsed))
        (pmGathered parsed)
        (comments (pmSource parsed))
    (stackHeader, rest) = takeStackHeader (pmPrologue parsed) loose'
    (pragmas, uncovered) =
      takeHeaderPragmas (pmSource parsed) (pmHeaderEnd parsed) rest
    held = heldOffModuleDoc hsMod haddocks pragmas uncovered
    (mctx, kept) = moduleCtx render parsed held
    loose = fmap (carryingOnSorted sorted carryingOn) kept
    carryingOn = carriedOnFrom kept
    sorted =
      IntSet.fromList . fmap spanEndLine $
        fmap hpSpan pragmas <> mapMaybe spanOf (hsmodImports hsMod)
    ctx =
      Ctx
        { ctxRender = render,
          ctxSourceType = pmSourceType parsed,
          ctxCaptured = capturedUses (pmGathered parsed),
          ctxSource = pmSource parsed,
          ctxHaddocks = indexOn haddocks,
          ctxKnot = knot,
          ctxIndentation = settingsIndentation (rcSettings render),
          ctxCommaStyle = settingsCommaStyle (rcSettings render),
          ctxArrowStyle = settingsArrowStyle (rcSettings render)
        }

-- | What printing the top level of one configuration takes, and the
-- comments it does not carry with the empty lines around them that stay.
moduleCtx ::
  -- | How to render it.
  RenderConfig ->
  -- | The configuration, parsed.
  ParsedModule ->
  -- | The comments neither its syntax tree nor its header carries.
  [Comment] ->
  (ModuleCtx, [Comment])
moduleCtx render parsed held =
  ( ModuleCtx
      { mcOpening = opening,
        mcHeadings = Set.toList (Set.fromList (mapMaybe heading held)),
        mcUnderImports = maybe id IntMap.delete final underImports,
        mcBelowImports = (`IntMap.lookup` underImports) =<< final,
        mcMagicCommentRanges = magicCommentRanges
      },
    [ if among c && isNothing (heading c) || IntSet.member (line c) travellers
        then withoutGaps c'
        else c'
    | c <- held,
      not (leftAlone c),
      let c' = belowOpening opening c
    ]
  )
  where
    hsMod = pmModule parsed
    ls = sourceLines (pmSource parsed)
    opening = importsOpening ls held (fst <$> bounds (listToMaybe starts))
    imports = mapMaybe spanOf (hsmodImports hsMod)
    among c = amongImports (bounds (Just (line c))) imports c
    final = snd <$> (bounds . Just . NE.last =<< NE.nonEmpty starts)
    bounds having = rcImportBounds render having <|> own
    own = (\ss -> (NE.head ss, NE.last ss)) <$> NE.nonEmpty starts
    starts = fmap spanStartLine imports
    blocks =
      commentRuns (filter (\c -> among c && not (commentTrailing c)) held)
    heading c = do
      guard (among c)
      (top, bottom) <- IntMap.lookup (spanStartLine (commentSpan c)) blocks
      let edges =
            ( spanStartLine (commentSpan top) - 1,
              spanEndLine (commentSpan bottom) + 1
            )
      guard $
        headsImports
          (rcImportBarriers render)
          ls
          imports
          (spanStartColumn (commentSpan c))
          edges
      pure (snd edges)
    travelling = travellingComments ls held hsMod
    underImports = fmap ((+ 1) . spanEndLine . commentSpan . NE.last) travelling
    travellers = IntSet.fromList (line <$> foldMap NE.toList travelling)
    line = spanStartLine . commentSpan
    withoutGaps c = c{commentGapAbove = False, commentGapBelow = False}
    leftAlone c = any (holds (spanStartLine (commentSpan c))) magicCommentRanges
    holds n (from, to) = from <= n && n <= to
    magicCommentRanges = preservedLines (pmSource parsed)

-- | Hold the comment that comes out right under the module's own Haddock
-- off it unless it is a divider: the first one between the Haddock and the
-- module line with no pragma after it, since one with a pragma after it is
-- placed with that pragma, above the Haddock.
heldOffModuleDoc ::
  -- | The module.
  HsModule GhcPs ->
  -- | The Haddocks of the module, the module's own among them.
  [Comment] ->
  -- | The pragmas the header is about to hoist.
  [HeaderPragma] ->
  -- | The comments.
  [Comment] ->
  [Comment]
heldOffModuleDoc hsMod haddocks pragmas cs
  | Just ended <- endOfModuleDoc,
    Just began <- startOfModuleLine,
    (before', c : after') <- break (uncoveredBetween ended began) cs,
    not (isDivider (NE.head (commentBody c))) =
      before' <> (c{commentGapAbove = True} : after')
  | otherwise = cs
  where
    uncoveredBetween ended began c =
      ended < spanStartLine here
        && spanStartLine here < began
        && not (any (\p -> spanEndLine here < p && p < began) pragmaStarts)
      where
        here = commentSpan c
    pragmaStarts = fmap (spanStartLine . hpSpan) pragmas
    endOfModuleDoc = do
      s <- moduleDoc
      c <-
        lookup (startPoint s) [(startPoint (commentSpan h), h) | h <- haddocks]
      if bracketed c then Nothing else Just (spanEndLine s)
    moduleDoc = case hsmodExt hsMod of
      XModulePs{hsmodHaddockModHeader = Just d} -> spanOfSrcSpan (getLoc d)
      _ -> Nothing
    startOfModuleLine = spanStartLine <$> (spanOf =<< hsmodName hsMod)

-- | The empty line right above the first import, or above the comments
-- written on top of it and the lines that hold only semicolons.
importsOpening ::
  -- | The module as written.
  Lines ->
  -- | Its comments.
  [Comment] ->
  -- | The line the first import begins on in the configurations that have
  -- the line of this one's first import, or in any where it has none.
  Maybe Int ->
  Maybe Int
importsOpening ls cs first = climb . pred =<< first
  where
    climb n
      | blankAt n ls = Just n
      | maybe False (T.all (\ch -> ch == ';' || isSpace ch)) (lineAt n ls) =
          climb (pred n)
      | otherwise = climb . pred =<< IntMap.lookup n onTheirOwnLines
    onTheirOwnLines =
      IntMap.fromList
        [ (spanEndLine s, spanStartLine s)
        | c <- cs,
          not (commentTrailing c),
          let s = commentSpan c
        ]

-- | Leave the empty line above the comments written on top of the first
-- import at the top of the imports, rather than carry it along when sorting
-- moves that import.
belowOpening :: Maybe Int -> Comment -> Comment
belowOpening opening c
  | Just l <- opening,
    spanStartLine (commentSpan c) == l + 1 =
      c{commentGapAbove = False}
  | otherwise = c

-- | Leave the empty line under a comment that carries on the remark of a
-- header pragma or an import to the lines sorting puts in order, which keep
-- none of their own, rather than carry it along when sorting moves the line.
carryingOnSorted ::
  -- | The last line of every header pragma and every import.
  IntSet ->
  -- | The line whose remark a comment carries on, and the column that
  -- remark begins at.
  (Comment -> Maybe (Int, Int)) ->
  -- | The comment.
  Comment ->
  Comment
carryingOnSorted sorted carryingOn c
  | Just (line, remark) <- carryingOn c,
    IntSet.member line sorted,
    remark <= spanStartColumn (commentSpan c) =
      c{commentGapBelow = False}
  | otherwise = c

-- | Does a block of comments head the imports under it: is it written at
-- their indentation, with empty lines right above and right under it, and
-- no line they must not be sorted across between it and the first of them?
headsImports ::
  -- | The lines the imports are not sorted across.
  [Int] ->
  -- | The module as written.
  Lines ->
  -- | Where the imports are.
  [Span] ->
  -- | The column the block begins at.
  Int ->
  -- | The lines right above and right under the block.
  (Int, Int) ->
  Bool
headsImports barriers ls imports column (above, under) =
  blankAt above ls
    && blankAt under ls
    && case dropWhile ((< under) . spanStartLine) imports of
      next : _ ->
        spanStartColumn next == column
          && not (any (\b -> under < b && b < spanStartLine next) barriers)
      [] -> False

-- | Is a comment written between imports rather than inside one?
amongImports ::
  -- | The first and the last line an import begins on in any configuration
  -- that has the comment, where they have imports.
  Maybe (Int, Int) ->
  -- | Where the imports of this configuration are.
  [Span] ->
  -- | The comment.
  Comment ->
  Bool
amongImports bounds imports c = case bounds of
  Just (first, final) ->
    (first < spanStartLine s || first == spanStartLine s && commentTrailing c)
      && spanEndLine s < final
      && not (any (`covers` s) imports)
  Nothing -> False
  where
    s = commentSpan c

-- | The lines above the module, put back exactly as they were written.
prologue :: [Text] -> Doc
prologue = foldMap (\l -> txt l <> newline)

-- | The knot: the printers that a module below their definition needs.
knot :: Knot
knot =
  Knot
    { knotExpr = hsExpr,
      knotSplice = untypedSplice,
      knotSig = sigDecl,
      knotDecls = decls
    }

-- | Separate the comments the syntax tree also knows about from the rest.
splitHaddocks ::
  -- | The module's lines.
  Lines ->
  -- | What the tree holds.
  Gathered ->
  -- | Every comment in the module.
  [Comment] ->
  -- | The ones the tree carries, and the ones it does not.
  ([Comment], [Comment])
splitHaddocks ls found = foldr sort' ([], [])
  where
    inTree = Set.fromList (fmap startPoint (haddockSpans found))
    sort' c (docs, rest)
      | startPoint (commentSpan c) `Set.member` inTree =
          (widenTrigger c : docs, rest)
      | otherwise = (docs, asOrdinary ls c <> rest)

-- | Index comments by where they begin.
indexOn :: [Comment] -> Map (Int, Int) Comment
indexOn cs = Map.fromList [(startPoint (commentSpan c), c) | c <- cs]