Debian Codegraph @

src/Yi/Command/Help.hs

raw
1 {-# LANGUAGE DeriveDataTypeable #-}
2 {-# LANGUAGE GeneralizedNewtypeDeriving #-}
3 {-# LANGUAGE OverloadedStrings #-}
4 -- |
5 -- Module : Yi.Command.Help
6 -- License : GPL-2
7 -- Maintainer : [email protected]
8 -- Stability : experimental
9 -- Portability : portable
10 --
11 -- Help command support
12 -- This module uses Yi.Eval.describeNamedAction to
13 -- show whatever information about particular action is available
14 -- from current evaluator (ghciEvaluator currently presents only type.)
15 -- TODO: Would be nice to show excerpt from Haddock documentation in the future.
16 --
17 -- If given no arguments, the help index is shown (using @getAllNamesInScope@).
18 --
19 -- Please do not try to show descriptions for the whole index,
20 -- as our interface to GHCi is too slow.
21
22 module Yi.Command.Help(displayHelpFor) where
23
24 import Data.Binary (Binary)
25 import Data.Default (Default)
26 import qualified Data.Text as T (Text, pack, unlines, unpack)
27 import Data.Typeable (Typeable)
28 import Yi.Buffer (BufferId (MemBuffer), BufferRef)
29 import Yi.Editor
30 import Yi.Eval (describeNamedAction, getAllNamesInScope)
31 import Yi.Keymap (YiM)
32 import Yi.Monad (maybeM)
33 import qualified Yi.Rope as R (fromText)
34 import Yi.Types (YiVariable)
35
36
37 -- | Displays help for a given name, or help index, if no name is given
38 displayHelpFor :: T.Text -> YiM ()
39 displayHelpFor name = helpFor name >>= displayHelpBuffer
40
41 -- | Finds help text to display, given a command argument
42 helpFor :: T.Text -> YiM T.Text
43 helpFor "" = (T.unlines . map T.pack) <$> getAllNamesInScope
44 helpFor name = T.pack <$> describeNamedAction (T.unpack name)
45
46 -- * To make help buffer unique:
47 -- | Dynamic YiVariable to store the help buffer reference.
48 newtype HelpBuffer = HelpBuffer { helpBuffer :: Maybe BufferRef }
49 deriving (Default, Typeable, Binary)
50
51 instance YiVariable HelpBuffer
52
53 -- | Display help buffer with a given text...
54 displayHelpBuffer :: T.Text -> YiM ()
55 displayHelpBuffer text = withEditor $ withOtherWindow $ do
56 maybeM deleteBuffer =<< helpBuffer <$> getEditorDyn
57 b <- newBufferE (MemBuffer "*help*") $ R.fromText text
58 putEditorDyn $ HelpBuffer $ Just b