@@ -22,7 +22,7 @@ import System.IO
22import Control.Monad.Trans.State.Strict
22import Control.Monad.Trans.State.Strict
23import Control.Monad.Trans.Error
23import Control.Monad.Trans.Error
25−import Data.List
25+import Data.List hiding (group)
26import Data.Char
26import Data.Char
27import qualified Data.Map as M
27import qualified Data.Map as M
28import qualified Data.Text as T
28import qualified Data.Text as T
@@ -83,6 +83,10 @@ data LanguageExt = TypeProviders | ErrorReflection deriving (Show, Eq, Read, Ord
83-- | The output mode in use
83-- | The output mode in use
84data OutputMode = RawOutput | IdeSlave Integer deriving Show
84data OutputMode = RawOutput | IdeSlave Integer deriving Show
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
86-- TODO: Add 'module data' to IState, which can be saved out and reloaded quickly (i.e
90-- TODO: Add 'module data' to IState, which can be saved out and reloaded quickly (i.e
87-- without typechecking).
91-- without typechecking).
88-- This will include all the functions and data declarations, plus fixity declarations
92-- This will include all the functions and data declarations, plus fixity declarations
@@ -144,8 +148,8 @@ data IState = IState {
144 idris_errorhandlers :: [Name], -- ^ Global error handlers
148 idris_errorhandlers :: [Name], -- ^ Global error handlers
145 idris_nameIdx :: (Int, Ctxt (Int, Name)),
149 idris_nameIdx :: (Int, Ctxt (Int, Name)),
146 idris_function_errorhandlers :: Ctxt (M.Map Name (S.Set Name)), -- ^ Specific error handlers
150 idris_function_errorhandlers :: Ctxt (M.Map Name (S.Set Name)), -- ^ Specific error handlers
147− module_aliases :: M.Map [T.Text] [T.Text]
151+ module_aliases :: M.Map [T.Text] [T.Text],
148−
152+ idris_consolewidth :: ConsoleWidth -- ^ How many chars wide is the console?
151data SizeChange = Smaller | Same | Bigger | Unknown
155data SizeChange = Smaller | Same | Bigger | Unknown
@@ -215,6 +219,7 @@ idrisInit = IState initContext [] [] emptyContext emptyContext emptyContext
215 [] [] defaultOpts 6 [] [] [] [] [] [] [] [] [] [] [] [] []
219 [] [] defaultOpts 6 [] [] [] [] [] [] [] [] [] [] [] [] []
216 [] Nothing Nothing [] [] [] Hidden False [] Nothing [] [] RawOutput
220 [] Nothing Nothing [] [] [] Hidden False [] Nothing [] [] RawOutput
217 True defaultTheme stdout [] (0, emptyContext) emptyContext M.empty
221 True defaultTheme stdout [] (0, emptyContext) emptyContext M.empty
219-- | The monad for the main REPL - reading and processing files and updating
224-- | The monad for the main REPL - reading and processing files and updating
220-- global state (hence the IO inner monad).
225-- global state (hence the IO inner monad).
@@ -691,7 +696,6 @@ data PTactic' t = Intro [Name] | Intros | Focus Name
691deriving instance Binary PTactic'
696deriving instance Binary PTactic'
692deriving instance NFData PTactic'
697deriving instance NFData PTactic'
695instance Sized a => Sized (PTactic' a) where
699instance Sized a => Sized (PTactic' a) where
696 size (Intro nms) = 1 + size nms
700 size (Intro nms) = 1 + size nms
697 size Intros = 1
701 size Intros = 1
@@ -1010,17 +1014,20 @@ piBindp p ((n, ty):ns) t = PPi p n ty (piBindp p ns t)
1011-- Pretty-printing declarations and terms
1015-- Pretty-printing declarations and terms
1017+-- These "show" instances render to an absurdly wide screen because inserted line breaks
1018+-- could interfere with interactive editing, which calls "show".
1013instance Show PTerm where
1020instance Show PTerm where
1014− showsPrec _ tm = (displayS . renderCompact . prettyImp False) tm
1021+ showsPrec _ tm = (displayS . renderPretty 1.0 10000000 . prettyImp False) tm
1016instance Show PDecl where
1023instance Show PDecl where
1017− showsPrec _ d = (displayS . renderCompact . showDeclImp False) d
1024+ showsPrec _ d = (displayS . renderPretty 1.0 10000000 . showDeclImp False) d
1019instance Show PClause where
1026instance Show PClause where
1020− showsPrec _ c = (displayS . renderCompact . showCImp True) c
1027+ showsPrec _ c = (displayS . renderPretty 1.0 10000000 . showCImp True) c
1022instance Show PData where
1029instance Show PData where
1023− showsPrec _ d = (displayS . renderCompact . showDImp False) d
1030+ showsPrec _ d = (displayS . renderPretty 1.0 10000000 . showDImp False) d
1025instance Pretty PTerm OutputAnnotation where
1032instance Pretty PTerm OutputAnnotation where
1026 pretty = prettyImp False
1033 pretty = prettyImp False
@@ -1060,9 +1067,6 @@ pprintPTerm impl bnd = prettySe 10 bnd
1061 prettySe :: Int -> [(Name, Bool)] -> PTerm -> Doc OutputAnnotation
1068 prettySe :: Int -> [(Name, Bool)] -> PTerm -> Doc OutputAnnotation
1062 prettySe p bnd (PQuote r) =
1069 prettySe p bnd (PQuote r) =
1063− if size r > breakingSize then
1064− text "![" <> line <> pretty r <> text "]"
1066 text "![" <> pretty r <> text "]"
1070 text "![" <> pretty r <> text "]"
1067 prettySe p bnd (PPatvar fc n) = pretty n
1071 prettySe p bnd (PPatvar fc n) = pretty n
1068 prettySe p bnd e
1072 prettySe p bnd e
@@ -1070,35 +1074,23 @@ pprintPTerm impl bnd = prettySe 10 bnd
1070 | Just n <- snat p e = annotate AnnConstData (text (show n))
1074 | Just n <- snat p e = annotate AnnConstData (text (show n))
1071 prettySe p bnd (PRef fc n) = prettyName impl bnd n
1075 prettySe p bnd (PRef fc n) = prettyName impl bnd n
1072 prettySe p bnd (PLam n ty sc) =
1076 prettySe p bnd (PLam n ty sc) =
1073− bracket p 2 $
1077+ bracket p 2 . group . hang 2 $
1074− if size sc > breakingSize then
1078+ text "\\" <> bindingOf n False <+> text "=>" <$>
1075− text "\\" <> bindingOf n False <+> text "=>" <> line <> prettySe 10 ((n, False):bnd) sc
1077− text "\\" <> bindingOf n False <+> text "=>" <+> prettySe 10 ((n, False):bnd) sc
1079+ prettySe 10 ((n, False):bnd) sc
1078 prettySe p bnd (PLet n ty v sc) =
1080 prettySe p bnd (PLet n ty v sc) =
1079 bracket p 2 $
1081 bracket p 2 $
1080− if size sc > breakingSize then
1081− text "let" <+> bindingOf n False <+> text "=" <+> prettySe 10 bnd v <+> text "in" <>
1082+ text "let" <+> bindingOf n False <+> text "=" </>
1082− nest nestingSize (prettySe 10 ((n, False):bnd) sc)
1084− text "let" <+> bindingOf n False <+> text "=" <+> prettySe 10 bnd v <+> text "in" <+>
1083+ prettySe 10 bnd v <+> text "in" </>
1085− prettySe 10 ((n, False):bnd) sc
1084+ prettySe 10 ((n, False):bnd) sc
1086 prettySe p bnd (PPi (Exp l s _ _) n ty sc)
1085 prettySe p bnd (PPi (Exp l s _ _) n ty sc)
1087 | n `elem` allNamesIn sc || impl =
1086 | n `elem` allNamesIn sc || impl =
1088 let open = if Lazy `elem` l then text "|" <> lparen else lparen in
1087 let open = if Lazy `elem` l then text "|" <> lparen else lparen in
1089− bracket p 2 $
1088+ bracket p 2 . group $
1090− if size sc > breakingSize then
1089+ enclose open rparen (group . align $ bindingOf n False <+> colon <+> prettySe 10 bnd ty) <+>
1091− enclose open rparen (bindingOf n False <+> colon <+> prettySe 10 bnd ty) <+>
1092− st <> text "->" <> line <> prettySe 10 ((n, False):bnd) sc
1094− enclose open rparen (bindingOf n False <+> colon <+> prettySe 10 bnd ty) <+>
1095− st <> text "->" <+> prettySe 10 ((n, False):bnd) sc
1090+ st <> text "->" <$> prettySe 10 ((n, False):bnd) sc
1096 | otherwise =
1091 | otherwise =
1097− bracket p 2 $
1092+ bracket p 2 . group $
1098− if size sc > breakingSize then
1099− prettySe 0 bnd ty <+> st <> text "->" <> line <> prettySe 10 ((n, False):bnd) sc
1093+ group (prettySe 0 bnd ty <+> st) <> text "->" <$> group (prettySe 10 ((n, False):bnd) sc)
1101− prettySe 0 bnd ty <+> st <> text "->" <+> prettySe 10 ((n, False):bnd) sc
1104 case s of
1096 case s of
@@ -1108,12 +1100,8 @@ pprintPTerm impl bnd = prettySe 10 bnd
1108 | impl =
1100 | impl =
1109 let open = if Lazy `elem` l then text "|" <> lbrace else lbrace in
1101 let open = if Lazy `elem` l then text "|" <> lbrace else lbrace in
1110 bracket p 2 $
1102 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
1115− open <> bindingOf n True <+> colon <+> prettySe 10 bnd ty <> rbrace <+>
1103+ open <> bindingOf n True <+> colon <+> prettySe 10 bnd ty <> rbrace <+>
1116− st <> text "->" <+> prettySe 10 ((n, True):bnd) sc
1104+ st <> text "->" </> prettySe 10 ((n, True):bnd) sc
1117 | otherwise = prettySe 10 ((n, True):bnd) sc
1105 | otherwise = prettySe 10 ((n, True):bnd) sc
@@ -1122,18 +1110,11 @@ pprintPTerm impl bnd = prettySe 10 bnd
1122 _ -> empty
1110 _ -> empty
1123 prettySe p bnd (PPi (Constraint _ _ _) n ty sc) =
1111 prettySe p bnd (PPi (Constraint _ _ _) n ty sc) =
1124 bracket p 2 $
1112 bracket p 2 $
1125− if size sc > breakingSize then
1126− prettySe 10 bnd ty <+> text "=>" <+> prettySe 10 ((n, True):bnd) sc
1113+ prettySe 10 bnd ty <+> text "=>" </> prettySe 10 ((n, True):bnd) sc
1128− prettySe 10 bnd ty <+> text "=>" <> line <> prettySe 10 ((n, True):bnd) sc
1129 prettySe p bnd (PPi (TacImp _ _ s _) n ty sc) =
1114 prettySe p bnd (PPi (TacImp _ _ s _) n ty sc) =
1130 bracket p 2 $
1115 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
1135− lbrace <> text "tacimp" <+> pretty n <+> colon <+> prettySe 10 bnd ty <>
1116+ lbrace <> text "tacimp" <+> pretty n <+> colon <+> prettySe 10 bnd ty <>
1136− rbrace <+> text "->" <+> prettySe 10 ((n, True):bnd) sc
1117+ rbrace <+> text "->" </> prettySe 10 ((n, True):bnd) sc
1137 prettySe p bnd (PApp _ (PRef _ f) [])
1118 prettySe p bnd (PApp _ (PRef _ f) [])
1138 | not impl = prettyName impl bnd f
1119 | not impl = prettyName impl bnd f
1139 prettySe p bnd (PAppBind _ (PRef _ f) [])
1120 prettySe p bnd (PAppBind _ (PRef _ f) [])
@@ -1143,11 +1124,8 @@ pprintPTerm impl bnd = prettySe 10 bnd
1143 , not (tnull nm) &&
1124 , not (tnull nm) &&
1144 length (getExps args) == 2 && (not impl) && (not $ isAlpha (thead nm)) =
1125 length (getExps args) == 2 && (not impl) && (not $ isAlpha (thead nm)) =
1145 let [l, r] = getExps args in
1126 let [l, r] = getExps args in
1146− bracket p 1 $
1127+ bracket p 1 . align . group $
1147− if size r > breakingSize then
1128+ (prettySe 1 bnd l <+> prettyName impl bnd op) <$> group (prettySe 0 bnd r)
1148− prettySe 1 bnd l <+> prettyName impl bnd op <> line <> prettySe 0 bnd r
1150− prettySe 1 bnd l <+> prettyName impl bnd op <+> prettySe 0 bnd r
1151 prettySe p bnd (PApp _ hd@(PRef fc f) [tm])
1129 prettySe p bnd (PApp _ hd@(PRef fc f) [tm])
1152 | PConstant (Idris.Core.TT.Str str) <- getTm tm,
1130 | PConstant (Idris.Core.TT.Str str) <- getTm tm,
1153 f == sUN "Symbol_" = char '\'' <> prettySe 10 bnd (PRef fc (sUN str))
1131 f == sUN "Symbol_" = char '\'' <> prettySe 10 bnd (PRef fc (sUN str))
@@ -1155,10 +1133,14 @@ pprintPTerm impl bnd = prettySe 10 bnd
1155 let args = getExps as
1133 let args = getExps as
1156 fp = prettySe 1 bnd f
1134 fp = prettySe 1 bnd f
1158− bracket p 1 $
1136+ bracket p 1 . group $
1159− hsep (if impl
1137+ if impl
1160− then fp : map (prettyArgS bnd) as
1140+ else fp <+> align (vsep (map (prettyArgS bnd) as))
1161− else fp : map (prettyArgSe bnd) args)
1143+ else fp <+> align (vsep (map (prettyArgSe bnd) args))
1162 prettySe p bnd (PCase _ scr opts) =
1144 prettySe p bnd (PCase _ scr opts) =
1163 text "case" <+> prettySe 10 bnd scr <+> text "of" <> prettyBody
1145 text "case" <+> prettySe 10 bnd scr <+> text "of" <> prettyBody
@@ -1171,32 +1153,18 @@ pprintPTerm impl bnd = prettySe 10 bnd
1171 prettySe p bnd (PTrue _) = annotate (AnnName unitTy Nothing Nothing) $ text "()"
1153 prettySe p bnd (PTrue _) = annotate (AnnName unitTy Nothing Nothing) $ text "()"
1172 prettySe p bnd (PFalse _) = annotate (AnnName falseTy Nothing Nothing) $ text "_|_"
1154 prettySe p bnd (PFalse _) = annotate (AnnName falseTy Nothing Nothing) $ text "_|_"
1173 prettySe p bnd (PEq _ l r) =
1155 prettySe p bnd (PEq _ l r) =
1174− bracket p 2 $
1156+ bracket p 2 . align . group $
1175− if size r > breakingSize then
1176− prettySe 10 bnd l <+> eq <$> nest nestingSize (prettySe 10 bnd r)
1157+ prettySe 10 bnd l <+> eq <$> group (prettySe 10 bnd r)
1178− prettySe 10 bnd l <+> eq <+> prettySe 10 bnd r
1179 where eq = annotate (AnnName eqTy Nothing Nothing) (text "=")
1158 where eq = annotate (AnnName eqTy Nothing Nothing) (text "=")
1180 prettySe p bnd (PRewrite _ l r _) =
1159 prettySe p bnd (PRewrite _ l r _) =
1181 bracket p 2 $
1160 bracket p 2 $
1182− if size r > breakingSize then
1183− text "rewrite" <+> prettySe 10 bnd l <+> text "in" <> nest nestingSize (prettySe 10 bnd r)
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
1186 prettySe p bnd (PTyped l r) =
1162 prettySe p bnd (PTyped l r) =
1187 lparen <> prettySe 10 bnd l <+> colon <+> prettySe 10 bnd r <> rparen
1163 lparen <> prettySe 10 bnd l <+> colon <+> prettySe 10 bnd r <> rparen
1188 prettySe p bnd (PPair _ l r) =
1164 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
1193− lparen <> prettySe 10 bnd l <> text "," <+> prettySe 10 bnd r <> rparen
1165+ lparen <> prettySe 10 bnd l <> text "," <+> prettySe 10 bnd r <> rparen
1194 prettySe p bnd (PDPair _ l t r) =
1166 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
1199− lparen <> prettySe 10 bnd l <+> text "**" <+> prettySe 10 bnd r <> rparen
1167+ lparen <> prettySe 10 bnd l <+> text "**" <+> prettySe 10 bnd r <> rparen
1200 prettySe p bnd (PAlternative a as) =
1168 prettySe p bnd (PAlternative a as) =
1201 lparen <> text "|" <> prettyAs <> text "|" <> rparen
1169 lparen <> text "|" <> prettyAs <> text "|" <> rparen