Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

what type is returned by vim_command ? #84

Closed
teto opened this issue Apr 9, 2020 · 2 comments
Closed

what type is returned by vim_command ? #84

teto opened this issue Apr 9, 2020 · 2 comments

Comments

@teto
Copy link

teto commented Apr 9, 2020

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
Copy link
Member

saep commented Apr 9, 2020

: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
Copy link
Author

teto commented Jun 9, 2020

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

@teto teto closed this as completed Jun 9, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants