@@ -342,34 +342,63 @@ runIdeSlaveCommand id orig fn mods (IdeSlave.Metavariables cols) =
342342 do ist <- getIState
343343 let mvs = reverse $ map fst (idris_metavars ist) \\ primDefs
344344 let ppo = ppOptionIst ist
345− let mvarTys = map (delabTy ist) mvs
346− let res = (IdeSlave.SymbolAtom "ok",
347− zipWith (\ n (prems, concl) -> (n, prems, concl))
348− (map (IdeSlave.StringAtom . show) mvs)
349− (map (sexpGoal ist cols ppo [] . getGoal) mvarTys))
350− runIO . putStrLn $ IdeSlave.convSExp "return" res id
351− where getGoal :: PTerm -> ([(Name, PTerm)], PTerm)
352− getGoal (PPi _ n t sc) = let (prems, conc) = getGoal sc
353− in ((n, t):prems, conc)
354− getGoal tm = ([], tm)
355− sexpGoal :: IState -> Int -> PPOption -> [Name] -> ([(Name, PTerm)], PTerm)
356− -> ([(String, String, SpanList OutputAnnotation)],
357− (String, SpanList OutputAnnotation))
358− sexpGoal ist cols ppo ns ([], concl) =
359− let infixes = idris_infixes ist
360− concl' = displaySpans . renderPretty 0.9 cols . fmap (fancifyAnnots ist) $
361− pprintPTerm ppo (zip ns (repeat False)) [] infixes concl
362− in ([], concl')
363− sexpGoal ist cols ppo ns ((n, t):ps, concl) =
364− let n' = case n of
365− NS (UN nm) ns -> str nm
366− UN nm | ('_':'_':_) <- str nm -> "_"
367− | otherwise -> str nm
368− _ -> "_"
369− (t', spans) = displaySpans . renderPretty 0.9 cols . fmap (fancifyAnnots ist) $
370− pprintPTerm ppo (zip ns (repeat False)) [] (idris_infixes ist) t
371− rest = sexpGoal ist cols ppo (n:ns) (ps, concl)
372− in ((n', t', spans) : fst rest, snd rest)
345+ -- splitMvs is a list of pairs of names and their split types
346+ let splitMvs = mapSnd (splitPi ist) (mvTys ist mvs)
347+ -- mvOutput is the pretty-printed version ready for conversion to SExpr
348+ let mvOutput = map (\(n, (hs, c)) -> (n, hs, c)) $
349+ mapPair show
350+ (\(hs, c, pc) ->
351+ let bnd = [ n | (n,_,_) <- hs ] in
352+ let bnds = inits bnd in
353+ (map (\(bnd, h) -> processPremise ist bnd h)
354+ (zip bnds hs),
355+ render ist bnd c pc))
356+ splitMvs
357+ runIO . putStrLn $
358+ IdeSlave.convSExp "return" (IdeSlave.SymbolAtom "ok", mvOutput) id
359+ where mapPair f g xs = zip (map (f . fst) xs) (map (g . snd) xs)
360+ mapSnd f xs = zip (map fst xs) (map (f . snd) xs)
361+
362+ -- | Split a function type into a pair of premises, conclusion.
363+ -- Each maintains both the original and delaborated versions.
364+ splitPi :: IState -> Type -> ([(Name, Type, PTerm)], Type, PTerm)
365+ splitPi ist (Bind n (Pi t) rest) =
366+ let (hs, c, pc) = splitPi ist rest in
367+ ((n, t, delabTy' ist [] t False False):hs,
368+ c, delabTy' ist [] c False False)
369+ splitPi ist tm = ([], tm, delabTy' ist [] tm False False)
370+
371+ -- | Get the types of a list of metavariable names
372+ mvTys :: IState -> [Name] -> [(Name, Type)]
373+ mvTys ist = mapSnd vToP . mapMaybe (flip lookupTyNameExact (tt_ctxt ist))
374+
375+ -- | Show a type and its corresponding PTerm in a format suitable
376+ -- for the IDE - that is, pretty-printed and annotated.
377+ render :: IState -> [Name] -> Type -> PTerm -> (String, SpanList OutputAnnotation)
378+ render ist bnd t pt =
379+ let prettyT = pprintPTerm (ppOptionIst ist)
380+ (zip bnd (repeat False))
381+ []
382+ (idris_infixes ist)
383+ pt
384+ in
385+ displaySpans .
386+ renderPretty 0.9 cols .
387+ fmap (fancifyAnnots ist) .
388+ annotate (AnnTerm (zip bnd (take (length bnd) (repeat False))) t) $
389+ prettyT
390+
391+ -- | Juggle the bits of a premise to prepare for output.
392+ processPremise :: IState
393+ -> [Name] -- ^ the names to highlight as bound
394+ -> (Name, Type, PTerm)
395+ -> (String,
396+ String,
397+ SpanList OutputAnnotation)
398+ processPremise ist bnd (n, t, pt) =
399+ let (out, spans) = render ist bnd t pt in
400+ (show n , out, spans)
401+
373402runIdeSlaveCommand id orig fn mods (IdeSlave.WhoCalls n) =
374403 case splitName n of
375404 Left err -> iPrintError err
@@ -394,6 +423,40 @@ runIdeSlaveCommand id orig fn mods (IdeSlave.CallsWho n) =
394423 renderPretty 0.9 1000 .
395424 fmap (fancifyAnnots ist) .
396425 prettyName True []
426+runIdeSlaveCommand id orig fn modes (IdeSlave.TermNormalise bnd tm) =
427+ do ctxt <- getContext
428+ ist <- getIState
429+ let tm' = force (normaliseAll ctxt [] tm)
430+ ptm = annotate (AnnTerm bnd tm')
431+ (pprintPTerm (ppOptionIst ist)
432+ bnd
433+ []
434+ (idris_infixes ist)
435+ (delab ist tm'))
436+ msg = (IdeSlave.SymbolAtom "ok",
437+ displaySpans .
438+ renderPretty 0.9 80 .
439+ fmap (fancifyAnnots ist) $ ptm)
440+ runIO . putStrLn $ IdeSlave.convSExp "return" msg id
441+runIdeSlaveCommand id orig fn modes (IdeSlave.TermShowImplicits bnd tm) =
442+ ideSlaveForceTermImplicits id bnd True tm
443+runIdeSlaveCommand id orig fn modes (IdeSlave.TermNoImplicits bnd tm) =
444+ ideSlaveForceTermImplicits id bnd False tm
445+
446+-- | Show a term for IDESlave with the specified implicitness
447+ideSlaveForceTermImplicits :: Integer -> [(Name, Bool)] -> Bool -> Term -> Idris ()
448+ideSlaveForceTermImplicits id bnd impl tm =
449+ do ist <- getIState
450+ let expl = annotate (AnnTerm bnd tm)
451+ (pprintPTerm ((ppOptionIst ist) { ppopt_impl = impl })
452+ bnd [] (idris_infixes ist)
453+ (delab ist tm))
454+ msg = (IdeSlave.SymbolAtom "ok",
455+ displaySpans .
456+ renderPretty 0.9 80 .
457+ fmap (fancifyAnnots ist) $ expl)
458+ runIO . putStrLn $ IdeSlave.convSExp "return" msg id
459+
397460
398461splitName :: String -> Either String Name
399462splitName s = case reverse $ splitOn "." s of
@@ -594,8 +657,8 @@ process h fn (Eval t)
594657 ist <- getIState
595658 logLvl 3 $ "Raw: " ++ show (tm', ty')
596659 logLvl 10 $ "Debug: " ++ showEnvDbg [] tm'
597− let tmDoc = prettyIst ist (delab ist tm')
598− tyDoc = prettyIst ist (delab ist ty')
660+ let tmDoc = pprintDelab ist tm'
661+ tyDoc = pprintDelab ist ty'
599662 ihPrintTermWithType h tmDoc tyDoc
600663
601664process h fn (ExecVal t)
@@ -653,8 +716,8 @@ process h fn (Check t)
653716 case tm of
654717 TType _ ->
655718 ihPrintTermWithType h (prettyIst ist PType) type1Doc
656− _ -> ihPrintTermWithType h (prettyIst ist (delab ist tm))
657− (prettyIst ist (delab ist ty))
719+ _ -> ihPrintTermWithType h (pprintDelab ist tm)
720+ (pprintDelab ist ty)
658721
659722process h fn (DocStr (Left n))
660723 = do ist <- getIState