···11jsoo-code-mirror
22----------------
3344-Some bindings using [brr]() to [code-mirror 6](https://codemirror.net/6/).44+*Very, very incomplete*
55+66+Some bindings using [brr](https://erratique.ch/software/brr) to [code-mirror 6](https://codemirror.net/6/).
+4
src/editor.ml
···1818 let create ?(config = Jv.undefined) () =
1919 let editor_state = Jv.get Jv.global "__CM__state" in
2020 Jv.call editor_state "create" [| config |]
2121+2222+ let doc t = Jv.get t "doc" |> Text.of_jv
2123end
22242325module View = struct
···37393840 let create ?(opts = Jv.undefined) () =
3941 Jv.new' (Jv.get Jv.global "__CM__view") [| opts |]
4242+4343+ let state t = Jv.get t "state" |> State.of_jv
40444145 (* TODO *)
4246 module Update = struct
+5
src/editor.mli
···1616 end
17171818 val create : ?config:Config.t -> unit -> t
1919+2020+ val doc : t -> Text.t
1921end
20222123module View : sig
···38403941 val create : ?opts:opts -> unit -> t
4042 (** Create a new view *)
4343+4444+ val state : t -> State.t
4545+ (** Current editor state *)
41464247 module Update : sig
4348 type t
···11+open Code_mirror
22+33+let lint = Jv.get Jv.global "__CM__lint"
44+55+module Action = struct
66+ type t = Jv.t
77+88+ let create ~name f =
99+ let f' view from to_ =
1010+ let view = Editor.View.of_jv view in
1111+ let from = Jv.to_int from in
1212+ let to_ = Jv.to_int to_ in
1313+ f ~view ~from ~to_
1414+ in
1515+ let o = Jv.obj [||] in
1616+ Jv.Jstr.set o "name" (Jstr.v name);
1717+ Jv.set o "apply" (Jv.repr f');
1818+ o
1919+2020+ include (Jv.Id : Jv.CONV with type t := t)
2121+end
2222+2323+module Diagnostic = struct
2424+ type t = Jv.t
2525+2626+ let from t = Jv.Int.get t "from"
2727+2828+ let to_ t = Jv.Int.get t "to"
2929+3030+ type severity = Info | Warning | Error
3131+3232+ let severity_of_string = function
3333+ | "info" -> Info
3434+ | "warning" -> Warning
3535+ | "error" -> Error
3636+ | _ -> raise (Invalid_argument "Unknown severity level")
3737+3838+ let severity_to_string = function
3939+ | Info -> "info"
4040+ | Warning -> "warning"
4141+ | Error -> "error"
4242+4343+ let severity t =
4444+ Jv.Jstr.get t "severity" |> Jstr.to_string |> severity_of_string
4545+4646+ let create ?source ?actions ~from ~to_ ~severity ~message () =
4747+ let o = Jv.obj [||] in
4848+ Jv.Int.set o "from" from;
4949+ Jv.Int.set o "to" to_;
5050+ Jv.Jstr.set o "severity" (severity_to_string severity |> Jstr.v);
5151+ Jv.Jstr.set o "message" (Jstr.v message);
5252+ Jv.Jstr.set_if_some o "source" (Option.map Jstr.v source);
5353+ Jv.set_if_some o "actions" (Option.map (Jv.of_array Action.to_jv) actions);
5454+ o
5555+5656+ let source t = Jv.Jstr.find t "source"
5757+5858+ let message t = Jv.Jstr.get t "message"
5959+6060+ let actions t = Option.map (Jv.to_array Action.to_jv) (Jv.find t "actions")
6161+6262+ include (Jv.Id : Jv.CONV with type t := t)
6363+end
6464+6565+let create ?delay
6666+ (source : Code_mirror.Editor.View.t -> Diagnostic.t array Fut.t) =
6767+ let o =
6868+ match delay with
6969+ | None -> Jv.obj [||]
7070+ | Some d -> Jv.obj [| ("delay", Jv.of_int d) |]
7171+ in
7272+ let source' view =
7373+ let fut =
7474+ Fut.map (Jv.of_array Diagnostic.to_jv) @@ source (Editor.View.of_jv view)
7575+ in
7676+ Fut.to_promise ~ok:Fun.id (Fut.map Result.ok fut)
7777+ in
7878+ let ext = Jv.call lint "linter" [| Jv.repr source'; o |] in
7979+ Code_mirror.Extension.of_jv ext
+50
src/lint/lint.mli
···11+open Code_mirror
22+33+val lint : Jv.t
44+(** Global lint value *)
55+66+module Action : sig
77+ type t
88+ (** The type for actions associated with a diagnostic *)
99+1010+ val create :
1111+ name:string -> (view:Editor.View.t -> from:int -> to_:int -> unit) -> t
1212+ (** [create ~name f] makes a new action with a function to call when the user activates the action *)
1313+end
1414+1515+module Diagnostic : sig
1616+ type t
1717+1818+ type severity = Info | Warning | Error
1919+2020+ val severity_of_string : string -> severity
2121+2222+ val severity_to_string : severity -> string
2323+2424+ val create :
2525+ ?source:string ->
2626+ ?actions:t array ->
2727+ from:int ->
2828+ to_:int ->
2929+ severity:severity ->
3030+ message:string ->
3131+ unit ->
3232+ t
3333+3434+ val severity : t -> severity
3535+3636+ val from : t -> int
3737+3838+ val to_ : t -> int
3939+4040+ val source : t -> Jstr.t option
4141+4242+ val actions : t -> Action.t array option
4343+4444+ val message : t -> Jstr.t
4545+end
4646+4747+val create :
4848+ ?delay:int ->
4949+ (Editor.View.t -> Diagnostic.t array Fut.t) ->
5050+ Code_mirror.Extension.t
···11+type t = Jv.t
22+33+include (Jv.Id : Jv.CONV with type t := t)
44+15module Line = struct
26 type t = Jv.t
37···11151216 let length t = Jv.Int.get t "length"
1317end
1818+1919+let length t = Jv.Int.get t "length"
2020+2121+let line n t = Jv.call t "line" [| Jv.of_int n |]
2222+2323+let to_jstr_array t = Jv.call t "toJSON" [||] |> Jv.to_jstr_array
+11
src/text.mli
···11+type t
22+33+include Jv.CONV with type t := t
44+15module Line : sig
26 type t
37 (** A text line *)
···1721 val length : t -> int
1822 (** The length of the line *)
1923end
2424+2525+val length : t -> int
2626+(** Length of the text *)
2727+2828+val line : int -> t -> Line.t
2929+3030+val to_jstr_array : t -> Jstr.t array