this repo has no description
1open Code_mirror
2module RegExp = RegExp
3
4let autocomplete = Jv.get Jv.global "__CM__autocomplete"
5
6module Completion = struct
7 type t = Jv.t
8
9 include (Jv.Id : Jv.CONV with type t := t)
10
11 let set_if_some_string t s v = Jv.Jstr.set_if_some t s (Option.map Jstr.v v)
12 let set_string t s v = Jv.Jstr.set t s (Jstr.v v)
13
14 let create ~label ?detail ?info ?apply ?type_ ?boost () =
15 let o = Jv.obj [||] in
16 set_string o "label" label;
17 set_if_some_string o "detail" detail;
18 set_if_some_string o "info" info;
19 Jv.set_if_some o "apply" apply;
20 set_if_some_string o "type" type_;
21 Jv.Int.set_if_some o "boost" boost;
22 o
23end
24
25module Context = struct
26 type t = Jv.t
27 (** Completion context *)
28
29 include (Jv.Id : Jv.CONV with type t := t)
30
31 let state t = Jv.get t "state" |> Editor.State.of_jv
32 let pos t = Jv.Int.get t "pos"
33 let explicit t = Jv.Bool.get t "explicit"
34
35 let token_before t types =
36 let jv = Jv.call t "tokenBefore" [| Jv.of_list Jv.of_string types |] in
37 if Jv.is_none jv then None else Some jv
38
39 let match_before t regex =
40 let jv = Jv.call t "matchBefore" [| RegExp.to_jv regex |] in
41 if Jv.is_none jv then None else Some jv
42
43 let aborted t = Jv.Bool.get t "aborted"
44end
45
46module Result = struct
47 type t = Jv.t
48 (** Completion result *)
49
50 include (Jv.Id : Jv.CONV with type t := t)
51
52 let create ~from ?to_ ~options ?span ?filter () =
53 let o = Jv.obj [||] in
54 Jv.Int.set o "from" from;
55 Jv.Int.set_if_some o "to" to_;
56 Jv.set o "options" (Jv.of_list Completion.to_jv options);
57 Jv.set_if_some o "span" (Option.map RegExp.to_jv span);
58 Jv.Bool.set_if_some o "filter" filter;
59 o
60end
61
62module Source = struct
63 type t = Jv.t
64
65 include (Jv.Id : Jv.CONV with type t := t)
66
67 let create (src : Context.t -> Result.t option Fut.t) =
68 let f ctx =
69 let fut = Fut.map (fun v -> Ok v) @@ src (Context.of_jv ctx) in
70 Fut.to_promise fut ~ok:(fun t ->
71 Option.value ~default:Jv.null (Option.map Result.to_jv t))
72 in
73 Jv.repr f
74
75 let from_list (l : Completion.t list) =
76 Jv.call autocomplete "completeFromList" [| Jv.of_jv_list l |] |> of_jv
77end
78
79type config = Jv.t
80
81let config ?activate_on_typing ?override ?max_rendered_options ?default_key_map
82 ?above_cursor ?option_class ?icons ?add_to_options () =
83 let o = Jv.obj [||] in
84 Jv.Bool.set_if_some o "activateOnTyping" activate_on_typing;
85 Jv.set_if_some o "override" (Option.map (fun v -> Jv.of_jv_list v) override);
86 Jv.Int.set_if_some o "maxRenderedOptions" max_rendered_options;
87 Jv.Bool.set_if_some o "defaultKeyMap" default_key_map;
88 Jv.Bool.set_if_some o "aboveCursor" above_cursor;
89 Jv.set_if_some o "optionClass" option_class;
90 Jv.Bool.set_if_some o "icons" icons;
91 Jv.set_if_some o "addToOptions" add_to_options;
92 o
93
94let create ?(config = Jv.null) () =
95 Extension.of_jv @@ Jv.call autocomplete "autocompletion" [| config |]
96
97(* type status = Active | Pending
98
99 let status state =
100
101 val status : Editor.State.t -> status option
102 (** Gets the current completion status *)
103
104 val current_completions : Editor.State.t -> Completion.t list
105 (** Returns the current available completions *)
106
107 val selected_completion : Editor.State.t -> Completion.t option
108 * Returh the currently selected completion if any *)