1module Idris.Output where
4import Idris.Core.Evaluate (isDConName, isTConName, isFnName)
12import Util.ScreenSize (getScreenWidth)
14import System.IO (stdout, Handle, hPutStrLn)
16pshow :: IState -> Err -> String
17pshow ist err = displayDecorated (consoleDecorate ist) .
19 fmap (fancifyAnnots ist) $ pprintErr ist err
21ihWarn :: Handle -> FC -> Doc OutputAnnotation -> Idris ()
22ihWarn h fc err = do i <- getIState
23 case idris_outputmode i of
25 do err' <- iRender . fmap (fancifyAnnots i) $
26 text (show fc) <> colon <//> err
27 runIO . hPutStrLn h $ displayDecorated (consoleDecorate i) err'
29 do err' <- iRender . fmap (fancifyAnnots i) $ err
30 let (str, spans) = displaySpans err'
32 convSExp "warning" (fc_fname fc, fc_start fc, fc_end fc, str, spans) n
34iRender :: Doc a -> Idris (SimpleDoc a)
35iRender d = do w <- getWidth
37 let ideSlave = case idris_outputmode ist of
41 InfinitelyWide -> return $ renderPretty 1.0 1000000000 d
42 ColsWide n -> return $
44 then renderPretty 1.0 1000000000 d
45 else renderPretty 0.8 n d
46 AutomaticWidth | ideSlave -> return $ renderPretty 1.0 1000000000 d
47 | otherwise -> do width <- runIO getScreenWidth
48 return $ renderPretty 0.8 width d
50ihPrintResult :: Handle -> String -> Idris ()
51ihPrintResult h s = do i <- getIState
52 case idris_outputmode i of
53 RawOutput -> case s of
55 s -> runIO $ hPutStrLn h s
57 let good = SexpList [SymbolAtom "ok", toSExp s] in
58 runIO $ hPutStrLn h $ convSExp "return" good n
60-- | Write a pretty-printed term to the console with semantic coloring
61consoleDisplayAnnotated :: Handle -> Doc OutputAnnotation -> Idris ()
62consoleDisplayAnnotated h output = do ist <- getIState
63 rendered <- iRender $ output
65 displayDecorated (consoleDecorate ist) $
69-- | Write pretty-printed output to IDESlave with semantic annotations
70ideSlaveReturnAnnotated :: Integer -> Handle -> Doc OutputAnnotation -> Idris ()
71ideSlaveReturnAnnotated n h out = do ist <- getIState
72 (str, spans) <- fmap displaySpans .
74 fmap (fancifyAnnots ist) $
76 let good = [SymbolAtom "ok", toSExp str, toSExp spans]
77 runIO . hPutStrLn h $ convSExp "return" good n
79ihPrintTermWithType :: Handle -> Doc OutputAnnotation -> Doc OutputAnnotation -> Idris ()
80ihPrintTermWithType h tm ty = do ist <- getIState
81 let output = tm <+> colon <+> ty
82 case idris_outputmode ist of
83 RawOutput -> consoleDisplayAnnotated h output
84 IdeSlave n -> ideSlaveReturnAnnotated n h output
86-- | Pretty-print a collection of overloadings to REPL or IDESlave - corresponds to :t name
87ihPrintFunTypes :: Handle -> [(Name, Bool)] -> Name -> [(Name, PTerm)] -> Idris ()
88ihPrintFunTypes h bnd n [] = ihPrintError h $ "No such variable " ++ show n
89ihPrintFunTypes h bnd n overloads = do imp <- impShow
91 let output = vsep (map (uncurry (ppOverload imp)) overloads)
92 case idris_outputmode ist of
93 RawOutput -> consoleDisplayAnnotated h output
94 IdeSlave n -> ideSlaveReturnAnnotated n h output
95 where fullName n = prettyName True bnd n
96 ppOverload imp n tm = fullName n <+> colon <+> align (pprintPTerm imp bnd [] tm)
98ihRenderResult :: Handle -> Doc OutputAnnotation -> Idris ()
99ihRenderResult h d = do ist <- getIState
100 case idris_outputmode ist of
101 RawOutput -> consoleDisplayAnnotated h d
102 IdeSlave n -> ideSlaveReturnAnnotated n h d
104fancifyAnnots :: IState -> OutputAnnotation -> OutputAnnotation
105fancifyAnnots ist annot@(AnnName n _ _ _) =
106 do let ctxt = tt_ctxt ist
107 docs = docOverview ist n
108 ty = Just (getTy ist n)
110 _ | isDConName n ctxt -> AnnName n (Just DataOutput) docs ty
111 _ | isFnName n ctxt -> AnnName n (Just FunOutput) docs ty
112 _ | isTConName n ctxt -> AnnName n (Just TypeOutput) docs ty
113 _ | isMetavarName n ist -> AnnName n (Just MetavarOutput) docs ty
114 _ | otherwise -> annot
115 where docOverview :: IState -> Name -> Maybe String -- pretty-print first paragraph of docs
116 docOverview ist n = do docs <- lookupCtxtExact n (idris_docstrings ist)
117 let o = overview (fst docs)
118 -- TODO make width configurable
119 out = displayS . renderPretty 1.0 50 $ renderDocstring o
121 getTy :: IState -> Name -> String -- fails if name not already extant!
122 getTy ist n = let theTy = pprintPTerm (opt_showimp (idris_options ist)) [] [] $
124 in (displayS . renderPretty 1.0 50 $ theTy) ""
125fancifyAnnots _ annot = annot
127-- | Show an error with semantic highlighting
128ihRenderError :: Handle -> Doc OutputAnnotation -> Idris ()
129ihRenderError h e = do ist <- getIState
130 case idris_outputmode ist of
131 RawOutput -> consoleDisplayAnnotated h e
133 (str, spans) <- fmap displaySpans .
135 fmap (fancifyAnnots ist) $
137 let good = [SymbolAtom "error", toSExp str, toSExp spans]
138 runIO . hPutStrLn h $ convSExp "return" good n
140ihPrintError :: Handle -> String -> Idris ()
141ihPrintError h s = do i <- getIState
142 case idris_outputmode i of
143 RawOutput -> case s of
145 s -> runIO $ hPutStrLn h s
147 let good = SexpList [SymbolAtom "error", toSExp s] in
148 runIO . hPutStrLn h $ convSExp "return" good n
150ihputStrLn :: Handle -> String -> Idris ()
151ihputStrLn h s = do i <- getIState
152 case idris_outputmode i of
153 RawOutput -> runIO $ hPutStrLn h s
154 IdeSlave n -> runIO . hPutStrLn h $ convSExp "write-string" s n
156iputStrLn = ihputStrLn stdout
157iPrintError = ihPrintError stdout
158iPrintResult = ihPrintResult stdout
161ideslavePutSExp :: SExpable a => String -> a -> Idris ()
162ideslavePutSExp cmd info = do i <- getIState
163 case idris_outputmode i of
164 IdeSlave n -> runIO . putStrLn $ convSExp cmd info n
167-- this needs some typing magic and more structured output towards emacs
168iputGoal :: SimpleDoc OutputAnnotation -> Idris ()
169iputGoal g = do i <- getIState
170 case idris_outputmode i of
171 RawOutput -> runIO $ putStrLn (displayDecorated (consoleDecorate i) g)
172 IdeSlave n -> runIO . putStrLn $
173 convSExp "write-goal" (displayS (fmap (fancifyAnnots i) g) "") n
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list