Skip to content

Commit

Permalink
Adjust to changes in nvim-hs 1.0.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
saep committed Mar 24, 2018
1 parent 7999798 commit 4811bcb
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 21 deletions.
24 changes: 13 additions & 11 deletions library/Neovim/User/Choice.hs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE OverloadedStrings #-}
{- |
Module : Neovim.User.Choice
Description : Ask the user for an answer
Expand All @@ -15,26 +16,26 @@ module Neovim.User.Choice

import Neovim

import Data.Char (toLower)
import Data.List (isPrefixOf)
import Text.PrettyPrint.ANSI.Leijen as P hiding ((<$>))
import Data.Char (toLower)
import Data.List (isPrefixOf)


-- | Call @inputlist()@ on the neovim side and ask the user for a choice. This
-- function returns 'Nothing' if the user input was invalid or 'Just' the chosen
-- element. The elements are rendered via 'Pretty'.
oneOf :: Pretty a => [a] -> Neovim r st (Maybe a)
oneOf :: [String] -> Neovim env (Maybe String)
oneOf cs = fmap (\i -> cs !! (i-1)) <$> askForIndex (zipWith mkChoice cs [1..])
where
mkChoice c i = docToObject $ int i P.<> text "." <+> pretty c
mkChoice :: String -> Int -> Object
mkChoice c i = toObject $ show i <> ". " <> c


-- | Ask user for a choice and 'Maybe' return the index of that choice
-- (1-based).
askForIndex :: [Object] -> Neovim r st (Maybe Int)
askForIndex :: [Object] -> Neovim env (Maybe Int)
askForIndex cs = vim_call_function "inputlist" [ObjectArray cs] >>= \case
Left e ->
(err . text . show) e
err $ exceptionToDoc e

Right a -> case fromObject a of
Right i | i >= 1 && i <= length cs ->
Expand All @@ -48,7 +49,7 @@ askForIndex cs = vim_call_function "inputlist" [ObjectArray cs] >>= \case


-- | Same as 'oneOf' only that @a@ is constrained by 'Show' insted of 'Pretty'.
oneOfS :: Show a => [a] -> Neovim r st (Maybe a)
oneOfS :: Show a => [a] -> Neovim env (Maybe a)
oneOfS cs = fmap (\i -> cs !! (i-1)) <$> askForIndex (zipWith mkChoice cs [1..])
where
mkChoice c i = toObject $ show (i :: Int) ++ ". " ++ show c
Expand All @@ -58,10 +59,11 @@ oneOfS cs = fmap (\i -> cs !! (i-1)) <$> askForIndex (zipWith mkChoice cs [1..])
-- prefix of @yes@ or @no@ or alternatively aborted the dialog. Defaults to
-- @yes@ for the empty input.
yesOrNo :: String -- ^ Question to the user
-> Neovim r st Bool
-> Neovim env Bool
yesOrNo message = do
spec <- vim_call_function
"inputdialog" $ (message ++ " (Y/n) ") +: "" +: "no" +: []
"inputdialog" $ (message ++ " (Y/n) ")
+: ("" :: String) +: ("no" :: String) +: []
case fmap fromObject spec of
Right (Right s) | map toLower s `isPrefixOf` "yes" ->
return True
Expand Down
6 changes: 3 additions & 3 deletions library/Neovim/User/Input.hs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import System.Directory
input :: String -- ^ Message to display
-> Maybe String -- ^ Input fiiled in
-> Maybe String -- ^ Completion mode
-> Neovim r st (Either NeovimException Object)
-> Neovim env (Either NeovimException Object)
input message mPrefilled mCompletion = vim_call_function "input" $
(message <> " ")
+: maybe "" id mPrefilled
Expand All @@ -35,7 +35,7 @@ input message mPrefilled mCompletion = vim_call_function "input" $
-- If the directory does not exist, ask the usere whether it should be created.
askForDirectory :: String -- ^ Message to put in front
-> Maybe FilePath -- ^ Prefilled text
-> Neovim r st FilePath
-> Neovim env FilePath
askForDirectory message mPrefilled = do
fp <- errOnInvalidResult $ input message mPrefilled (Just "dir")

Expand All @@ -51,6 +51,6 @@ askForDirectory message mPrefilled = do

askForString :: String -- ^ message to put in front
-> Maybe String -- ^ Prefilled text
-> Neovim r st String
-> Neovim env String
askForString message mPrefilled =
errOnInvalidResult $ input message mPrefilled Nothing
10 changes: 5 additions & 5 deletions nvim-hs-contrib.cabal
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: nvim-hs-contrib
version: 0.2.1
version: 1.0.0.0
synopsis: Haskell plugin backend for neovim
description:
Library for nvim-hs.
Expand All @@ -24,14 +24,14 @@ library

-- other-extensions:
build-depends: base >=4.6 && < 5
, nvim-hs >= 0.2.0
, ansi-wl-pprint
, nvim-hs >= 1.0
, prettyprinter
, prettyprinter-ansi-terminal
, bytestring
, data-default
, directory
, exceptions
, filepath
, messagepack >= 0.4
, messagepack
, mtl >= 2.2.1 && < 2.3
, text
, time
Expand Down
7 changes: 5 additions & 2 deletions stack.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
flags: {}
packages:
- '.'
resolver: lts-8.10
resolver: nightly-2018-03-10
extra-deps:
- nvim-hs-0.2.0
- resourcet-1.2.0
- conduit-1.3.0
- conduit-extra-1.3.0
- cereal-conduit-0.8.0

0 comments on commit 4811bcb

Please sign in to comment.