@@ -31,6 +31,7 @@ import qualified Data.Map as M
3131import Data.Either
3232import qualified Data.Set as S
3333import Data.Word (Word)
34+import Data.Maybe (fromMaybe)
3435
3536import Debug.Trace
3637
@@ -1133,6 +1134,10 @@ prettyImp :: Bool -- ^^ whether to show implicits
11331134 -> Doc OutputAnnotation
11341135prettyImp impl = pprintPTerm impl [] [] []
11351136
1137+-- | Do the right thing for rendering a term in an IState
1138+prettyIst :: IState -> PTerm -> Doc OutputAnnotation
1139+prettyIst ist = pprintPTerm (opt_showimp (idris_options ist)) [] [] (idris_infixes ist)
1140+
11361141-- | Pretty-print a high-level Idris term in some bindings context with infix info
11371142pprintPTerm :: Bool -- ^^ whether to show implicits
11381143 -> [(Name, Bool)] -- ^^ the currently-bound names and whether they are implicit
@@ -1205,13 +1210,23 @@ pprintPTerm impl bnd docArgs infixes = prettySe 10 bnd
12051210 case getExps args of
12061211 [] -> enclose lparen rparen opName
12071212 [x] -> group (enclose lparen rparen opName <$> group (prettySe 0 bnd x))
1208− [l,r] -> bracket p 1 $ inFix l r
1213+ [l,r] -> let precedence = fromMaybe 20 (fmap prec f)
1214+ in trace ("p=" ++ show p++",prec="++show precedence) (bracket p precedence $ inFix l r)
12091215 (l:r:rest) -> bracket p 1 $
12101216 enclose lparen rparen (inFix l r) <+>
12111217 align (group (vsep (map (prettyArgSe bnd) rest)))
12121218 where opName = prettyName impl bnd op
1219+ f = getFixity (opStr op)
1220+ left l = case f of
1221+ Nothing -> prettySe (-1) bnd l
1222+ Just (Infixl p') -> prettySe p' bnd l
1223+ Just f' -> prettySe (prec f'-1) bnd l
1224+ right r = case f of
1225+ Nothing -> prettySe (-1) bnd r
1226+ Just (Infixr p') -> prettySe p' bnd r
1227+ Just f' -> prettySe (prec f'-1) bnd r
12131228 inFix l r = align . group $
1214− (prettySe 1 bnd l <+> opName) <$> group (prettySe 0 bnd r)
1229+ (left l <+> opName) <$> group (right r)
12151230 prettySe p bnd (PApp _ hd@(PRef fc f) [tm])
12161231 | PConstant (Idris.Core.TT.Str str) <- getTm tm,
12171232 f == sUN "Symbol_" = char '\'' <> prettySe 10 bnd (PRef fc (sUN str))
@@ -1316,6 +1331,10 @@ pprintPTerm impl bnd docArgs infixes = prettySe 10 bnd
13161331 annName :: Name -> Doc OutputAnnotation -> Doc OutputAnnotation
13171332 annName n = annotate (AnnName n Nothing Nothing Nothing)
13181333
1334+ opStr :: Name -> String
1335+ opStr (NS n _) = opStr n
1336+ opStr (UN n) = T.unpack n
1337+
13191338 basename :: Name -> Name
13201339 basename (NS n _) = basename n
13211340 basename n = n
@@ -1369,6 +1388,12 @@ pprintPTerm impl bnd docArgs infixes = prettySe 10 bnd
13691388
13701389 kwd = annotate AnnKeyword . text
13711390
1391+ fixities :: M.Map String Fixity
1392+ fixities = M.fromList [(s, f) | (Fix f s) <- infixes]
1393+
1394+ getFixity :: String -> Maybe Fixity
1395+ getFixity = flip M.lookup fixities
1396+
13721397-- | Pretty-printer helper for the binding site of a name
13731398bindingOf :: Name -- ^^ the bound name
13741399 -> Bool -- ^^ whether the name is implicit