@@ -22,7 +22,7 @@ import System.IO
2222import Control.Monad.Trans.State.Strict
2323import Control.Monad.Trans.Error
2424
25−import Data.List
25+import Data.List hiding (group)
2626import Data.Char
2727import qualified Data.Map as M
2828import qualified Data.Text as T
@@ -83,6 +83,10 @@ data LanguageExt = TypeProviders | ErrorReflection deriving (Show, Eq, Read, Ord
8383-- | The output mode in use
8484data OutputMode = RawOutput | IdeSlave Integer deriving Show
8585
86+-- | How wide is the console?
87+data ConsoleWidth = InfinitelyWide -- ^ Have pretty-printer assume that lines should not be broken
88+ | ColsWide Int -- ^ Must be positive
89+
8690-- TODO: Add 'module data' to IState, which can be saved out and reloaded quickly (i.e
8791-- without typechecking).
8892-- This will include all the functions and data declarations, plus fixity declarations
@@ -144,8 +148,8 @@ data IState = IState {
144148 idris_errorhandlers :: [Name], -- ^ Global error handlers
145149 idris_nameIdx :: (Int, Ctxt (Int, Name)),
146150 idris_function_errorhandlers :: Ctxt (M.Map Name (S.Set Name)), -- ^ Specific error handlers
147− module_aliases :: M.Map [T.Text] [T.Text]
148−
151+ module_aliases :: M.Map [T.Text] [T.Text],
152+ idris_consolewidth :: ConsoleWidth -- ^ How many chars wide is the console?
149153 }
150154
151155data SizeChange = Smaller | Same | Bigger | Unknown
@@ -215,6 +219,7 @@ idrisInit = IState initContext [] [] emptyContext emptyContext emptyContext
215219 [] [] defaultOpts 6 [] [] [] [] [] [] [] [] [] [] [] [] []
216220 [] Nothing Nothing [] [] [] Hidden False [] Nothing [] [] RawOutput
217221 True defaultTheme stdout [] (0, emptyContext) emptyContext M.empty
222+ (ColsWide 80)
218223
219224-- | The monad for the main REPL - reading and processing files and updating
220225-- global state (hence the IO inner monad).
@@ -691,7 +696,6 @@ data PTactic' t = Intro [Name] | Intros | Focus Name
691696deriving instance Binary PTactic'
692697deriving instance NFData PTactic'
693698!-}
694−
695699instance Sized a => Sized (PTactic' a) where
696700 size (Intro nms) = 1 + size nms
697701 size Intros = 1
@@ -1010,17 +1014,20 @@ piBindp p ((n, ty):ns) t = PPi p n ty (piBindp p ns t)
10101014
10111015-- Pretty-printing declarations and terms
10121016
1017+-- These "show" instances render to an absurdly wide screen because inserted line breaks
1018+-- could interfere with interactive editing, which calls "show".
1019+
10131020instance Show PTerm where
1014− showsPrec _ tm = (displayS . renderCompact . prettyImp False) tm
1021+ showsPrec _ tm = (displayS . renderPretty 1.0 10000000 . prettyImp False) tm
10151022
10161023instance Show PDecl where
1017− showsPrec _ d = (displayS . renderCompact . showDeclImp False) d
1024+ showsPrec _ d = (displayS . renderPretty 1.0 10000000 . showDeclImp False) d
10181025
10191026instance Show PClause where
1020− showsPrec _ c = (displayS . renderCompact . showCImp True) c
1027+ showsPrec _ c = (displayS . renderPretty 1.0 10000000 . showCImp True) c
10211028
10221029instance Show PData where
1023− showsPrec _ d = (displayS . renderCompact . showDImp False) d
1030+ showsPrec _ d = (displayS . renderPretty 1.0 10000000 . showDImp False) d
10241031
10251032instance Pretty PTerm OutputAnnotation where
10261033 pretty = prettyImp False
@@ -1060,9 +1067,6 @@ pprintPTerm impl bnd = prettySe 10 bnd
10601067 where
10611068 prettySe :: Int -> [(Name, Bool)] -> PTerm -> Doc OutputAnnotation
10621069 prettySe p bnd (PQuote r) =
1063− if size r > breakingSize then
1064− text "![" <> line <> pretty r <> text "]"
1065− else
10661070 text "![" <> pretty r <> text "]"
10671071 prettySe p bnd (PPatvar fc n) = pretty n
10681072 prettySe p bnd e
@@ -1070,35 +1074,23 @@ pprintPTerm impl bnd = prettySe 10 bnd
10701074 | Just n <- snat p e = annotate AnnConstData (text (show n))
10711075 prettySe p bnd (PRef fc n) = prettyName impl bnd n
10721076 prettySe p bnd (PLam n ty sc) =
1073− bracket p 2 $
1074− if size sc > breakingSize then
1075− text "\\" <> bindingOf n False <+> text "=>" <> line <> prettySe 10 ((n, False):bnd) sc
1076− else
1077− text "\\" <> bindingOf n False <+> text "=>" <+> prettySe 10 ((n, False):bnd) sc
1077+ bracket p 2 . group . hang 2 $
1078+ text "\\" <> bindingOf n False <+> text "=>" <$>
1079+ prettySe 10 ((n, False):bnd) sc
10781080 prettySe p bnd (PLet n ty v sc) =
10791081 bracket p 2 $
1080− if size sc > breakingSize then
1081− text "let" <+> bindingOf n False <+> text "=" <+> prettySe 10 bnd v <+> text "in" <>
1082− nest nestingSize (prettySe 10 ((n, False):bnd) sc)
1083− else
1084− text "let" <+> bindingOf n False <+> text "=" <+> prettySe 10 bnd v <+> text "in" <+>
1085− prettySe 10 ((n, False):bnd) sc
1082+ text "let" <+> bindingOf n False <+> text "=" </>
1083+ prettySe 10 bnd v <+> text "in" </>
1084+ prettySe 10 ((n, False):bnd) sc
10861085 prettySe p bnd (PPi (Exp l s _ _) n ty sc)
10871086 | n `elem` allNamesIn sc || impl =
10881087 let open = if Lazy `elem` l then text "|" <> lparen else lparen in
1089− bracket p 2 $
1090− if size sc > breakingSize then
1091− enclose open rparen (bindingOf n False <+> colon <+> prettySe 10 bnd ty) <+>
1092− st <> text "->" <> line <> prettySe 10 ((n, False):bnd) sc
1093− else
1094− enclose open rparen (bindingOf n False <+> colon <+> prettySe 10 bnd ty) <+>
1095− st <> text "->" <+> prettySe 10 ((n, False):bnd) sc
1088+ bracket p 2 . group $
1089+ enclose open rparen (group . align $ bindingOf n False <+> colon <+> prettySe 10 bnd ty) <+>
1090+ st <> text "->" <$> prettySe 10 ((n, False):bnd) sc
10961091 | otherwise =
1097− bracket p 2 $
1098− if size sc > breakingSize then
1099− prettySe 0 bnd ty <+> st <> text "->" <> line <> prettySe 10 ((n, False):bnd) sc
1100− else
1101− prettySe 0 bnd ty <+> st <> text "->" <+> prettySe 10 ((n, False):bnd) sc
1092+ bracket p 2 . group $
1093+ group (prettySe 0 bnd ty <+> st) <> text "->" <$> group (prettySe 10 ((n, False):bnd) sc)
11021094 where
11031095 st =
11041096 case s of
@@ -1108,12 +1100,8 @@ pprintPTerm impl bnd = prettySe 10 bnd
11081100 | impl =
11091101 let open = if Lazy `elem` l then text "|" <> lbrace else lbrace in
11101102 bracket p 2 $
1111− if size sc > breakingSize then
1112− open <> bindingOf n True <+> colon <+> prettySe 10 bnd ty <> rbrace <+>
1113− st <> text "->" <+> prettySe 10 ((n, True):bnd) sc
1114− else
1115− open <> bindingOf n True <+> colon <+> prettySe 10 bnd ty <> rbrace <+>
1116− st <> text "->" <+> prettySe 10 ((n, True):bnd) sc
1103+ open <> bindingOf n True <+> colon <+> prettySe 10 bnd ty <> rbrace <+>
1104+ st <> text "->" </> prettySe 10 ((n, True):bnd) sc
11171105 | otherwise = prettySe 10 ((n, True):bnd) sc
11181106 where
11191107 st =
@@ -1122,18 +1110,11 @@ pprintPTerm impl bnd = prettySe 10 bnd
11221110 _ -> empty
11231111 prettySe p bnd (PPi (Constraint _ _ _) n ty sc) =
11241112 bracket p 2 $
1125− if size sc > breakingSize then
1126− prettySe 10 bnd ty <+> text "=>" <+> prettySe 10 ((n, True):bnd) sc
1127− else
1128− prettySe 10 bnd ty <+> text "=>" <> line <> prettySe 10 ((n, True):bnd) sc
1113+ prettySe 10 bnd ty <+> text "=>" </> prettySe 10 ((n, True):bnd) sc
11291114 prettySe p bnd (PPi (TacImp _ _ s _) n ty sc) =
11301115 bracket p 2 $
1131− if size sc > breakingSize then
1132− lbrace <> text "tacimp" <+> bindingOf n True <+> colon <+> prettySe 10 bnd ty <>
1133− rbrace <+> text "->" <> line <> prettySe 10 ((n, True):bnd) sc
1134− else
1135− lbrace <> text "tacimp" <+> pretty n <+> colon <+> prettySe 10 bnd ty <>
1136− rbrace <+> text "->" <+> prettySe 10 ((n, True):bnd) sc
1116+ lbrace <> text "tacimp" <+> pretty n <+> colon <+> prettySe 10 bnd ty <>
1117+ rbrace <+> text "->" </> prettySe 10 ((n, True):bnd) sc
11371118 prettySe p bnd (PApp _ (PRef _ f) [])
11381119 | not impl = prettyName impl bnd f
11391120 prettySe p bnd (PAppBind _ (PRef _ f) [])
@@ -1143,11 +1124,8 @@ pprintPTerm impl bnd = prettySe 10 bnd
11431124 , not (tnull nm) &&
11441125 length (getExps args) == 2 && (not impl) && (not $ isAlpha (thead nm)) =
11451126 let [l, r] = getExps args in
1146− bracket p 1 $
1147− if size r > breakingSize then
1148− prettySe 1 bnd l <+> prettyName impl bnd op <> line <> prettySe 0 bnd r
1149− else
1150− prettySe 1 bnd l <+> prettyName impl bnd op <+> prettySe 0 bnd r
1127+ bracket p 1 . align . group $
1128+ (prettySe 1 bnd l <+> prettyName impl bnd op) <$> group (prettySe 0 bnd r)
11511129 prettySe p bnd (PApp _ hd@(PRef fc f) [tm])
11521130 | PConstant (Idris.Core.TT.Str str) <- getTm tm,
11531131 f == sUN "Symbol_" = char '\'' <> prettySe 10 bnd (PRef fc (sUN str))
@@ -1155,10 +1133,14 @@ pprintPTerm impl bnd = prettySe 10 bnd
11551133 let args = getExps as
11561134 fp = prettySe 1 bnd f
11571135 in
1158− bracket p 1 $
1159− hsep (if impl
1160− then fp : map (prettyArgS bnd) as
1161− else fp : map (prettyArgSe bnd) args)
1136+ bracket p 1 . group $
1137+ if impl
1138+ then if null as
1139+ then fp
1140+ else fp <+> align (vsep (map (prettyArgS bnd) as))
1141+ else if null args
1142+ then fp
1143+ else fp <+> align (vsep (map (prettyArgSe bnd) args))
11621144 prettySe p bnd (PCase _ scr opts) =
11631145 text "case" <+> prettySe 10 bnd scr <+> text "of" <> prettyBody
11641146 where
@@ -1171,32 +1153,18 @@ pprintPTerm impl bnd = prettySe 10 bnd
11711153 prettySe p bnd (PTrue _) = annotate (AnnName unitTy Nothing Nothing) $ text "()"
11721154 prettySe p bnd (PFalse _) = annotate (AnnName falseTy Nothing Nothing) $ text "_|_"
11731155 prettySe p bnd (PEq _ l r) =
1174− bracket p 2 $
1175− if size r > breakingSize then
1176− prettySe 10 bnd l <+> eq <$> nest nestingSize (prettySe 10 bnd r)
1177− else
1178− prettySe 10 bnd l <+> eq <+> prettySe 10 bnd r
1156+ bracket p 2 . align . group $
1157+ prettySe 10 bnd l <+> eq <$> group (prettySe 10 bnd r)
11791158 where eq = annotate (AnnName eqTy Nothing Nothing) (text "=")
11801159 prettySe p bnd (PRewrite _ l r _) =
11811160 bracket p 2 $
1182− if size r > breakingSize then
1183− text "rewrite" <+> prettySe 10 bnd l <+> text "in" <> nest nestingSize (prettySe 10 bnd r)
1184− else
1185− text "rewrite" <+> prettySe 10 bnd l <+> text "in" <+> prettySe 10 bnd r
1161+ text "rewrite" <+> prettySe 10 bnd l <+> text "in" <+> prettySe 10 bnd r
11861162 prettySe p bnd (PTyped l r) =
11871163 lparen <> prettySe 10 bnd l <+> colon <+> prettySe 10 bnd r <> rparen
11881164 prettySe p bnd (PPair _ l r) =
1189− if size r > breakingSize then
1190− lparen <> prettySe 10 bnd l <> text "," <>
1191− line <> prettySe 10 bnd r <> rparen
1192− else
1193− lparen <> prettySe 10 bnd l <> text "," <+> prettySe 10 bnd r <> rparen
1165+ lparen <> prettySe 10 bnd l <> text "," <+> prettySe 10 bnd r <> rparen
11941166 prettySe p bnd (PDPair _ l t r) =
1195− if size r > breakingSize then
1196− lparen <> prettySe 10 bnd l <+> text "**" <>
1197− line <> prettySe 10 bnd r <> rparen
1198− else
1199− lparen <> prettySe 10 bnd l <+> text "**" <+> prettySe 10 bnd r <> rparen
1167+ lparen <> prettySe 10 bnd l <+> text "**" <+> prettySe 10 bnd r <> rparen
12001168 prettySe p bnd (PAlternative a as) =
12011169 lparen <> text "|" <> prettyAs <> text "|" <> rparen
12021170 where