this repo has no description
at main 76 lines 2.1 kB view raw
1open Code_mirror 2 3let lint = Jv.get Jv.global "__CM__lint" 4 5module Action = struct 6 type t = Jv.t 7 8 let create ~name f = 9 let f' view from to_ = 10 let view = Editor.View.of_jv view in 11 let from = Jv.to_int from in 12 let to_ = Jv.to_int to_ in 13 f ~view ~from ~to_ 14 in 15 let o = Jv.obj [||] in 16 Jv.Jstr.set o "name" (Jstr.v name); 17 Jv.set o "apply" (Jv.repr f'); 18 o 19 20 include (Jv.Id : Jv.CONV with type t := t) 21end 22 23module Diagnostic = struct 24 type t = Jv.t 25 26 let from t = Jv.Int.get t "from" 27 let to_ t = Jv.Int.get t "to" 28 29 type severity = Info | Warning | Error 30 31 let severity_of_string = function 32 | "info" -> Info 33 | "warning" -> Warning 34 | "error" -> Error 35 | _ -> raise (Invalid_argument "Unknown severity level") 36 37 let severity_to_string = function 38 | Info -> "info" 39 | Warning -> "warning" 40 | Error -> "error" 41 42 let severity t = 43 Jv.Jstr.get t "severity" |> Jstr.to_string |> severity_of_string 44 45 let create ?source ?actions ~from ~to_ ~severity ~message () = 46 let o = Jv.obj [||] in 47 Jv.Int.set o "from" from; 48 Jv.Int.set o "to" to_; 49 Jv.Jstr.set o "severity" (severity_to_string severity |> Jstr.v); 50 Jv.Jstr.set o "message" (Jstr.v message); 51 Jv.Jstr.set_if_some o "source" (Option.map Jstr.v source); 52 Jv.set_if_some o "actions" (Option.map (Jv.of_array Action.to_jv) actions); 53 o 54 55 let source t = Jv.Jstr.find t "source" 56 let message t = Jv.Jstr.get t "message" 57 let actions t = Option.map (Jv.to_array Action.to_jv) (Jv.find t "actions") 58 59 include (Jv.Id : Jv.CONV with type t := t) 60end 61 62let create ?delay 63 (source : Code_mirror.Editor.View.t -> Diagnostic.t array Fut.t) = 64 let o = 65 match delay with 66 | None -> Jv.obj [||] 67 | Some d -> Jv.obj [| ("delay", Jv.of_int d) |] 68 in 69 let source' view = 70 let fut = 71 Fut.map (Jv.of_array Diagnostic.to_jv) @@ source (Editor.View.of_jv view) 72 in 73 Fut.to_promise ~ok:Fun.id (Fut.map Result.ok fut) 74 in 75 let ext = Jv.call lint "linter" [| Jv.repr source'; o |] in 76 Code_mirror.Extension.of_jv ext