-- | Render a doc that the server has evaluated (a 'Doc', the structure Unison Share and the codebase server return) for
-- the console. This is @syntax.docFormatConsole@ in "Unison.Runtime.IOSource", for the evaluated structure, together
-- with how each special form shows up on the console.
module Unison.Server.Doc.Console
  ( docToConsole,
  )
where

import Data.List qualified as List
import Data.Sequence qualified as Seq
import Data.Text qualified as Text
import Unison.Codebase.Editor.DisplayObject (DisplayObject (..))
import Unison.HashQualified qualified as HQ
import Unison.Prelude
import Unison.Reference qualified as Reference
import Unison.Referent qualified as Referent
import Unison.Server.Doc
import Unison.Server.Syntax qualified as Server
import Unison.Syntax.Name qualified as Name
import Unison.Util.AnnotatedText (AnnotatedText (..), Segment (..))
import Unison.Util.ColorText (ColorText)
import Unison.Util.ColorText qualified as CT
import Unison.Util.Pretty qualified as P
import Unison.Util.SyntaxText qualified as ST

type Pretty = P.Pretty ColorText

docToConsole :: Doc -> Pretty
docToConsole :: Doc -> Pretty ColorText
docToConsole = Doc -> Pretty ColorText
go
  where
    go :: Doc -> Pretty
    go :: Doc -> Pretty ColorText
go = \case
      Word Text
t -> Text -> Pretty ColorText
lit Text
t
      Code Doc
d -> Pretty ColorText -> Pretty ColorText
forall s. Pretty s -> Pretty s
P.group (Text -> Pretty ColorText
lit Text
"`" Pretty ColorText -> Pretty ColorText -> Pretty ColorText
forall a. Semigroup a => a -> a -> a
<> Doc -> Pretty ColorText
go Doc
d Pretty ColorText -> Pretty ColorText -> Pretty ColorText
forall a. Semigroup a => a -> a -> a
<> Text -> Pretty ColorText
lit Text
"`")
      CodeBlock Text
typ Doc
d -> Pretty ColorText -> Pretty ColorText
forall s. Pretty s -> Pretty s
P.group (Text -> Pretty ColorText
lit Text
"``` " Pretty ColorText -> Pretty ColorText -> Pretty ColorText
forall a. Semigroup a => a -> a -> a
<> Pretty ColorText -> Pretty ColorText
forall s. Pretty s -> Pretty s
P.group (Text -> Pretty ColorText
lit Text
typ) Pretty ColorText -> Pretty ColorText -> Pretty ColorText
forall a. Semigroup a => a -> a -> a
<> Pretty ColorText
nl Pretty ColorText -> Pretty ColorText -> Pretty ColorText
forall a. Semigroup a => a -> a -> a
<> Doc -> Pretty ColorText
go Doc
d Pretty ColorText -> Pretty ColorText -> Pretty ColorText
forall a. Semigroup a => a -> a -> a
<> Pretty ColorText
nl Pretty ColorText -> Pretty ColorText -> Pretty ColorText
forall a. Semigroup a => a -> a -> a
<> Text -> Pretty ColorText
lit Text
"```")
      Italic Doc
d -> Text -> Doc -> Pretty ColorText
emphasized Text
"*" Doc
d
      Strikethrough Doc
d -> Text -> Doc -> Pretty ColorText
emphasized Text
"~~" Doc
d
      Bold Doc
d -> Pretty ColorText -> Pretty ColorText
P.bold (Doc -> Pretty ColorText
go Doc
d)
      Style Text
_ Doc
d -> Doc -> Pretty ColorText
go Doc
d
      Anchor Text
_ Doc
d -> Doc -> Pretty ColorText
go Doc
d
      Blockquote Doc
d -> Pretty ColorText -> Pretty ColorText
forall s. Pretty s -> Pretty s
P.group (Pretty ColorText -> Pretty ColorText -> Pretty ColorText
indent (Text -> Pretty ColorText
lit Text
"> ") (Doc -> Pretty ColorText
go Doc
d))
      Doc
Blankline -> Pretty ColorText -> Pretty ColorText
forall s. Pretty s -> Pretty s
P.group (Text -> Pretty ColorText
lit Text
"\n\n")
      Doc
Linebreak -> Pretty ColorText -> Pretty ColorText
forall s. Pretty s -> Pretty s
P.group (Text -> Pretty ColorText
lit Text
"\n")
      Doc
SectionBreak -> Text -> Pretty ColorText
lit Text
"܍"
      Tooltip Doc
inner Doc
_ -> Doc -> Pretty ColorText
go Doc
inner
      Aside Doc
d -> Pretty ColorText -> Pretty ColorText
P.hiBlack (Text -> Pretty ColorText
lit Text
"(" Pretty ColorText -> Pretty ColorText -> Pretty ColorText
forall a. Semigroup a => a -> a -> a
<> Doc -> Pretty ColorText
go Doc
d Pretty ColorText -> Pretty ColorText -> Pretty ColorText
forall a. Semigroup a => a -> a -> a
<> Text -> Pretty ColorText
lit Text
")")
      Callout Maybe Doc
Nothing Doc
d -> Pretty ColorText -> Pretty ColorText
forall s. Pretty s -> Pretty s
P.group (Pretty ColorText -> Pretty ColorText -> Pretty ColorText
indent (Text -> Pretty ColorText
lit Text
"  | ") (Doc -> Pretty ColorText
go Doc
d))
      Callout (Just Doc
icon) Doc
d -> Pretty ColorText -> Pretty ColorText
forall s. Pretty s -> Pretty s
P.group (Pretty ColorText -> Pretty ColorText -> Pretty ColorText
indent (Text -> Pretty ColorText
lit Text
"  | ") (Pretty ColorText -> [Pretty ColorText] -> Pretty ColorText
forall (f :: * -> *) s.
(Foldable f, IsString s) =>
Pretty s -> f (Pretty s) -> Pretty s
P.sep Pretty ColorText
nl [Pretty ColorText -> Pretty ColorText
P.bold (Doc -> Pretty ColorText
go Doc
icon), Text -> Pretty ColorText
lit Text
"", Doc -> Pretty ColorText
go Doc
d]))
      Table [[Doc]]
rows -> Pretty ColorText -> Pretty ColorText
forall s. Pretty s -> Pretty s
P.group ([[Pretty ColorText]] -> Pretty ColorText
forall s. (IsString s, ListLike s Char) => [[Pretty s]] -> Pretty s
P.table (([Doc] -> [Pretty ColorText]) -> [[Doc]] -> [[Pretty ColorText]]
forall a b. (a -> b) -> [a] -> [b]
map ((Doc -> Pretty ColorText) -> [Doc] -> [Pretty ColorText]
forall a b. (a -> b) -> [a] -> [b]
map Doc -> Pretty ColorText
go) [[Doc]]
rows))
      Folded Bool
_ Doc
summary Doc
details -> Doc -> Pretty ColorText
go Doc
summary Pretty ColorText -> Pretty ColorText -> Pretty ColorText
forall a. Semigroup a => a -> a -> a
<> Doc -> Pretty ColorText
go Doc
details
      Paragraph [Doc]
ds -> Pretty ColorText -> [Pretty ColorText] -> Pretty ColorText
forall (f :: * -> *) s.
(Foldable f, IsString s) =>
Pretty s -> f (Pretty s) -> Pretty s
P.sep Pretty ColorText
nl ((Either Doc [Doc] -> Pretty ColorText)
-> [Either Doc [Doc]] -> [Pretty ColorText]
forall a b. (a -> b) -> [a] -> [b]
map Either Doc [Doc] -> Pretty ColorText
paragraphPiece ([Doc] -> [Either Doc [Doc]]
paragraphRuns [Doc]
ds))
      BulletedList [Doc]
ds -> Pretty ColorText -> Pretty ColorText
forall s. Pretty s -> Pretty s
P.group (Pretty ColorText -> [Pretty ColorText] -> Pretty ColorText
forall (f :: * -> *) s.
(Foldable f, IsString s) =>
Pretty s -> f (Pretty s) -> Pretty s
P.sep Pretty ColorText
nl [Pretty ColorText
-> Pretty ColorText -> Pretty ColorText -> Pretty ColorText
indent' (Text -> Pretty ColorText
lit Text
"* ") (Text -> Pretty ColorText
lit Text
"  ") (Doc -> Pretty ColorText
go Doc
d) | Doc
d <- [Doc]
ds])
      NumberedList Nat
n [Doc]
ds ->
        let dot :: Text
dot = Text
". "
            w :: Int
w = Text -> Int
Text.length (Nat -> Text
forall a. Show a => a -> Text
tShow (Nat
n Nat -> Nat -> Nat
forall a. Num a => a -> a -> a
+ Int -> Nat
forall a b. (Integral a, Num b) => a -> b
fromIntegral ([Doc] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [Doc]
ds))) Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Text -> Int
Text.length Text
dot
            num :: Nat -> Pretty ColorText
num Nat
k = Text -> Pretty ColorText
lit (Int -> Char -> Text -> Text
Text.justifyRight Int
w Char
' ' (Nat -> Text
forall a. Show a => a -> Text
tShow Nat
k Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
dot))
            pad :: Pretty ColorText
pad = Text -> Pretty ColorText
lit (Int -> Text -> Text
Text.replicate Int
w Text
" ")
         in Pretty ColorText -> Pretty ColorText
forall s. Pretty s -> Pretty s
P.group (Pretty ColorText -> [Pretty ColorText] -> Pretty ColorText
forall (f :: * -> *) s.
(Foldable f, IsString s) =>
Pretty s -> f (Pretty s) -> Pretty s
P.sep Pretty ColorText
nl [Pretty ColorText
-> Pretty ColorText -> Pretty ColorText -> Pretty ColorText
indent' (Nat -> Pretty ColorText
num Nat
k) Pretty ColorText
pad (Doc -> Pretty ColorText
go Doc
d) | (Nat
k, Doc
d) <- [Nat] -> [Doc] -> [(Nat, Doc)]
forall a b. [a] -> [b] -> [(a, b)]
zip [Nat
n ..] [Doc]
ds])
      Section Doc
title [Doc]
ds ->
        let t :: Pretty ColorText
t = Pretty ColorText
-> Pretty ColorText -> Pretty ColorText -> Pretty ColorText
indent' (Text -> Pretty ColorText
lit Text
"# ") (Text -> Pretty ColorText
lit Text
"  ") (Pretty ColorText -> Pretty ColorText
forall s. Pretty s -> Pretty s
P.group (Doc -> Pretty ColorText
go (Doc -> Doc
forall specialForm. DocG specialForm -> DocG specialForm
Bold Doc
title)))
            subs :: [Pretty ColorText]
subs = [Pretty ColorText -> Pretty ColorText -> Pretty ColorText
indent (Text -> Pretty ColorText
lit Text
"  ") (Pretty ColorText -> Pretty ColorText
forall s. Pretty s -> Pretty s
P.group (Doc -> Pretty ColorText
go Doc
d)) | Doc
d <- [Doc]
ds]
         in Pretty ColorText -> Pretty ColorText
forall s. Pretty s -> Pretty s
P.group (Pretty ColorText -> [Pretty ColorText] -> Pretty ColorText
forall (f :: * -> *) s.
(Foldable f, IsString s) =>
Pretty s -> f (Pretty s) -> Pretty s
P.sep (Pretty ColorText
nl Pretty ColorText -> Pretty ColorText -> Pretty ColorText
forall a. Semigroup a => a -> a -> a
<> Pretty ColorText
nl) (Pretty ColorText
t Pretty ColorText -> [Pretty ColorText] -> [Pretty ColorText]
forall a. a -> [a] -> [a]
: [Pretty ColorText]
subs))
      UntitledSection [Doc]
ds -> Pretty ColorText -> Pretty ColorText
forall s. Pretty s -> Pretty s
P.group (Pretty ColorText -> [Pretty ColorText] -> Pretty ColorText
forall (f :: * -> *) s.
(Foldable f, IsString s) =>
Pretty s -> f (Pretty s) -> Pretty s
P.sep (Pretty ColorText
nl Pretty ColorText -> Pretty ColorText -> Pretty ColorText
forall a. Semigroup a => a -> a -> a
<> Pretty ColorText
nl) [Pretty ColorText -> Pretty ColorText
forall s. Pretty s -> Pretty s
P.group (Doc -> Pretty ColorText
go Doc
d) | Doc
d <- [Doc]
ds])
      Join [Doc]
ds -> (Doc -> Pretty ColorText) -> [Doc] -> Pretty ColorText
forall m a. Monoid m => (a -> m) -> [a] -> m
forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap Doc -> Pretty ColorText
go [Doc]
ds
      Column [Doc]
ds -> Pretty ColorText -> [Pretty ColorText] -> Pretty ColorText
forall (f :: * -> *) s.
(Foldable f, IsString s) =>
Pretty s -> f (Pretty s) -> Pretty s
P.sep Pretty ColorText
nl ((Doc -> Pretty ColorText) -> [Doc] -> [Pretty ColorText]
forall a b. (a -> b) -> [a] -> [b]
map Doc -> Pretty ColorText
go [Doc]
ds)
      Group Doc
d -> Pretty ColorText -> Pretty ColorText
forall s. Pretty s -> Pretty s
P.group (Doc -> Pretty ColorText
go Doc
d)
      NamedLink Doc
name Doc
_ -> Pretty ColorText -> Pretty ColorText
P.underline (Doc -> Pretty ColorText
go Doc
name)
      Image Doc
alt Doc
_ (Just Doc
caption) -> Pretty ColorText -> [Pretty ColorText] -> Pretty ColorText
forall (f :: * -> *) s.
(Foldable f, IsString s) =>
Pretty s -> f (Pretty s) -> Pretty s
P.sep Pretty ColorText
nl [Doc -> Pretty ColorText
go Doc
alt, Doc -> Pretty ColorText
go (Doc -> Doc
forall specialForm. DocG specialForm -> DocG specialForm
Italic Doc
caption)]
      Image Doc
alt Doc
_ Maybe Doc
Nothing -> Doc -> Pretty ColorText
go Doc
alt
      Special RenderedSpecialForm
form -> RenderedSpecialForm -> Pretty ColorText
special RenderedSpecialForm
form

    -- A code block inside a paragraph is not words: it keeps its lines, and the text before and after it wraps on its own.
    paragraphPiece :: Either Doc [Doc] -> Pretty
    paragraphPiece :: Either Doc [Doc] -> Pretty ColorText
paragraphPiece = \case
      Left Doc
block -> Doc -> Pretty ColorText
go Doc
block
      Right [Doc]
ds -> (ColorText -> [Pretty ColorText])
-> Pretty ColorText -> Pretty ColorText
forall s. IsString s => (s -> [Pretty s]) -> Pretty s -> Pretty s
P.wrap' (Pretty ColorText -> [Pretty ColorText]
forall a. a -> [a]
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Pretty ColorText -> [Pretty ColorText])
-> (ColorText -> Pretty ColorText)
-> ColorText
-> [Pretty ColorText]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ColorText -> Pretty ColorText
forall s. (IsString s, ListLike s Char) => s -> Pretty s
P.lit) ((Doc -> Pretty ColorText) -> [Doc] -> Pretty ColorText
forall m a. Monoid m => (a -> m) -> [a] -> m
forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap Doc -> Pretty ColorText
go [Doc]
ds)

    paragraphRuns :: [Doc] -> [Either Doc [Doc]]
    paragraphRuns :: [Doc] -> [Either Doc [Doc]]
paragraphRuns = \case
      [] -> []
      Doc
d : [Doc]
rest
        | Doc -> Bool
isBlock Doc
d -> Doc -> Either Doc [Doc]
forall a b. a -> Either a b
Left Doc
d Either Doc [Doc] -> [Either Doc [Doc]] -> [Either Doc [Doc]]
forall a. a -> [a] -> [a]
: [Doc] -> [Either Doc [Doc]]
paragraphRuns [Doc]
rest
        | Bool
otherwise ->
            let ([Doc]
inline, [Doc]
more) = (Doc -> Bool) -> [Doc] -> ([Doc], [Doc])
forall a. (a -> Bool) -> [a] -> ([a], [a])
List.span (Bool -> Bool
not (Bool -> Bool) -> (Doc -> Bool) -> Doc -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Doc -> Bool
isBlock) [Doc]
rest
             in [Doc] -> Either Doc [Doc]
forall a b. b -> Either a b
Right (Doc
d Doc -> [Doc] -> [Doc]
forall a. a -> [a] -> [a]
: [Doc]
inline) Either Doc [Doc] -> [Either Doc [Doc]] -> [Either Doc [Doc]]
forall a. a -> [a] -> [a]
: [Doc] -> [Either Doc [Doc]]
paragraphRuns [Doc]
more

    isBlock :: Doc -> Bool
    isBlock :: Doc -> Bool
isBlock = \case
      CodeBlock {} -> Bool
True
      Special (Eval {}) -> Bool
True
      Special (ExampleBlock {}) -> Bool
True
      Special (Signature {}) -> Bool
True
      Special (Source {}) -> Bool
True
      Special (FoldedSource {}) -> Bool
True
      Doc
_ -> Bool
False

    -- Emphasis marks go inside the first and last words of a paragraph, so they stay with them when the paragraph wraps.
    emphasized :: Text -> Doc -> Pretty ColorText
emphasized Text
mark = \case
      Paragraph (Doc
l : rest :: [Doc]
rest@(Doc
_ : [Doc]
_)) ->
        let ([Doc]
mid, Doc
r) = ([Doc] -> [Doc]
forall a. HasCallStack => [a] -> [a]
List.init [Doc]
rest, [Doc] -> Doc
forall a. HasCallStack => [a] -> a
List.last [Doc]
rest)
         in Pretty ColorText -> Pretty ColorText
forall s. Pretty s -> Pretty s
P.group (Text -> Pretty ColorText
lit Text
mark Pretty ColorText -> Pretty ColorText -> Pretty ColorText
forall a. Semigroup a => a -> a -> a
<> Doc -> Pretty ColorText
go Doc
l) Pretty ColorText -> Pretty ColorText -> Pretty ColorText
forall a. Semigroup a => a -> a -> a
<> (Doc -> Pretty ColorText) -> [Doc] -> Pretty ColorText
forall m a. Monoid m => (a -> m) -> [a] -> m
forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap Doc -> Pretty ColorText
go [Doc]
mid Pretty ColorText -> Pretty ColorText -> Pretty ColorText
forall a. Semigroup a => a -> a -> a
<> Pretty ColorText -> Pretty ColorText
forall s. Pretty s -> Pretty s
P.group (Doc -> Pretty ColorText
go Doc
r Pretty ColorText -> Pretty ColorText -> Pretty ColorText
forall a. Semigroup a => a -> a -> a
<> Text -> Pretty ColorText
lit Text
mark)
      Doc
d -> Pretty ColorText -> Pretty ColorText
forall s. Pretty s -> Pretty s
P.group (Text -> Pretty ColorText
lit Text
mark Pretty ColorText -> Pretty ColorText -> Pretty ColorText
forall a. Semigroup a => a -> a -> a
<> Doc -> Pretty ColorText
go Doc
d Pretty ColorText -> Pretty ColorText -> Pretty ColorText
forall a. Semigroup a => a -> a -> a
<> Text -> Pretty ColorText
lit Text
mark)

    special :: RenderedSpecialForm -> Pretty
    special :: RenderedSpecialForm -> Pretty ColorText
special = \case
      Source [SrcRefs]
srcs -> [SrcRefs] -> Pretty ColorText
forall {a}.
[Ref (a, DisplayObject SyntaxText Src)] -> Pretty ColorText
sources [SrcRefs]
srcs
      FoldedSource [SrcRefs]
srcs -> [SrcRefs] -> Pretty ColorText
forall {a}.
[Ref (a, DisplayObject SyntaxText Src)] -> Pretty ColorText
sources [SrcRefs]
srcs
      Example SyntaxText
code -> Pretty ColorText -> Pretty ColorText
forall s. IsString s => Pretty s -> Pretty s
P.backticked (SyntaxText -> Pretty ColorText
syntax SyntaxText
code)
      ExampleBlock SyntaxText
code -> Width -> Pretty ColorText -> Pretty ColorText
forall s.
(ListLike s Char, IsString s) =>
Width -> Pretty s -> Pretty s
P.indentN Width
4 (SyntaxText -> Pretty ColorText
syntax SyntaxText
code)
      Link SyntaxText
code -> Pretty ColorText -> Pretty ColorText
P.underline (SyntaxText -> Pretty ColorText
syntax SyntaxText
code)
      Signature [SyntaxText]
sigs -> Pretty ColorText -> Pretty ColorText
forall s. Pretty s -> Pretty s
P.group (Pretty ColorText -> [Pretty ColorText] -> Pretty ColorText
forall (f :: * -> *) s.
(Foldable f, IsString s) =>
Pretty s -> f (Pretty s) -> Pretty s
P.sep Pretty ColorText
"\n\n" [Width -> Pretty ColorText -> Pretty ColorText
forall s.
(ListLike s Char, IsString s) =>
Width -> Pretty s -> Pretty s
P.indentN Width
4 (SyntaxText -> Pretty ColorText
syntax SyntaxText
s) | SyntaxText
s <- [SyntaxText]
sigs])
      SignatureInline SyntaxText
sig -> Pretty ColorText -> Pretty ColorText
forall s. IsString s => Pretty s -> Pretty s
P.backticked (SyntaxText -> Pretty ColorText
syntax SyntaxText
sig)
      Eval SyntaxText
code SyntaxText
result -> Width -> Pretty ColorText -> Pretty ColorText
forall s.
(ListLike s Char, IsString s) =>
Width -> Pretty s -> Pretty s
P.indentN Width
4 ([Pretty ColorText] -> Pretty ColorText
forall (f :: * -> *) s.
(Foldable f, IsString s) =>
f (Pretty s) -> Pretty s
P.lines [SyntaxText -> Pretty ColorText
syntax SyntaxText
code, Pretty ColorText
"⧨", Pretty ColorText -> Pretty ColorText
P.green (SyntaxText -> Pretty ColorText
syntax SyntaxText
result)])
      EvalInline SyntaxText
_ SyntaxText
result -> Pretty ColorText -> Pretty ColorText
forall s. IsString s => Pretty s -> Pretty s
P.backticked (SyntaxText -> Pretty ColorText
syntax SyntaxText
result)
      Embed SyntaxText
code -> Width -> Pretty ColorText -> Pretty ColorText
forall s.
(ListLike s Char, IsString s) =>
Width -> Pretty s -> Pretty s
P.indentN Width
2 (Pretty ColorText
nl Pretty ColorText -> Pretty ColorText -> Pretty ColorText
forall a. Semigroup a => a -> a -> a
<> Pretty ColorText
"{{ embed {{" Pretty ColorText -> Pretty ColorText -> Pretty ColorText
forall a. Semigroup a => a -> a -> a
<> SyntaxText -> Pretty ColorText
syntax SyntaxText
code Pretty ColorText -> Pretty ColorText -> Pretty ColorText
forall a. Semigroup a => a -> a -> a
<> Pretty ColorText
"}} }}" Pretty ColorText -> Pretty ColorText -> Pretty ColorText
forall a. Semigroup a => a -> a -> a
<> Pretty ColorText
nl)
      EmbedInline SyntaxText
code -> Pretty ColorText
"{{ embed {{" Pretty ColorText -> Pretty ColorText -> Pretty ColorText
forall a. Semigroup a => a -> a -> a
<> SyntaxText -> Pretty ColorText
syntax SyntaxText
code Pretty ColorText -> Pretty ColorText -> Pretty ColorText
forall a. Semigroup a => a -> a -> a
<> Pretty ColorText
"}} }}"
      Video {} -> Pretty ColorText
"(a video)"
      FrontMatter {} -> Pretty ColorText
forall a. Monoid a => a
mempty
      LaTeXInline Text
t -> Pretty ColorText -> Pretty ColorText
forall s. IsString s => Pretty s -> Pretty s
P.backticked (Text -> Pretty ColorText
lit Text
t)
      Svg {} -> Pretty ColorText
"(an image)"
      RenderError RenderError SyntaxText
_ -> Pretty ColorText -> Pretty ColorText
P.red Pretty ColorText
"🆘  This part of the doc couldn't be rendered"

    sources :: [Ref (a, DisplayObject SyntaxText Src)] -> Pretty ColorText
sources [Ref (a, DisplayObject SyntaxText Src)]
srcs = Pretty ColorText -> Pretty ColorText
forall s. Pretty s -> Pretty s
P.group (Width -> Pretty ColorText -> Pretty ColorText
forall s.
(ListLike s Char, IsString s) =>
Width -> Pretty s -> Pretty s
P.indentN Width
4 (Pretty ColorText -> [Pretty ColorText] -> Pretty ColorText
forall (f :: * -> *) s.
(Foldable f, IsString s) =>
Pretty s -> f (Pretty s) -> Pretty s
P.sep Pretty ColorText
"\n\n" ((Ref (a, DisplayObject SyntaxText Src) -> Pretty ColorText)
-> [Ref (a, DisplayObject SyntaxText Src)] -> [Pretty ColorText]
forall a b. (a -> b) -> [a] -> [b]
map Ref (a, DisplayObject SyntaxText Src) -> Pretty ColorText
forall {a}.
Ref (a, DisplayObject SyntaxText Src) -> Pretty ColorText
source [Ref (a, DisplayObject SyntaxText Src)]
srcs)))

    source :: Ref (a, DisplayObject SyntaxText Src) -> Pretty ColorText
source Ref (a, DisplayObject SyntaxText Src)
ref = case Ref (a, DisplayObject SyntaxText Src)
ref of
      Term (a
_, DisplayObject SyntaxText Src
obj) -> DisplayObject SyntaxText Src -> Pretty ColorText
displayObject DisplayObject SyntaxText Src
obj
      Type (a
_, DisplayObject SyntaxText Src
obj) -> DisplayObject SyntaxText Src -> Pretty ColorText
displayObject DisplayObject SyntaxText Src
obj

    displayObject :: DisplayObject SyntaxText Src -> Pretty ColorText
displayObject = \case
      BuiltinObject SyntaxText
code -> SyntaxText -> Pretty ColorText
syntax SyntaxText
code
      MissingObject ShortHash
hash -> Pretty ColorText -> Pretty ColorText
P.red (Pretty ColorText
"😶  Missing source for: " Pretty ColorText -> Pretty ColorText -> Pretty ColorText
forall a. Semigroup a => a -> a -> a
<> Text -> Pretty ColorText
lit (ShortHash -> Text
forall a. Show a => a -> Text
tShow ShortHash
hash))
      UserObject (Src SyntaxText
_ SyntaxText
full) -> SyntaxText -> Pretty ColorText
syntax SyntaxText
full

    lit :: Text -> Pretty
    lit :: Text -> Pretty ColorText
lit = Text -> Pretty ColorText
forall s. IsString s => Text -> Pretty s
P.text

    nl :: Pretty
    nl :: Pretty ColorText
nl = Text -> Pretty ColorText
lit Text
"\n"

    indent :: Pretty -> Pretty -> Pretty
    indent :: Pretty ColorText -> Pretty ColorText -> Pretty ColorText
indent Pretty ColorText
i = Pretty ColorText
-> Pretty ColorText -> Pretty ColorText -> Pretty ColorText
indent' Pretty ColorText
i Pretty ColorText
i

    indent' :: Pretty -> Pretty -> Pretty -> Pretty
    indent' :: Pretty ColorText
-> Pretty ColorText -> Pretty ColorText -> Pretty ColorText
indent' Pretty ColorText
initial Pretty ColorText
afterNewline Pretty ColorText
p = Pretty ColorText
initial Pretty ColorText -> Pretty ColorText -> Pretty ColorText
forall a. Semigroup a => a -> a -> a
<> Pretty ColorText -> Pretty ColorText -> Pretty ColorText
forall s.
(ListLike s Char, IsString s) =>
Pretty s -> Pretty s -> Pretty s
P.indentAfterNewline Pretty ColorText
afterNewline Pretty ColorText
p

-- | Code with its syntax highlighting, as lines (a literal can't have a newline in it).
syntax :: Server.SyntaxText -> Pretty
syntax :: SyntaxText -> Pretty ColorText
syntax (AnnotatedText Seq (Segment Element)
segments) =
  [Pretty ColorText] -> Pretty ColorText
forall (f :: * -> *) s.
(Foldable f, IsString s) =>
f (Pretty s) -> Pretty s
P.lines (([(Text, Maybe Color)] -> Pretty ColorText)
-> [[(Text, Maybe Color)]] -> [Pretty ColorText]
forall a b. (a -> b) -> [a] -> [b]
map (((Text, Maybe Color) -> Pretty ColorText)
-> [(Text, Maybe Color)] -> Pretty ColorText
forall m a. Monoid m => (a -> m) -> [a] -> m
forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap (Text, Maybe Color) -> Pretty ColorText
forall {a}. (Text, Maybe a) -> Pretty (AnnotatedText a)
piece) ([Segment Element] -> [[(Text, Maybe Color)]]
splitLines (Seq (Segment Element) -> [Segment Element]
forall a. Seq a -> [a]
forall (t :: * -> *) a. Foldable t => t a -> [a]
toList Seq (Segment Element)
segments)))
  where
    piece :: (Text, Maybe a) -> Pretty (AnnotatedText a)
piece (Text
text, Maybe a
color) = AnnotatedText a -> Pretty (AnnotatedText a)
forall s. (IsString s, ListLike s Char) => s -> Pretty s
P.lit (Seq (Segment a) -> AnnotatedText a
forall a. Seq (Segment a) -> AnnotatedText a
AnnotatedText (Segment a -> Seq (Segment a)
forall a. a -> Seq a
Seq.singleton (Text -> Maybe a -> Segment a
forall a. Text -> Maybe a -> Segment a
Segment Text
text Maybe a
color)))

    splitLines :: [Segment Server.Element] -> [[(Text, Maybe CT.Color)]]
    splitLines :: [Segment Element] -> [[(Text, Maybe Color)]]
splitLines =
      [[(Text, Maybe Color)]] -> [[(Text, Maybe Color)]]
forall a. [a] -> [a]
reverse ([[(Text, Maybe Color)]] -> [[(Text, Maybe Color)]])
-> ([Segment Element] -> [[(Text, Maybe Color)]])
-> [Segment Element]
-> [[(Text, Maybe Color)]]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ([(Text, Maybe Color)] -> [(Text, Maybe Color)])
-> [[(Text, Maybe Color)]] -> [[(Text, Maybe Color)]]
forall a b. (a -> b) -> [a] -> [b]
map [(Text, Maybe Color)] -> [(Text, Maybe Color)]
forall a. [a] -> [a]
reverse ([[(Text, Maybe Color)]] -> [[(Text, Maybe Color)]])
-> ([Segment Element] -> [[(Text, Maybe Color)]])
-> [Segment Element]
-> [[(Text, Maybe Color)]]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ([[(Text, Maybe Color)]]
 -> Segment Element -> [[(Text, Maybe Color)]])
-> [[(Text, Maybe Color)]]
-> [Segment Element]
-> [[(Text, Maybe Color)]]
forall b a. (b -> a -> b) -> b -> [a] -> b
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' [[(Text, Maybe Color)]]
-> Segment Element -> [[(Text, Maybe Color)]]
step [[]]
      where
        -- The lines so far, last first, each with its pieces last first.
        step :: [[(Text, Maybe CT.Color)]] -> Segment Server.Element -> [[(Text, Maybe CT.Color)]]
        step :: [[(Text, Maybe Color)]]
-> Segment Element -> [[(Text, Maybe Color)]]
step [[(Text, Maybe Color)]]
soFar (Segment Text
text Maybe Element
element) =
          let color :: Maybe Color
color = Maybe Element
element Maybe Element -> (Element -> Maybe Color) -> Maybe Color
forall a b. Maybe a -> (a -> Maybe b) -> Maybe b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Element Reference -> Maybe Color
forall r. Element r -> Maybe Color
CT.defaultColors (Element Reference -> Maybe Color)
-> (Element -> Element Reference) -> Element -> Maybe Color
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Element -> Element Reference
elementForColor
           in case ([[(Text, Maybe Color)]]
soFar, HasCallStack => Text -> Text -> [Text]
Text -> Text -> [Text]
Text.splitOn Text
"\n" Text
text) of
                ([(Text, Maybe Color)]
cur : [[(Text, Maybe Color)]]
done, Text
first : [Text]
rest) ->
                  ([[(Text, Maybe Color)]] -> Text -> [[(Text, Maybe Color)]])
-> [[(Text, Maybe Color)]] -> [Text] -> [[(Text, Maybe Color)]]
forall b a. (b -> a -> b) -> b -> [a] -> b
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' (\[[(Text, Maybe Color)]]
acc Text
line -> [(Text
line, Maybe Color
color)] [(Text, Maybe Color)]
-> [[(Text, Maybe Color)]] -> [[(Text, Maybe Color)]]
forall a. a -> [a] -> [a]
: [[(Text, Maybe Color)]]
acc) (((Text
first, Maybe Color
color) (Text, Maybe Color)
-> [(Text, Maybe Color)] -> [(Text, Maybe Color)]
forall a. a -> [a] -> [a]
: [(Text, Maybe Color)]
cur) [(Text, Maybe Color)]
-> [[(Text, Maybe Color)]] -> [[(Text, Maybe Color)]]
forall a. a -> [a] -> [a]
: [[(Text, Maybe Color)]]
done) [Text]
rest
                ([[(Text, Maybe Color)]], [Text])
_ -> [[(Text, Maybe Color)]]
soFar

-- | The server's syntax elements say what they are, which is all the colors need (not what they refer to).
elementForColor :: Server.Element -> ST.Element Reference.Reference
elementForColor :: Element -> Element Reference
elementForColor = \case
  Element
Server.NumericLiteral -> Element Reference
forall r. Element r
ST.NumericLiteral
  Element
Server.TextLiteral -> Element Reference
forall r. Element r
ST.TextLiteral
  Element
Server.BytesLiteral -> Element Reference
forall r. Element r
ST.BytesLiteral
  Element
Server.CharLiteral -> Element Reference
forall r. Element r
ST.CharLiteral
  Element
Server.BooleanLiteral -> Element Reference
forall r. Element r
ST.BooleanLiteral
  Element
Server.Blank -> Element Reference
forall r. Element r
ST.Blank
  Element
Server.Var -> Element Reference
forall r. Element r
ST.Var
  Server.TypeReference Maybe Text
_ Text
_ -> Maybe Name -> Reference -> Element Reference
forall r. Maybe Name -> r -> Element r
ST.TypeReference Maybe Name
forall a. Maybe a
Nothing Reference
forall {h}. Reference' Text h
dummy
  Server.DataConstructorReference Maybe Text
_ Text
_ -> Maybe Name -> Referent' Reference -> Element Reference
forall r. Maybe Name -> Referent' r -> Element r
ST.TermReference Maybe Name
forall a. Maybe a
Nothing (Reference -> Referent' Reference
Referent.Ref Reference
forall {h}. Reference' Text h
dummy)
  Server.AbilityConstructorReference Maybe Text
_ Text
_ -> Maybe Name -> Referent' Reference -> Element Reference
forall r. Maybe Name -> Referent' r -> Element r
ST.TermReference Maybe Name
forall a. Maybe a
Nothing (Reference -> Referent' Reference
Referent.Ref Reference
forall {h}. Reference' Text h
dummy)
  Server.TermReference Maybe Text
_ Text
_ -> Maybe Name -> Referent' Reference -> Element Reference
forall r. Maybe Name -> Referent' r -> Element r
ST.TermReference Maybe Name
forall a. Maybe a
Nothing (Reference -> Referent' Reference
Referent.Ref Reference
forall {h}. Reference' Text h
dummy)
  Server.Op SeqOp
op -> SeqOp -> Element Reference
forall r. SeqOp -> Element r
ST.Op SeqOp
op
  Element
Server.AbilityBraces -> Element Reference
forall r. Element r
ST.AbilityBraces
  Element
Server.ControlKeyword -> Element Reference
forall r. Element r
ST.ControlKeyword
  Element
Server.TypeOperator -> Element Reference
forall r. Element r
ST.TypeOperator
  Element
Server.BindingEquals -> Element Reference
forall r. Element r
ST.BindingEquals
  Element
Server.TypeAscriptionColon -> Element Reference
forall r. Element r
ST.TypeAscriptionColon
  Element
Server.DataTypeKeyword -> Element Reference
forall r. Element r
ST.DataTypeKeyword
  Element
Server.DataTypeParams -> Element Reference
forall r. Element r
ST.DataTypeParams
  Element
Server.Unit -> Element Reference
forall r. Element r
ST.Unit
  Element
Server.DataTypeModifier -> Element Reference
forall r. Element r
ST.DataTypeModifier
  Element
Server.UseKeyword -> Element Reference
forall r. Element r
ST.UseKeyword
  Element
Server.UsePrefix -> Element Reference
forall r. Element r
ST.UsePrefix
  Element
Server.UseSuffix -> Element Reference
forall r. Element r
ST.UseSuffix
  Server.HashQualifier Text
_ -> HashQualified Name -> Element Reference
forall r. HashQualified Name -> Element r
ST.HashQualifier (Name -> HashQualified Name
forall n. n -> HashQualified n
HQ.NameOnly (HasCallStack => Text -> Name
Text -> Name
Name.unsafeParseText Text
"x"))
  Element
Server.DelayForceChar -> Element Reference
forall r. Element r
ST.DelayForceChar
  Element
Server.DelimiterChar -> Element Reference
forall r. Element r
ST.DelimiterChar
  Element
Server.Parenthesis -> Element Reference
forall r. Element r
ST.Parenthesis
  Element
Server.LinkKeyword -> Element Reference
forall r. Element r
ST.LinkKeyword
  Element
Server.DocDelimiter -> Element Reference
forall r. Element r
ST.DocDelimiter
  Element
Server.DocKeyword -> Element Reference
forall r. Element r
ST.DocKeyword
  Server.RecordFieldName Text
name -> Text -> Element Reference
forall r. Text -> Element r
ST.RecordFieldName Text
name
  Element
Server.RecordFieldValueColon -> Element Reference
forall r. Element r
ST.RecordFieldValueColon
  Element
Server.RecordExtraFields -> Element Reference
forall r. Element r
ST.RecordExtraFields
  where
    dummy :: Reference' Text h
dummy = Text -> Reference' Text h
forall t h. t -> Reference' t h
Reference.Builtin Text
""