this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Initial partial bindings to autocomplete (#4)

* Initial partial bindings to autocomplete

* Add completion creation

* Fix overrides

* Use is_none

authored by

Patrick Ferris and committed by
GitHub
1222cc0a 9cde9873

+302
+109
src/autocomplete/autocomplete.ml
··· 1 + open Code_mirror 2 + 3 + module RegExp = RegExp 4 + let autocomplete = Jv.get Jv.global "__CM__autocomplete" 5 + 6 + module 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 = 12 + Jv.Jstr.set_if_some t s (Option.map Jstr.v v) 13 + 14 + let set_string t s v = 15 + Jv.Jstr.set t s (Jstr.v v) 16 + 17 + let create ~label ?detail ?info ?apply ?type_ ?boost () = 18 + let o = Jv.obj [||] in 19 + set_string o "label" label; 20 + set_if_some_string o "detail" detail; 21 + set_if_some_string o "info" info; 22 + Jv.set_if_some o "apply" apply; 23 + set_if_some_string o "type" type_; 24 + Jv.Int.set_if_some o "boost" boost; 25 + o 26 + 27 + end 28 + 29 + module Context = struct 30 + type t = Jv.t 31 + (** Completion context *) 32 + 33 + include (Jv.Id : Jv.CONV with type t := t) 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 + end 43 + 44 + module Result = struct 45 + type t = Jv.t 46 + (** Completion result *) 47 + 48 + include (Jv.Id : Jv.CONV with type t := t) 49 + 50 + let create ~from ?to_ ~options ?span ?filter () = 51 + let o = Jv.obj [||] in 52 + Jv.Int.set o "from" from; 53 + Jv.Int.set_if_some o "to" to_; 54 + Jv.set o "options" (Jv.of_list Completion.to_jv options); 55 + Jv.set_if_some o "span" (Option.map RegExp.to_jv span); 56 + Jv.Bool.set_if_some o "filter" filter; 57 + o 58 + end 59 + 60 + type source = Context.t -> Result.t option Fut.t 61 + (** A completion source *) 62 + 63 + let source_to_jv (src : source) = 64 + let f ctx = 65 + let fut = Fut.map (fun v -> Ok v) @@ src (Context.of_jv ctx) in 66 + Fut.to_promise ~ok:(fun t -> Option.value ~default:Jv.null (Option.map Result.to_jv t)) fut 67 + in 68 + Jv.repr f 69 + 70 + type config = Jv.t 71 + 72 + let config 73 + ?activate_on_typing 74 + ?override 75 + ?max_rendered_options 76 + ?default_key_map 77 + ?above_cursor 78 + ?option_class 79 + ?icons 80 + ?add_to_options 81 + () = 82 + let o = Jv.obj [||] in 83 + Jv.Bool.set_if_some o "activateOnTyping" activate_on_typing; 84 + Jv.set_if_some o "override" (Option.map (fun v -> Jv.of_list source_to_jv v) override); 85 + Jv.Int.set_if_some o "maxRenderedOptions" max_rendered_options; 86 + Jv.Bool.set_if_some o "defaultKeyMap" default_key_map; 87 + Jv.Bool.set_if_some o "aboveCursor" above_cursor; 88 + Jv.set_if_some o "optionClass" option_class; 89 + Jv.Bool.set_if_some o "icons" icons; 90 + Jv.set_if_some o "addToOptions" add_to_options; 91 + o 92 + 93 + let create ?(config = Jv.null) () = 94 + Extension.of_jv @@ 95 + 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 *) 109 +
+70
src/autocomplete/autocomplete.mli
··· 1 + val autocomplete : Jv.t 2 + (** Global autocomplete value *) 3 + 4 + module RegExp = RegExp 5 + 6 + module Completion : sig 7 + type t 8 + 9 + include Jv.CONV with type t := t 10 + 11 + val create : 12 + label:string -> 13 + ?detail:string -> 14 + ?info:string -> 15 + ?apply:t -> 16 + ?type_:string -> 17 + ?boost:int -> 18 + unit -> t 19 + end 20 + 21 + module Context : sig 22 + type t 23 + (** Completion context *) 24 + 25 + include Jv.CONV with type t := t 26 + 27 + val token_before : t -> string list -> Jv.t option 28 + 29 + val match_before : t -> RegExp.t -> Jv.t option 30 + end 31 + 32 + module Result : sig 33 + type t 34 + (** Completion result *) 35 + 36 + include Jv.CONV with type t := t 37 + 38 + val create : 39 + from:int -> 40 + ?to_:int -> 41 + options:Completion.t list -> 42 + ?span:RegExp.t -> 43 + ?filter:bool -> 44 + unit -> t 45 + (** Creating a new completion result (see {{: https://codemirror.net/6/docs/ref/#autocomplete.CompletionResult} the docs}).*) 46 + end 47 + 48 + type source = Context.t -> Result.t option Fut.t 49 + (** A completion source *) 50 + 51 + 52 + type config 53 + 54 + val config : 55 + ?activate_on_typing:bool -> 56 + ?override:source list -> 57 + ?max_rendered_options:int -> 58 + ?default_key_map:bool -> 59 + ?above_cursor:bool -> 60 + ?option_class:Jv.t -> 61 + ?icons:bool -> 62 + ?add_to_options:Jv.t -> 63 + unit -> 64 + config 65 + (** Configuration options for your autocompleter, see {{: https://codemirror.net/6/docs/ref/#autocomplete.autocompletion^config} the online docs}.*) 66 + 67 + val create : 68 + ?config:config -> unit -> 69 + Code_mirror.Extension.t 70 + (** Autocompleter *)
+6
src/autocomplete/dune
··· 1 + (library 2 + (name autocomplete) 3 + (js_of_ocaml 4 + (javascript_files require.js)) 5 + (public_name code-mirror.autocomplete) 6 + (libraries code-mirror))
+75
src/autocomplete/regExp.ml
··· 1 + open Brr 2 + 3 + type t = Jv.t 4 + 5 + include (Jv.Id : Jv.CONV with type t := t) 6 + 7 + let regexp = Jv.get (Window.to_jv G.window) "RegExp" 8 + 9 + type opts = 10 + | Indices 11 + | Global 12 + | Ignore 13 + | Multiline 14 + | DotAll 15 + | Unicode 16 + | Sticky 17 + 18 + let opts_to_string = function 19 + | Indices -> 20 + "d" 21 + | Global -> 22 + "g" 23 + | Ignore -> 24 + "i" 25 + | Multiline -> 26 + "m" 27 + | DotAll -> 28 + "s" 29 + | Unicode -> 30 + "u" 31 + | Sticky -> 32 + "y" 33 + 34 + let create ?(opts = []) s = 35 + let opts = 36 + match List.length opts with 37 + | 0 -> 38 + Jv.undefined 39 + | _ -> 40 + let options = List.sort_uniq Stdlib.compare opts in 41 + let opt_string = 42 + List.fold_left (fun acc t -> acc ^ opts_to_string t) "" options 43 + in 44 + Jv.of_string opt_string 45 + in 46 + Jv.new' regexp [| Jv.of_string s; opts |] 47 + 48 + type result = Jv.t 49 + 50 + let get_full_string_match res = 51 + let arr = Jv.to_jv_array res in 52 + arr.(0) |> Jv.to_string 53 + 54 + let get_index res = Jv.Int.get res "index" 55 + 56 + let get_indices res = 57 + let jv = Jv.get res "indices" in 58 + match Jv.is_null jv with 59 + | true -> 60 + [] 61 + | false -> 62 + let conv arr = 63 + let indices = Jv.to_array Jv.to_int arr in 64 + indices.(0), indices.(1) 65 + in 66 + Jv.to_list conv jv 67 + 68 + let get_substring_matches res = 69 + let arr = Jv.to_jv_array res in 70 + let length = Array.length arr in 71 + Array.sub arr 1 length |> Array.to_list |> List.map Jv.to_string 72 + 73 + let exec' t s = Jv.to_option Jv.Id.to_jv @@ Jv.call t "exec" [| Jv.of_jstr s |] 74 + 75 + let exec t s = exec' t @@ Jstr.v s
+41
src/autocomplete/regExp.mli
··· 1 + (** A regular expression *) 2 + type t 3 + 4 + include Jv.CONV with type t := t 5 + 6 + type opts = 7 + | Indices 8 + | Global 9 + | Ignore 10 + | Multiline 11 + | DotAll 12 + | Unicode 13 + | Sticky 14 + 15 + val create : ?opts:opts list -> string -> t 16 + (** Create a regular expression from a string. Internally this uses 17 + [new RegExp(s)] which 18 + {{:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/RegExp} 19 + has it's own documentation}. Note we pass noo flags at the moment. *) 20 + 21 + (** The result of executing a regular expression search on a string *) 22 + type result 23 + 24 + val get_full_string_match : result -> string 25 + (** The matched text *) 26 + 27 + val get_index : result -> int 28 + (** 0-based index of the match in the string *) 29 + 30 + val get_indices : result -> (int * int) list 31 + (** Each entry is a substring match of [(start, end_)] *) 32 + 33 + val get_substring_matches : result -> string list 34 + (** The matches for the parennthetical capture groups *) 35 + 36 + val exec : t -> string -> result option 37 + (** [exec t s] using the regular expression [t] to execute a search for a match 38 + in a specified string [s]. *) 39 + 40 + val exec' : t -> Jstr.t -> result option 41 + (** Same as {!exec} only you can pass a {!Jstr.t} instead. *)
+1
src/autocomplete/require.js
··· 1 + joo_global_object.__CM__autocomplete = require('@codemirror/autocomplete');