<commit_before>{-# LANGUAGE OverloadedStrings   #-}
{-# LANGUAGE PatternSynonyms     #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE FlexibleContexts    #-}

module Unison.TypePrinter where

import qualified Data.Map              as Map
import           Data.Maybe            (isJust)
import           Data.String           (fromString)
import           Unison.HashQualified  (HashQualified)
import           Unison.NamePrinter    ( prettyHashQualified
                                       , prettyHashQualified0)
import           Unison.PrettyPrintEnv (PrettyPrintEnv, Imports, elideFQN)
import qualified Unison.PrettyPrintEnv as PrettyPrintEnv
import           Unison.Reference      (pattern Builtin)
import qualified Unison.SyntaxHighlights as S
import           Unison.SyntaxHighlights ( fmt )
import           Unison.Type
import           Unison.Util.Pretty    (ColorText, Pretty)
import           Unison.Util.ColorText (toPlain)
import qualified Unison.Util.Pretty    as PP
import           Unison.Var            (Var)
import qualified Unison.Var            as Var
import qualified Unison.DataDeclaration as DD

{- Explanation of precedence handling

   We illustrate precedence rules as follows.

     >=10
       10f 10x

   This example shows that a type application f x is enclosed in parentheses
   whenever the ambient precedence around it is >= 10, and that when printing
   its two components, an ambient precedence of 10 is used in both places.

   The pretty-printer uses the following rules for printing types.

     >=10
       10f 10x
       { 0e } 10t

     >=0
       0a -> 0b

-}

prettyTop
  :: forall s v a . (IsString s, LL.ListLike s Char, Var v)
  => PrettyPrintEnv -> AnnotatedType v a -> Pretty s
prettyTop ppe ty = pretty ppe mempty (-1) ty

pretty
  :: forall v a . (Var v)
  => PrettyPrintEnv
  -> Imports
  -> Int
  -> AnnotatedType v a
  -> Pretty ColorText
pretty n im p tp = pretty0 n im p (cleanup (removePureEffects tp))

pretty0
  :: forall v a . (Var v)
  => PrettyPrintEnv
  -> Imports
  -> Int
  -> AnnotatedType v a
  -> Pretty ColorText
-- p is the operator precedence of the enclosing context (a number from 0 to
-- 11, or -1 to avoid outer parentheses unconditionally).  Function
-- application has precedence 10.
pretty0 n im p tp = go n im p tp
  where
  go :: PrettyPrintEnv -> Imports -> Int -> AnnotatedType v a -> Pretty ColorText<commit_msg>#287 pretty-print in color - fix after rebase onto branchless<commit_after>{-# LANGUAGE OverloadedStrings   #-}
{-# LANGUAGE PatternSynonyms     #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE FlexibleContexts    #-}

module Unison.TypePrinter where

import qualified Data.Map              as Map
import           Data.Maybe            (isJust)
import           Data.String           (fromString)
import           Unison.HashQualified  (HashQualified)
import           Unison.NamePrinter    ( prettyHashQualified
                                       , prettyHashQualified0)
import           Unison.PrettyPrintEnv (PrettyPrintEnv, Imports, elideFQN)
import qualified Unison.PrettyPrintEnv as PrettyPrintEnv
import           Unison.Reference      (pattern Builtin)
import qualified Unison.SyntaxHighlights as S
import           Unison.SyntaxHighlights ( fmt )
import           Unison.Type
import           Unison.Util.Pretty    (ColorText, Pretty)
import           Unison.Util.ColorText (toPlain)
import qualified Unison.Util.Pretty    as PP
import           Unison.Var            (Var)
import qualified Unison.Var            as Var
import qualified Unison.DataDeclaration as DD

{- Explanation of precedence handling

   We illustrate precedence rules as follows.

     >=10
       10f 10x

   This example shows that a type application f x is enclosed in parentheses
   whenever the ambient precedence around it is >= 10, and that when printing
   its two components, an ambient precedence of 10 is used in both places.

   The pretty-printer uses the following rules for printing types.

     >=10
       10f 10x
       { 0e } 10t

     >=0
       0a -> 0b

-}

prettyTop
  :: forall v a . (Var v)
  => PrettyPrintEnv -> AnnotatedType v a -> Pretty ColorText
prettyTop ppe ty = pretty ppe mempty (-1) ty

pretty
  :: forall v a . (Var v)
  => PrettyPrintEnv
  -> Imports
  -> Int
  -> AnnotatedType v a
  -> Pretty ColorText
pretty n im p tp = pretty0 n im p (cleanup (removePureEffects tp))

pretty0
  :: forall v a . (Var v)
  => PrettyPrintEnv
  -> Imports
  -> Int
  -> AnnotatedType v a
  -> Pretty ColorText
-- p is the operator precedence of the enclosing context (a number from 0 to
-- 11, or -1 to avoid outer parentheses unconditionally).  Function
-- application has precedence 10.
pretty0 n im p tp = go n im p tp
  where
  go :: PrettyPrintEnv -> Imports -> Int -> AnnotatedType v a -> Pretty ColorText
{'fraction_non_alphanumeric': 0.07045171985080813, 'fraction_numerical': 0.014297554910899296, 'mean_word_length': 2.420978029766123, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 47, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1450096', 'n_tokens_mistral': 1474, 'n_tokens_neox': 1367, 'n_words': 521}