@@ -16,6 +16,7 @@ import Debug.Trace
1616import System.IO (stdout, Handle, hPutStrLn)
1717
1818import Data.List (nub)
19+import Data.Maybe (fromMaybe)
1920
2021pshow :: IState -> Err -> String
2122pshow ist err = displayDecorated (consoleDecorate ist) .
@@ -53,16 +54,6 @@ iRender d = do w <- getWidth
5354 | otherwise -> do width <- runIO getScreenWidth
5455 return $ renderPretty 0.8 width d
5556
56−ihPrintResult :: Handle -> String -> Idris ()
57−ihPrintResult h s = do i <- getIState
58− case idris_outputmode i of
59− RawOutput -> case s of
60− "" -> return ()
61− s -> runIO $ hPutStrLn h s
62− IdeSlave n ->
63− let good = SexpList [SymbolAtom "ok", toSExp s] in
64− runIO $ hPutStrLn h $ convSExp "return" good n
65−
6657-- | Write a pretty-printed term to the console with semantic coloring
6758consoleDisplayAnnotated :: Handle -> Doc OutputAnnotation -> Idris ()
6859consoleDisplayAnnotated h output = do ist <- getIState
@@ -71,23 +62,8 @@ consoleDisplayAnnotated h output = do ist <- getIState
7162 displayDecorated (consoleDecorate ist) $
7263 rendered
7364
74−
75−-- | Write pretty-printed output to IDESlave with semantic annotations
76−ideSlaveReturnAnnotated :: Integer -> Handle -> Doc OutputAnnotation -> Idris ()
77−ideSlaveReturnAnnotated n h out = do ist <- getIState
78− (str, spans) <- fmap displaySpans .
79− iRender .
80− fmap (fancifyAnnots ist) $
81− out
82− let good = [SymbolAtom "ok", toSExp str, toSExp spans]
83− runIO . hPutStrLn h $ convSExp "return" good n
84−
8565ihPrintTermWithType :: Handle -> Doc OutputAnnotation -> Doc OutputAnnotation -> Idris ()
86−ihPrintTermWithType h tm ty = do ist <- getIState
87− let output = tm <+> colon <+> align ty
88− case idris_outputmode ist of
89− RawOutput -> consoleDisplayAnnotated h output
90− IdeSlave n -> ideSlaveReturnAnnotated n h output
66+ihPrintTermWithType h tm ty = ihRenderResult h (tm <+> colon <+> align ty)
9167
9268-- | Pretty-print a collection of overloadings to REPL or IDESlave - corresponds to :t name
9369ihPrintFunTypes :: Handle -> [(Name, Bool)] -> Name -> [(Name, PTerm)] -> Idris ()
@@ -96,9 +72,7 @@ ihPrintFunTypes h bnd n overloads = do ist <- getIState
9672 let ppo = ppOptionIst ist
9773 let infixes = idris_infixes ist
9874 let output = vsep (map (uncurry (ppOverload ppo infixes)) overloads)
99− case idris_outputmode ist of
100− RawOutput -> consoleDisplayAnnotated h output
101− IdeSlave n -> ideSlaveReturnAnnotated n h output
75+ ihRenderResult h output
10276 where fullName n = prettyName True bnd n
10377 ppOverload ppo infixes n tm =
10478 fullName n <+> colon <+> align (pprintPTerm ppo bnd [] infixes tm)
@@ -109,18 +83,20 @@ ihRenderResult h d = do ist <- getIState
10983 RawOutput -> consoleDisplayAnnotated h d
11084 IdeSlave n -> ideSlaveReturnAnnotated n h d
11185
112−fancifyAnnots :: IState -> OutputAnnotation -> OutputAnnotation
113−fancifyAnnots ist annot@(AnnName n _ _ _) =
114− do let ctxt = tt_ctxt ist
115− docs = docOverview ist n
116− ty = Just (getTy ist n)
117− case () of
118− _ | isDConName n ctxt -> AnnName n (Just DataOutput) docs ty
119− _ | isFnName n ctxt -> AnnName n (Just FunOutput) docs ty
120− _ | isTConName n ctxt -> AnnName n (Just TypeOutput) docs ty
121− _ | isMetavarName n ist -> AnnName n (Just MetavarOutput) docs ty
122− _ | otherwise -> annot
123− where docOverview :: IState -> Name -> Maybe String -- pretty-print first paragraph of docs
86+makeAnnName :: IState -> Name -> Maybe OutputAnnotation
87+makeAnnName ist n
88+ | isDConName n ctxt = annName DataOutput
89+ | isFnName n ctxt = annName FunOutput
90+ | isTConName n ctxt = annName TypeOutput
91+ | isMetavarName n ist = annName MetavarOutput
92+ | otherwise = Nothing
93+ where ctxt = tt_ctxt ist
94+ docs = docOverview ist n
95+ ty = Just (getTy ist n)
96+
97+ annName :: NameOutput -> Maybe OutputAnnotation
98+ annName nameType = Just (AnnName n (Just nameType) docs ty)
99+ docOverview :: IState -> Name -> Maybe String -- pretty-print first paragraph of docs
124100 docOverview ist n = do docs <- lookupCtxtExact n (idris_docstrings ist)
125101 let o = overview (fst docs)
126102 -- TODO make width configurable
@@ -130,30 +106,54 @@ fancifyAnnots ist annot@(AnnName n _ _ _) =
130106 getTy ist n = let theTy = pprintPTerm (ppOptionIst ist) [] [] (idris_infixes ist) $
131107 delabTy ist n
132108 in (displayS . renderPretty 1.0 50 $ theTy) ""
109+
110+
111+fancifyAnnots :: IState -> OutputAnnotation -> OutputAnnotation
112+fancifyAnnots ist annot@(AnnName n _ _ _) = case makeAnnName ist n of
113+ Just fancyAnnot -> fancyAnnot
114+ Nothing -> annot
133115fancifyAnnots _ annot = annot
134116
117+
118+ideSlaveReturnWithStatus :: String -> Integer -> Handle -> Doc OutputAnnotation -> Idris ()
119+ideSlaveReturnWithStatus status n h out = do
120+ ist <- getIState
121+ (str, spans) <- fmap displaySpans .
122+ iRender .
123+ fmap (fancifyAnnots ist) $
124+ out
125+ let good = [SymbolAtom status, toSExp str, toSExp spans]
126+ runIO . hPutStrLn h $ convSExp "return" good n
127+
128+
129+-- | Write pretty-printed output to IDESlave with semantic annotations
130+ideSlaveReturnAnnotated :: Integer -> Handle -> Doc OutputAnnotation -> Idris ()
131+ideSlaveReturnAnnotated = ideSlaveReturnWithStatus "ok"
132+
135133-- | Show an error with semantic highlighting
136134ihRenderError :: Handle -> Doc OutputAnnotation -> Idris ()
137135ihRenderError h e = do ist <- getIState
138136 case idris_outputmode ist of
139137 RawOutput -> consoleDisplayAnnotated h e
140− IdeSlave n -> do
141− (str, spans) <- fmap displaySpans .
142− iRender .
143− fmap (fancifyAnnots ist) $
144− e
145− let good = [SymbolAtom "error", toSExp str, toSExp spans]
146− runIO . hPutStrLn h $ convSExp "return" good n
138+ IdeSlave n -> ideSlaveReturnWithStatus "error" n h e
139+
140+ihPrintWithStatus :: String -> Handle -> String -> Idris ()
141+ihPrintWithStatus status h s = do
142+ i <- getIState
143+ case idris_outputmode i of
144+ RawOutput -> case s of
145+ "" -> return ()
146+ s -> runIO $ hPutStrLn h s
147+ IdeSlave n ->
148+ let good = SexpList [SymbolAtom status, toSExp s] in
149+ runIO $ hPutStrLn h $ convSExp "return" good n
150+
151+
152+ihPrintResult :: Handle -> String -> Idris ()
153+ihPrintResult = ihPrintWithStatus "ok"
147154
148155ihPrintError :: Handle -> String -> Idris ()
149−ihPrintError h s = do i <- getIState
150− case idris_outputmode i of
151− RawOutput -> case s of
152− "" -> return ()
153− s -> runIO $ hPutStrLn h s
154− IdeSlave n ->
155− let good = SexpList [SymbolAtom "error", toSExp s] in
156− runIO . hPutStrLn h $ convSExp "return" good n
156+ihPrintError = ihPrintWithStatus "error"
157157
158158ihputStrLn :: Handle -> String -> Idris ()
159159ihputStrLn h s = do i <- getIState