@@ -20,19 +20,20 @@ import Neovim.Classes
2020
2121import Control.Applicative
2222import Control.Monad.Except
23- import qualified Data.ByteString as B
24- import Data.Map (Map )
25- import qualified Data.Map as Map
23+ import qualified Data.ByteString as B
24+ import Data.Map (Map )
25+ import qualified Data.Map as Map
2626import Data.MessagePack
2727import Data.Monoid
2828import Data.Serialize
29- import System.IO (hClose )
29+ import Neovim.Compat.Megaparsec as P
30+ import System.IO (hClose )
3031import System.Process
31- import Neovim.Compat.Megaparsec as P
32- import Text.PrettyPrint.ANSI.Leijen ( Doc )
33- import qualified Text.PrettyPrint.ANSI.Leijen as P
34- import UnliftIO.Exception ( SomeException ,
35- bracket , catch )
32+ import UnliftIO.Exception ( SomeException ,
33+ bracket , catch )
34+
35+ import Data.Text.Prettyprint.Doc ( Doc , Pretty ( .. ), (<+>) )
36+ import Data.Text.Prettyprint.Doc.Render.Terminal ( AnsiStyle )
3637
3738import Prelude
3839
@@ -80,10 +81,10 @@ data NeovimAPI
8081 deriving (Show )
8182
8283-- | Run @nvim --api-info@ and parse its output.
83- parseAPI :: IO (Either Doc NeovimAPI )
84- parseAPI = either (Left . P. text ) extractAPI <$> (decodeAPI `catch` readFromAPIFile)
84+ parseAPI :: IO (Either ( Doc AnsiStyle ) NeovimAPI )
85+ parseAPI = either (Left . pretty ) extractAPI <$> (decodeAPI `catch` readFromAPIFile)
8586
86- extractAPI :: Object -> Either Doc NeovimAPI
87+ extractAPI :: Object -> Either ( Doc AnsiStyle ) NeovimAPI
8788extractAPI apiObj = fromObject apiObj >>= \ apiMap -> NeovimAPI
8889 <$> extractErrorTypes apiMap
8990 <*> extractCustomTypes apiMap
@@ -112,43 +113,43 @@ decodeAPI = bracket queryNeovimAPI clean $ \(out, _) ->
112113 terminateProcess ph
113114
114115
115- oLookup :: (NvimObject o ) => String -> Map String Object -> Either Doc o
116+ oLookup :: (NvimObject o ) => String -> Map String Object -> Either ( Doc AnsiStyle ) o
116117oLookup qry = maybe throwErrorMessage fromObject . Map. lookup qry
117118 where
118- throwErrorMessage = throwError . P. text $ " No entry for: " <> show qry
119+ throwErrorMessage = throwError $ " No entry for:" <+> pretty qry
119120
120121
121- oLookupDefault :: (NvimObject o ) => o -> String -> Map String Object -> Either Doc o
122+ oLookupDefault :: (NvimObject o ) => o -> String -> Map String Object -> Either ( Doc AnsiStyle ) o
122123oLookupDefault d qry m = maybe (return d) fromObject $ Map. lookup qry m
123124
124125
125- extractErrorTypes :: Map String Object -> Either Doc [(String , Int64 )]
126+ extractErrorTypes :: Map String Object -> Either ( Doc AnsiStyle ) [(String , Int64 )]
126127extractErrorTypes objAPI = extractTypeNameAndID =<< oLookup " error_types" objAPI
127128
128129
129- extractTypeNameAndID :: Object -> Either Doc [(String , Int64 )]
130+ extractTypeNameAndID :: Object -> Either ( Doc AnsiStyle ) [(String , Int64 )]
130131extractTypeNameAndID m = do
131132 types <- Map. toList <$> fromObject m
132133 forM types $ \ (errName, idMap) -> do
133134 i <- oLookup " id" idMap
134135 return (errName,i)
135136
136137
137- extractCustomTypes :: Map String Object -> Either Doc [(String , Int64 )]
138+ extractCustomTypes :: Map String Object -> Either ( Doc AnsiStyle ) [(String , Int64 )]
138139extractCustomTypes objAPI = extractTypeNameAndID =<< oLookup " types" objAPI
139140
140141
141- extractFunctions :: Map String Object -> Either Doc [NeovimFunction ]
142+ extractFunctions :: Map String Object -> Either ( Doc AnsiStyle ) [NeovimFunction ]
142143extractFunctions objAPI = mapM extractFunction =<< oLookup " functions" objAPI
143144
144145
145- toParameterlist :: [(String , String )] -> Either Doc [(NeovimType , String )]
146+ toParameterlist :: [(String , String )] -> Either ( Doc AnsiStyle ) [(NeovimType , String )]
146147toParameterlist ps = forM ps $ \ (t,n) -> do
147148 t' <- parseType t
148149 return (t', n)
149150
150151
151- extractFunction :: Map String Object -> Either Doc NeovimFunction
152+ extractFunction :: Map String Object -> Either ( Doc AnsiStyle ) NeovimFunction
152153extractFunction funDefMap = NeovimFunction
153154 <$> (oLookup " name" funDefMap)
154155 <*> (oLookup " parameters" funDefMap >>= toParameterlist)
@@ -157,8 +158,8 @@ extractFunction funDefMap = NeovimFunction
157158 <*> (oLookup " return_type" funDefMap >>= parseType)
158159
159160
160- parseType :: String -> Either Doc NeovimType
161- parseType s = either (throwError . P. text . show ) return $ parse (pType <* eof) s s
161+ parseType :: String -> Either ( Doc AnsiStyle ) NeovimType
162+ parseType s = either (throwError . pretty . show ) return $ parse (pType <* eof) s s
162163
163164
164165pType :: P. Parser NeovimType
0 commit comments