this repo has no description

Merge pull request #5 from voodoos/more-bindings

Add more bindings to the autocomplete module and better documentation

authored by

Patrick Ferris and committed by
GitHub
9940f149 1d46095a

+100 -19
+27 -12
src/autocomplete/autocomplete.ml
··· 21 21 set_if_some_string o "info" info; 22 22 Jv.set_if_some o "apply" apply; 23 23 set_if_some_string o "type" type_; 24 - Jv.Int.set_if_some o "boost" boost; 24 + Jv.Int.set_if_some o "boost" boost; 25 25 o 26 26 27 27 end ··· 32 32 33 33 include (Jv.Id : Jv.CONV with type t := t) 34 34 35 + let state t = Jv.get t "state" |> Editor.State.of_jv 36 + 37 + let pos t = Jv.Int.get t "pos" 38 + 39 + let explicit t = Jv.Bool.get t "explicit" 40 + 35 41 let token_before t types = 36 42 let jv = Jv.call t "tokenBefore" [| Jv.of_list Jv.of_string types |] in 37 43 if Jv.is_none jv then None else Some jv ··· 39 45 let match_before t regex = 40 46 let jv = Jv.call t "matchBefore" [| RegExp.to_jv regex |] in 41 47 if Jv.is_none jv then None else Some jv 48 + 49 + let aborted t = Jv.Bool.get t "aborted" 42 50 end 43 51 44 52 module Result = struct ··· 57 65 o 58 66 end 59 67 60 - type source = Context.t -> Result.t option Fut.t 61 - (** A completion source *) 68 + module Source = struct 69 + type t = Jv.t 62 70 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 71 + include (Jv.Id : Jv.CONV with type t := t) 72 + 73 + let create (src : Context.t -> Result.t option Fut.t) = 74 + let f ctx = 75 + let fut = Fut.map (fun v -> Ok v) @@ src (Context.of_jv ctx) in 76 + Fut.to_promise fut 77 + ~ok:(fun t -> 78 + Option.value ~default:Jv.null (Option.map Result.to_jv t)) 79 + in 68 80 Jv.repr f 69 81 82 + let from_list (l : Completion.t list) = 83 + Jv.call autocomplete "completeFromList" [| Jv.of_jv_list l |] |> of_jv 84 + end 85 + 70 86 type config = Jv.t 71 87 72 - let config 88 + let config 73 89 ?activate_on_typing 74 90 ?override 75 91 ?max_rendered_options ··· 81 97 () = 82 98 let o = Jv.obj [||] in 83 99 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); 100 + Jv.set_if_some o "override" (Option.map (fun v -> Jv.of_jv_list v) override); 85 101 Jv.Int.set_if_some o "maxRenderedOptions" max_rendered_options; 86 102 Jv.Bool.set_if_some o "defaultKeyMap" default_key_map; 87 103 Jv.Bool.set_if_some o "aboveCursor" above_cursor; ··· 96 112 97 113 (* type status = Active | Pending 98 114 99 - let status state = 115 + let status state = 100 116 101 117 val status : Editor.State.t -> status option 102 118 (** Gets the current completion status *) ··· 106 122 107 123 val selected_completion : Editor.State.t -> Completion.t option 108 124 * Returh the currently selected completion if any *) 109 -
+73 -7
src/autocomplete/autocomplete.mli
··· 1 + open Code_mirror 2 + 3 + (** Most of this documention originate from the code-mirror reference. 4 + 5 + {{:https://codemirror.net/6/docs/ref/#autocomplete} Visit the 6 + reference directly for additional information.} *) 7 + 1 8 val autocomplete : Jv.t 2 9 (** Global autocomplete value *) 3 10 4 11 module RegExp = RegExp 5 12 6 13 module Completion : sig 14 + (** Represents individual completions. *) 15 + 7 16 type t 17 + (** Completion *) 8 18 9 19 include Jv.CONV with type t := t 10 20 11 21 val create : 12 22 label:string -> 13 23 ?detail:string -> 14 - ?info:string -> 24 + ?info:string -> 15 25 ?apply:t -> 16 26 ?type_:string -> 17 27 ?boost:int -> 18 28 unit -> t 29 + (** Creates a completion. 30 + 31 + @param label The label to show in the completion picker. 32 + @param detail An optional short piece of information to show after the 33 + label. 34 + @param info Additional info to show when the completion is selected. 35 + @param apply (todo) How to apply the completion. 36 + @param type The type of the completion. This is used to pick an icon to 37 + show for the completion. 38 + @param boost 39 + 40 + {{:https://codemirror.net/6/docs/ref/#autocomplete.Completion} See the 41 + reference for additional information.} *) 19 42 end 20 43 21 44 module Context : sig 45 + (** An instance of this is passed to completion source functions. *) 46 + 22 47 type t 23 48 (** Completion context *) 24 49 25 50 include Jv.CONV with type t := t 26 51 52 + val state : t -> Editor.State.t 53 + (** The editor state that the completion happens in. *) 54 + 55 + val pos : t -> int 56 + (** The position at which the completion is happening. *) 57 + 58 + val explicit : t -> bool 59 + (** Indicates whether completion was activated explicitly, or implicitly by 60 + typing. The usual way to respond to this is to only return completions when 61 + either there is part of a completable entity before the cursor, or explicit 62 + is true. *) 63 + 27 64 val token_before : t -> string list -> Jv.t option 65 + (** Get the extent, content, and (if there is a token) type of the token 66 + before this.pos. *) 28 67 29 68 val match_before : t -> RegExp.t -> Jv.t option 69 + (** Get the match of the given expression directly before the cursor. *) 70 + 71 + val aborted : t -> bool 72 + (** Yields true when the query has been aborted. Can be useful in 73 + asynchronous queries to avoid doing work that will be ignored. *) 30 74 end 31 75 32 76 module Result : sig 77 + (** Objects returned by completion sources. *) 78 + 33 79 type t 34 80 (** Completion result *) 35 81 ··· 42 88 ?span:RegExp.t -> 43 89 ?filter:bool -> 44 90 unit -> t 45 - (** Creating a new completion result (see {{: https://codemirror.net/6/docs/ref/#autocomplete.CompletionResult} the docs}).*) 91 + (** Creating a new completion result (see {{: https://codemirror.net/6/docs/ref/#autocomplete.CompletionResult} the docs}). 92 + @param from The start of the range that is being completed. 93 + @param to_ The end of the range that is being completed. Defaults to the 94 + main cursor position. 95 + @param options The completions returned. 96 + @param span When given, further input that causes the part of the document 97 + between [from] and [to_] to match this regular expression will not query 98 + the completion source again 99 + @param filter By default, the library filters and scores completions. Set 100 + filter to false to disable this, and cause your completions to all be 101 + included, in the order they were given. 102 + *) 46 103 end 47 104 48 - type source = Context.t -> Result.t option Fut.t 49 - (** A completion source *) 105 + module Source : sig 106 + type t 107 + (** Completion source *) 50 108 109 + include Jv.CONV with type t := t 110 + 111 + val create : (Context.t -> Result.t option Fut.t) -> t 112 + 113 + val from_list : Completion.t list -> t 114 + (** Given a a fixed array of options, return an autocompleter that completes 115 + them. *) 116 + end 51 117 52 118 type config 53 119 54 - val config : 55 - ?activate_on_typing:bool -> 56 - ?override:source list -> 120 + val config : 121 + ?activate_on_typing:bool -> 122 + ?override:Source.t list -> 57 123 ?max_rendered_options:int -> 58 124 ?default_key_map:bool -> 59 125 ?above_cursor:bool ->