neovimhaskell / nvim-hs

Neovim API for Haskell plugins as well as the plugin provider
Other
267 stars 18 forks source link

what type is returned by vim_command ? #84

Closed teto closed 4 years ago

teto commented 4 years ago

Hi, I am still hesitant when it comes to haskell, especially when TemplateHaskell is involved. I would like to parse the output of menu_get("") from my haskell plugin but not sure how

let json = vim_command "menu_get('')"
    -- case AE.eitherDecode json of
    --   Left err -> err $ "could not decode the output of menu_get"
    --   Right menuEntries -> return
saep commented 4 years ago

:t vim_command

vim_command :: String -> Neovim env ()

vim_command returns no value as indicated by the unit () in the type.

If you start a ghci session from within neovim (i.e. starting a terminal with :term and then run stack ghci), then you can call remote functions and see what they return. For example calling menu_get with an empty String as the only argument can be achieved by the following:

import Neovim.Debug import qualified Neovim.API.String as Str

debug' $ Str.nvim_call_function "menu_get" ("" +: [])

Right (ObjectArray [])

:t Str.nvim_call_function Str.nvim_call_function :: String -> [Object] -> Neovim env Object

:+ prepends a value to a list just like : except that it additionally converts it to a messagepack Object.

Anyhow, the result is a messagepack object, which is very similar to a JSON object. It is already a structured object and you can pattern match on it to extract the values you want.

Alternatively, you can implement an NvimObject instance which requires the data type to have FromJSON and ToJSON instances. Then you can use Aeson's mechanisms to parse the object as if it were JSON. This would also implement #74 :-)

teto commented 4 years ago

thanks. I am a working towards building up my haskell skills but this is super helpful. Thanks !