packages feed

tilia-0.2.0.0: src/Tilia/Doc.hs

{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE PatternSynonyms #-}
{-# LANGUAGE ViewPatterns #-}

-- | Documents: what they hold, and turning them into source text.
module Tilia.Doc
  ( Doc (..),
    LineStart (..),
    TrailingWhitespace (..),
    Layout (..),
    SeparatorSide (..),
    BracketSide (..),
    DeclDelimiterStyle (..),
    CommentPlacement (..),
    Spill (..),
    Conditional (..),
    conditionalRange,
    printDoc,
    separatorSide,
    printsNothing,
    namesOnly,
    referentIn,
    pattern Cat,
    printsOnOneLine,
    choosing,
    foldChildren,
    foldlChildren',
    mapChildren,
    regionsIn,
    foldRegions,
    regionOf,
    spine,
    spineAt,
    printedFrom,
    onlySpacing,
    onlyBreaks,
    edgeTexts,
    Wrapper (..),
    unwrap,
    wrap,
    layoutInside,
  )
where

import Data.Maybe (mapMaybe)
import Data.Monoid (Any (..))
import Data.Text (Text)
import Data.Text qualified as T

import Tilia.Doc.Internal
  ( BracketSide (..),
    CommentPlacement (..),
    Conditional (..),
    DeclDelimiterStyle (..),
    Doc (..),
    Layout (..),
    LineStart (..),
    SeparatorSide (..),
    Spill (..),
    TrailingWhitespace (..),
    Wrapper (..),
    conditionalRange,
    layoutInside,
    namesOnly,
    printDoc,
    printsNothing,
    referentIn,
    unwrap,
    wrap,
  )
import Tilia.Span (Span, spanEndLine, spanStartLine)

-- | The side a separator, in the indentation it was written in or named as
-- a referent, is written on.
separatorSide :: Doc -> Maybe SeparatorSide
separatorSide = \case
  DSeparator s _ _ -> Just s
  DIndented _ _ d -> separatorSide d
  DReferent _ d -> separatorSide d
  _ -> Nothing

-- | Does this document print on one line, holding neither a line break nor
-- a choice?
printsOnOneLine ::
  -- | The layout of the group it is printed in.
  Layout ->
  -- | The document.
  Doc ->
  Bool
printsOnOneLine layout = \case
  DCat a b -> printsOnOneLine layout a && printsOnOneLine layout b
  DIndented _ a b -> printsOnOneLine layout a && printsOnOneLine layout b
  DVariant flatD brokenD ->
    printsOnOneLine layout (case layout of Broken -> brokenD; _ -> flatD)
  DNewline -> False
  DBreak -> layout /= Broken
  DBreak' -> layout /= Broken
  DVerbatimBreak{} -> False
  DCppChoice{} -> False
  x ->
    maybe True (\(w, y) -> printsOnOneLine (layoutInside layout w) y) (unwrap x)

-- | Is there a choice anywhere in this document?
choosing :: Doc -> Bool
choosing = getAny . go
  where
    go = \case
      DCppChoice{} -> Any True
      d -> foldChildren go d

-- | Combine what a function makes of each document directly inside this
-- one, a variant's broken layout standing for the variant.
foldChildren ::
  Monoid m =>
  -- | What to make of each document inside.
  (Doc -> m) ->
  -- | The document.
  Doc ->
  m
foldChildren f = \case
  DCat a b -> f a <> f b
  DIndented _ a b | not (namesOnly a) -> f a <> f b
  DVariant _ d -> f d
  DCppChoice _ bs e -> foldr ((<>) . f . snd) (f e) bs
  d -> maybe mempty (f . snd) (unwrap d)
{-# INLINE foldChildren #-}

-- | 'foldChildren' as a strict left fold, which threads what it makes
-- through the documents inside in the order they are printed.
foldlChildren' ::
  -- | What to make of a document inside, given what was made before it.
  (a -> Doc -> a) ->
  -- | What there is before the first document inside.
  a ->
  -- | The document.
  Doc ->
  a
foldlChildren' f z = \case
  DCat a b -> let !z' = f z a in f z' b
  DIndented _ a b | not (namesOnly a) -> let !z' = f z a in f z' b
  DVariant _ d -> f z d
  DCppChoice _ bs e -> let !z' = foldl' (\acc -> f acc . snd) z bs in f z' e
  d -> maybe z (f z . snd) (unwrap d)
{-# INLINE foldlChildren' #-}

-- | Rewrite the documents directly inside this one, both layouts of a
-- variant included.
mapChildren ::
  -- | How to rewrite each document inside.
  (Doc -> Doc) ->
  -- | The document.
  Doc ->
  Doc
mapChildren f = \case
  DCat a b -> f a <> f b
  DIndented n a b | not (namesOnly a) -> DIndented n (f a) (f b)
  DVariant a b -> DVariant (f a) (f b)
  DCppChoice cs bs e -> DCppChoice cs [(g, f d) | (g, d) <- bs] (f e)
  d -> maybe d (\(w, x) -> wrap w (f x)) (unwrap d)
{-# INLINE mapChildren #-}

-- | Every region a document records, and what it holds.
regionsIn :: Doc -> [(Span, Doc)]
regionsIn = foldRegions (\s d inside -> (s, d) : inside)

-- | Fold the regions a document records, giving each its span, what it
-- holds, and what the fold made of the regions inside it.
foldRegions ::
  Monoid m =>
  -- | What to make of a region, given what was made of those inside it.
  (Span -> Doc -> m -> m) ->
  -- | The document.
  Doc ->
  m
foldRegions f = go
  where
    go = \case
      DLocated s d -> f s d (go d)
      DFence s d -> f s d (go d)
      d -> foldChildren go d
{-# INLINE foldRegions #-}

-- | The smallest span covering the outermost regions a document records.
regionOf :: Doc -> Maybe Span
regionOf = \case
  DLocated s _ -> Just s
  DFence s _ -> Just s
  d -> foldChildren regionOf d

-- | A document that prints one document and then another: a
-- concatenation, or a relation printing the part it measures from, as that
-- part and the relation left with only the name of its referent.
pattern Cat :: Doc -> Doc -> Doc
pattern Cat a b <- (halves -> Just (a, b))

-- | The two documents a document prints one after the other, if it does.
halves :: Doc -> Maybe (Doc, Doc)
halves = \case
  DCat a b -> Just (a, b)
  DIndented n a b
    | not (namesOnly a) ->
        Just
          (a, DIndented n (maybe mempty (`DReferent` mempty) (referentIn a)) b)
  _ -> Nothing

-- | A document as the sequence of things it concatenates.
spine :: Doc -> [Doc]
spine = \case
  DEmpty -> []
  Cat a b -> spine a <> spine b
  d -> [d]

-- | 'spine', with the variants resolved the way this layout will print them.
spineAt ::
  -- | The layout of the group it is printed in.
  Layout ->
  -- | The document.
  Doc ->
  [Doc]
spineAt layout = \case
  DEmpty -> []
  Cat a b -> spineAt layout a <> spineAt layout b
  DVariant flatD brokenD ->
    spineAt layout (case layout of Broken -> brokenD; _ -> flatD)
  d -> [d]

-- | The lines of the input a document holds something printed from, as far
-- down as there is anything.
printedFrom :: Doc -> [(Int, Int)]
printedFrom = \case
  DLocated s x -> (spanStartLine s, spanEndLine s) : printedFrom x
  DFence s x -> (spanStartLine s, spanEndLine s) : printedFrom x
  d@(DCppChoice cs _ _) ->
    mapMaybe conditionalRange cs <> foldChildren printedFrom d
  d -> foldChildren printedFrom d

-- | Nothing but the whitespace that separates one thing from the next.
onlySpacing :: Doc -> Bool
onlySpacing d = d == DSpace || onlyBreaks d

-- | The texts a document may print first, or last, counting each layout of
-- a variant and each branch of a choice, with a space or a break as @\" \"@
-- and an empty text for printing nothing.
edgeTexts ::
  -- | The edge: the opening one for the first text, the closing one for the
  -- last.
  BracketSide ->
  -- | The document.
  Doc ->
  [Text]
edgeTexts side = \case
  DText t -> [t]
  DSpace -> [" "]
  DBreak -> [" "]
  DCat a b -> inOrder a b
  DIndented _ a b | not (namesOnly a) -> inOrder a b
  DVariant flatD brokenD -> go flatD <> go brokenD
  DCppChoice _ bs e -> foldMap (go . snd) bs <> go e
  d -> maybe [""] (go . snd) (unwrap d)
  where
    go = edgeTexts side
    inOrder a b = case side of
      OpeningBracket -> past (go a) (go b)
      ClosingBracket -> past (go b) (go a)
    past xs ys = filter (not . T.null) xs <> if any T.null xs then ys else []

-- | Nothing but what ends a line, in a broken group at least.
onlyBreaks :: Doc -> Bool
onlyBreaks = \case
  DEmpty -> True
  DBreak -> True
  DBreak' -> True
  DNewline -> True
  DDeclDelimiter _ -> True
  _ -> False