···11+open Code_mirror
22+33+module RegExp = RegExp
44+let autocomplete = Jv.get Jv.global "__CM__autocomplete"
55+66+module Completion = struct
77+ type t = Jv.t
88+99+ include (Jv.Id : Jv.CONV with type t := t)
1010+1111+ let set_if_some_string t s v =
1212+ Jv.Jstr.set_if_some t s (Option.map Jstr.v v)
1313+1414+ let set_string t s v =
1515+ Jv.Jstr.set t s (Jstr.v v)
1616+1717+ let create ~label ?detail ?info ?apply ?type_ ?boost () =
1818+ let o = Jv.obj [||] in
1919+ set_string o "label" label;
2020+ set_if_some_string o "detail" detail;
2121+ set_if_some_string o "info" info;
2222+ Jv.set_if_some o "apply" apply;
2323+ set_if_some_string o "type" type_;
2424+ Jv.Int.set_if_some o "boost" boost;
2525+ o
2626+2727+end
2828+2929+module Context = struct
3030+ type t = Jv.t
3131+ (** Completion context *)
3232+3333+ include (Jv.Id : Jv.CONV with type t := t)
3434+3535+ let token_before t types =
3636+ let jv = Jv.call t "tokenBefore" [| Jv.of_list Jv.of_string types |] in
3737+ if Jv.is_none jv then None else Some jv
3838+3939+ let match_before t regex =
4040+ let jv = Jv.call t "matchBefore" [| RegExp.to_jv regex |] in
4141+ if Jv.is_none jv then None else Some jv
4242+end
4343+4444+module Result = struct
4545+ type t = Jv.t
4646+ (** Completion result *)
4747+4848+ include (Jv.Id : Jv.CONV with type t := t)
4949+5050+ let create ~from ?to_ ~options ?span ?filter () =
5151+ let o = Jv.obj [||] in
5252+ Jv.Int.set o "from" from;
5353+ Jv.Int.set_if_some o "to" to_;
5454+ Jv.set o "options" (Jv.of_list Completion.to_jv options);
5555+ Jv.set_if_some o "span" (Option.map RegExp.to_jv span);
5656+ Jv.Bool.set_if_some o "filter" filter;
5757+ o
5858+end
5959+6060+type source = Context.t -> Result.t option Fut.t
6161+(** A completion source *)
6262+6363+let source_to_jv (src : source) =
6464+ let f ctx =
6565+ let fut = Fut.map (fun v -> Ok v) @@ src (Context.of_jv ctx) in
6666+ Fut.to_promise ~ok:(fun t -> Option.value ~default:Jv.null (Option.map Result.to_jv t)) fut
6767+ in
6868+ Jv.repr f
6969+7070+type config = Jv.t
7171+7272+let config
7373+ ?activate_on_typing
7474+ ?override
7575+ ?max_rendered_options
7676+ ?default_key_map
7777+ ?above_cursor
7878+ ?option_class
7979+ ?icons
8080+ ?add_to_options
8181+ () =
8282+ let o = Jv.obj [||] in
8383+ Jv.Bool.set_if_some o "activateOnTyping" activate_on_typing;
8484+ Jv.set_if_some o "override" (Option.map (fun v -> Jv.of_list source_to_jv v) override);
8585+ Jv.Int.set_if_some o "maxRenderedOptions" max_rendered_options;
8686+ Jv.Bool.set_if_some o "defaultKeyMap" default_key_map;
8787+ Jv.Bool.set_if_some o "aboveCursor" above_cursor;
8888+ Jv.set_if_some o "optionClass" option_class;
8989+ Jv.Bool.set_if_some o "icons" icons;
9090+ Jv.set_if_some o "addToOptions" add_to_options;
9191+ o
9292+9393+let create ?(config = Jv.null) () =
9494+ Extension.of_jv @@
9595+ Jv.call autocomplete "autocompletion" [| config |]
9696+9797+(* type status = Active | Pending
9898+9999+let status state =
100100+101101+val status : Editor.State.t -> status option
102102+(** Gets the current completion status *)
103103+104104+val current_completions : Editor.State.t -> Completion.t list
105105+(** Returns the current available completions *)
106106+107107+val selected_completion : Editor.State.t -> Completion.t option
108108+* Returh the currently selected completion if any *)
109109+
+70
src/autocomplete/autocomplete.mli
···11+val autocomplete : Jv.t
22+(** Global autocomplete value *)
33+44+module RegExp = RegExp
55+66+module Completion : sig
77+ type t
88+99+ include Jv.CONV with type t := t
1010+1111+ val create :
1212+ label:string ->
1313+ ?detail:string ->
1414+ ?info:string ->
1515+ ?apply:t ->
1616+ ?type_:string ->
1717+ ?boost:int ->
1818+ unit -> t
1919+end
2020+2121+module Context : sig
2222+ type t
2323+ (** Completion context *)
2424+2525+ include Jv.CONV with type t := t
2626+2727+ val token_before : t -> string list -> Jv.t option
2828+2929+ val match_before : t -> RegExp.t -> Jv.t option
3030+end
3131+3232+module Result : sig
3333+ type t
3434+ (** Completion result *)
3535+3636+ include Jv.CONV with type t := t
3737+3838+ val create :
3939+ from:int ->
4040+ ?to_:int ->
4141+ options:Completion.t list ->
4242+ ?span:RegExp.t ->
4343+ ?filter:bool ->
4444+ unit -> t
4545+ (** Creating a new completion result (see {{: https://codemirror.net/6/docs/ref/#autocomplete.CompletionResult} the docs}).*)
4646+end
4747+4848+type source = Context.t -> Result.t option Fut.t
4949+(** A completion source *)
5050+5151+5252+type config
5353+5454+val config :
5555+ ?activate_on_typing:bool ->
5656+ ?override:source list ->
5757+ ?max_rendered_options:int ->
5858+ ?default_key_map:bool ->
5959+ ?above_cursor:bool ->
6060+ ?option_class:Jv.t ->
6161+ ?icons:bool ->
6262+ ?add_to_options:Jv.t ->
6363+ unit ->
6464+ config
6565+ (** Configuration options for your autocompleter, see {{: https://codemirror.net/6/docs/ref/#autocomplete.autocompletion^config} the online docs}.*)
6666+6767+val create :
6868+ ?config:config -> unit ->
6969+ Code_mirror.Extension.t
7070+ (** Autocompleter *)
···11+open Brr
22+33+type t = Jv.t
44+55+include (Jv.Id : Jv.CONV with type t := t)
66+77+let regexp = Jv.get (Window.to_jv G.window) "RegExp"
88+99+type opts =
1010+ | Indices
1111+ | Global
1212+ | Ignore
1313+ | Multiline
1414+ | DotAll
1515+ | Unicode
1616+ | Sticky
1717+1818+let opts_to_string = function
1919+ | Indices ->
2020+ "d"
2121+ | Global ->
2222+ "g"
2323+ | Ignore ->
2424+ "i"
2525+ | Multiline ->
2626+ "m"
2727+ | DotAll ->
2828+ "s"
2929+ | Unicode ->
3030+ "u"
3131+ | Sticky ->
3232+ "y"
3333+3434+let create ?(opts = []) s =
3535+ let opts =
3636+ match List.length opts with
3737+ | 0 ->
3838+ Jv.undefined
3939+ | _ ->
4040+ let options = List.sort_uniq Stdlib.compare opts in
4141+ let opt_string =
4242+ List.fold_left (fun acc t -> acc ^ opts_to_string t) "" options
4343+ in
4444+ Jv.of_string opt_string
4545+ in
4646+ Jv.new' regexp [| Jv.of_string s; opts |]
4747+4848+type result = Jv.t
4949+5050+let get_full_string_match res =
5151+ let arr = Jv.to_jv_array res in
5252+ arr.(0) |> Jv.to_string
5353+5454+let get_index res = Jv.Int.get res "index"
5555+5656+let get_indices res =
5757+ let jv = Jv.get res "indices" in
5858+ match Jv.is_null jv with
5959+ | true ->
6060+ []
6161+ | false ->
6262+ let conv arr =
6363+ let indices = Jv.to_array Jv.to_int arr in
6464+ indices.(0), indices.(1)
6565+ in
6666+ Jv.to_list conv jv
6767+6868+let get_substring_matches res =
6969+ let arr = Jv.to_jv_array res in
7070+ let length = Array.length arr in
7171+ Array.sub arr 1 length |> Array.to_list |> List.map Jv.to_string
7272+7373+let exec' t s = Jv.to_option Jv.Id.to_jv @@ Jv.call t "exec" [| Jv.of_jstr s |]
7474+7575+let exec t s = exec' t @@ Jstr.v s
+41
src/autocomplete/regExp.mli
···11+(** A regular expression *)
22+type t
33+44+include Jv.CONV with type t := t
55+66+type opts =
77+ | Indices
88+ | Global
99+ | Ignore
1010+ | Multiline
1111+ | DotAll
1212+ | Unicode
1313+ | Sticky
1414+1515+val create : ?opts:opts list -> string -> t
1616+(** Create a regular expression from a string. Internally this uses
1717+ [new RegExp(s)] which
1818+ {{:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/RegExp}
1919+ has it's own documentation}. Note we pass noo flags at the moment. *)
2020+2121+(** The result of executing a regular expression search on a string *)
2222+type result
2323+2424+val get_full_string_match : result -> string
2525+(** The matched text *)
2626+2727+val get_index : result -> int
2828+(** 0-based index of the match in the string *)
2929+3030+val get_indices : result -> (int * int) list
3131+(** Each entry is a substring match of [(start, end_)] *)
3232+3333+val get_substring_matches : result -> string list
3434+(** The matches for the parennthetical capture groups *)
3535+3636+val exec : t -> string -> result option
3737+(** [exec t s] using the regular expression [t] to execute a search for a match
3838+ in a specified string [s]. *)
3939+4040+val exec' : t -> Jstr.t -> result option
4141+(** Same as {!exec} only you can pass a {!Jstr.t} instead. *)