Source file api_call.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
open Core
open Async

type _ t =
  | Single : 'a Nvim_internal.Types.api_result -> 'a Or_error.t t
  | Map : ('a -> 'b) * 'a t -> 'b t
  | Map_bind : ('a -> 'b Or_error.t) * 'a Or_error.t t -> 'b Or_error.t t
  | Pair : 'a t * 'b t -> ('a * 'b) t
  | Const : 'a -> 'a t

let of_api_result x = Single x

let call_atomic { Types.call_nvim_api_fn; _ } ~calls =
  Nvim_internal.Wrappers.nvim_call_atomic ~calls |> call_nvim_api_fn
;;

let rec collect_calls : type a. a t -> Msgpack.t list =
  let open Nvim_internal.Types in
  function
  | Single { name; params; _ } -> [ Msgpack.Array [ String name; params ] ]
  | Map (_, c) -> collect_calls c
  | Map_bind (_, c) -> collect_calls c
  | Pair (c1, c2) -> collect_calls c1 @ collect_calls c2
  | Const _ -> []
;;

let rec extract_results
  : type a. Msgpack.t list -> a t -> Types.client -> a * Msgpack.t list
  =
  fun l shape cli ->
  let open Nvim_internal.Types in
  match l, shape with
  | l, Const x -> x, l
  | [], Single _ ->
    Or_error.error_string "got bad response from vim: wrong number of responses", []
  | [], Map _ -> failwith "got bad response from vim: wrong number of responses"
  | [], Pair _ -> failwith "got bad response from vim: wrong number of responses"
  | obj :: rest, Single { witness; _ } -> Extract.value witness obj, rest
  | l, Map (f, c) ->
    let obj, rest = extract_results l c cli in
    f obj, rest
  | l, Map_bind (f, c) ->
    let obj, rest = extract_results l c cli in
    Or_error.bind ~f obj, rest
  | l, Pair (a, b) ->
    let left, remaining = extract_results l a cli in
    let right, rest = extract_results remaining b cli in
    (left, right), rest
;;

let rec run : type a. Types.client -> a t -> a Or_error.t Deferred.t =
  fun ({ Types.call_nvim_api_fn; _ } as client) res ->
  match res with
  | Const x -> return (Ok x)
  | Single api ->
    let%map result = call_nvim_api_fn api in
    Ok result
  | Map (f, c) -> run client c |> Deferred.Or_error.map ~f
  | Map_bind (f, c) ->
    let%map result = run client c in
    Or_error.map ~f:(Or_error.bind ~f) result
  | Pair _ ->
    let calls = collect_calls res in
    (match%map call_atomic client ~calls with
     | Error _ as e -> e
     | Ok [ Msgpack.Array results; _err ] ->
       let r = Or_error.try_with (fun () -> extract_results results res client) in
       Or_error.map ~f:Tuple2.get1 r
     | _ -> Or_error.error_string "got bad response from vim: bad format")
;;

let run_join cli t = run cli t >>| Or_error.join
let map_bind x ~f = Map_bind (f, x)
let return x = Const x
let both x y = Pair (x, y)

(* Api_call.t is an applicative which means that it can be defined either as
   the tripple [map, both, return] or via [map, apply, return].  Because
   [Api_call.t] never contain functions in practice (though it is possible via
   [return]), it is easier to think about as an implementation based off both.
   We define the operation [both : 'a api_call -> 'b api_call -> ('a * 'b)
   api_call], which atomically receives its values from neovim before combining
   them. *)
module Let_syntax = struct
  module Let_syntax = struct
    let map x ~f = Map (f, x)
    let both x y = Pair (x, y)
    let return x = Const x
  end
end