diff --git a/.github/workflows/workflow.yml b/.github/workflows/workflow.yml index 1ab3ffe..03f77ba 100644 --- a/.github/workflows/workflow.yml +++ b/.github/workflows/workflow.yml @@ -12,7 +12,9 @@ jobs: os: - ubuntu-latest ocaml-version: - - 5.1.0~rc1 + - 5.3.0~alpha1 + - 5.2.0 + - 5.1.1 - 5.0.0 - 4.14.0 - 4.13.1 diff --git a/libs/indexBuild.ml b/libs/indexBuild.ml index a40a5e4..7eb87b8 100644 --- a/libs/indexBuild.ml +++ b/libs/indexBuild.ml @@ -31,6 +31,10 @@ type parents = (string list * t Lazy.t) list open IndexMisc +#if OCAML_VERSION >= (5,3,0) + module Printtyp = Out_type +#endif + let orig_file_name = function | Cmt f | Cmti f | Cmi f -> f @@ -246,7 +250,11 @@ let qualify_ty (parents:parents) ty = Otyp_object (List.map (fun (str,ty) -> str, aux ty) strtylist, blopt) #endif | Otyp_record (strbltylist) -> +#if OCAML_VERSION >= (5,3,0) + Otyp_record (List.map (fun {olab_name; olab_mut; olab_type} -> {olab_name; olab_mut; olab_type = aux olab_type}) strbltylist) +#else Otyp_record (List.map (fun (str,bl,ty) -> str, bl, aux ty) strbltylist) +#endif | Otyp_stuff str -> Otyp_stuff str | Otyp_sum (strtylisttyoptlist) -> Otyp_sum @@ -420,7 +428,9 @@ let doc_of_attributes attrs = | _, PStr [{pstr_desc = Pstr_eval ({pexp_desc},_)}] -> #endif (match pexp_desc with -#if OCAML_VERSION >= (4,11,0) +#if OCAML_VERSION >= (5,3,0) + | Pexp_constant {pconst_desc = Pconst_string (s,_,_); _} -> Some s +#elif OCAML_VERSION >= (4,11,0) | Pexp_constant (Pconst_string (s,_,_)) -> Some s #elif OCAML_VERSION >= (4,03,0) | Pexp_constant (Pconst_string (s,_)) -> Some s @@ -533,12 +543,20 @@ let trie_of_type_decl ?comments info ty_decl = Outcometree.Otyp_record ( List.map (fun l -> +#if OCAML_VERSION >= (5,3,0) + { + Outcometree.olab_name = Ident.name l.Types.ld_id; + olab_mut = l.ld_mutable; + olab_type = Printtyp.tree_of_typexp Printtyp.Type l.ld_type; + } +#else (Ident.name l.Types.ld_id, l.ld_mutable = Mutable, #if OCAML_VERSION >= (4,14,0) Printtyp.tree_of_typexp Printtyp.Type l.ld_type) #else Printtyp.tree_of_typexp false l.ld_type) +#endif #endif ) params) diff --git a/libs/indexOut.ml b/libs/indexOut.ml index b7f2cd9..4ee716b 100644 --- a/libs/indexOut.ml +++ b/libs/indexOut.ml @@ -38,9 +38,9 @@ module IndexFormat = struct | [] -> () | [x] -> left fmt; pr fmt x; right fmt | _::_::_ -> - if paren then Format.pp_print_char fmt '('; + if paren then Format_doc.pp_print_char fmt '('; left fmt; aux lst; right fmt; - if paren then Format.pp_print_char fmt ')' + if paren then Format_doc.pp_print_char fmt ')' let lines ?(escaped=false) fmt str = let len = String.length str in @@ -48,17 +48,17 @@ module IndexFormat = struct let rec aux i = if i >= len then () else let j = try String.index_from str i '\n' with Not_found -> len in - Format.pp_print_string fmt + Format_doc.pp_print_string fmt (esc (String.trim (String.sub str i (j - i)))); if j < len - 1 then - (Format.pp_force_newline fmt (); + (Format_doc.pp_force_newline fmt (); aux (j+1)) in aux 0 type coloriser = { f: 'a. kind -> - ('a, Format.formatter, unit) format -> Format.formatter + ('a, Format_doc.formatter, unit) format -> Format_doc.formatter -> 'a } let color = @@ -74,12 +74,12 @@ module IndexFormat = struct | Class | ClassType -> "\027[35m" | Keyword -> "\027[32m" in - Format.pp_print_as fmt 0 colorcode; - Format.kfprintf (fun fmt -> Format.pp_print_as fmt 0 "\027[m") fmt fstr + Format_doc.pp_print_as fmt 0 colorcode; + Format_doc.kfprintf (fun fmt -> Format_doc.pp_print_as fmt 0 "\027[m") fmt fstr in { f } let no_color = - let f _ fstr fmt = Format.fprintf fmt fstr in + let f _ fstr fmt = Format_doc.fprintf fmt fstr in { f } let name ?(colorise = no_color) fmt id = @@ -87,50 +87,58 @@ module IndexFormat = struct let path ?(short = false) ?(colorise = no_color) fmt id = List.iter - (Format.fprintf fmt "%a." (colorise.f Module "%s")) + (Format_doc.fprintf fmt "%a." (colorise.f Module "%s")) (if short then id.path else id.orig_path); name ~colorise fmt id let kind ?(colorise = no_color) fmt id = match id.kind with - | OpenType -> Format.pp_print_string fmt "opentype" - | Type -> Format.pp_print_string fmt "type" - | Value -> Format.pp_print_string fmt "val" - | Exception -> Format.pp_print_string fmt "exception" + | OpenType -> Format_doc.pp_print_string fmt "opentype" + | Type -> Format_doc.pp_print_string fmt "type" + | Value -> Format_doc.pp_print_string fmt "val" + | Exception -> Format_doc.pp_print_string fmt "exception" | Field parentty -> - Format.fprintf fmt "field(%a)" + Format_doc.fprintf fmt "field(%a)" (colorise.f parentty.kind "%s") parentty.name | Variant parentty -> - Format.fprintf fmt "constr(%a)" + Format_doc.fprintf fmt "constr(%a)" (colorise.f parentty.kind "%s") parentty.name | Method parentclass -> - Format.fprintf fmt "method(%a)" + Format_doc.fprintf fmt "method(%a)" (colorise.f parentclass.kind "%s") parentclass.name - | Module -> Format.pp_print_string fmt "module" - | ModuleType -> Format.pp_print_string fmt "modtype" - | Class -> Format.pp_print_string fmt "class" - | ClassType -> Format.pp_print_string fmt "classtype" - | Keyword -> Format.pp_print_string fmt "keyword" + | Module -> Format_doc.pp_print_string fmt "module" + | ModuleType -> Format_doc.pp_print_string fmt "modtype" + | Class -> Format_doc.pp_print_string fmt "class" + | ClassType -> Format_doc.pp_print_string fmt "classtype" + | Keyword -> Format_doc.pp_print_string fmt "keyword" let rec tydecl fmt = let open Outcometree in function - | Otyp_abstract -> Format.fprintf fmt "" + | Otyp_abstract -> Format_doc.fprintf fmt "" | Otyp_manifest (ty,_) -> tydecl fmt ty | Otyp_record fields -> +#if OCAML_VERSION >= (5,3,0) + let print_field fmt {olab_name; olab_mut; olab_type} = + Format_doc.fprintf fmt "@[<2>%s%s :@ @[%a@]@];" + (match olab_mut with Mutable -> "mutable " | Immutable -> "") + olab_name + !Oprint.out_type olab_type +#else let print_field fmt (name, mut, arg) = Format.fprintf fmt "@[<2>%s%s :@ @[%a@]@];" (if mut then "mutable " else "") name !Oprint.out_type arg +#endif in - Format.fprintf fmt "@[{%a}@]" + Format_doc.fprintf fmt "@[{%a}@]" (list - ~left:(fun fmt -> Format.pp_print_space fmt ()) - ~right:(fun fmt -> Format.pp_print_break fmt 1 (-2)) - print_field Format.pp_print_space) + ~left:(fun fmt -> Format_doc.pp_print_space fmt ()) + ~right:(fun fmt -> Format_doc.pp_print_break fmt 1 (-2)) + print_field Format_doc.pp_print_space) fields | Otyp_sum [] -> - Format.pp_print_char fmt '-' + Format_doc.pp_print_char fmt '-' | Otyp_sum constrs -> #if OCAML_VERSION >= (4,14,0) let print_variant fmt {Outcometree.ocstr_name = name; ocstr_args = tyl; ocstr_return_type = ret_type_opt} = @@ -139,29 +147,29 @@ module IndexFormat = struct #endif match ret_type_opt with | None -> - if tyl = [] then Format.pp_print_string fmt name + if tyl = [] then Format_doc.pp_print_string fmt name else - Format.fprintf fmt "@[<2>%s of@ @[%a@]@]" + Format_doc.fprintf fmt "@[<2>%s of@ @[%a@]@]" name (list !Oprint.out_type - (fun fmt () -> Format.fprintf fmt " *@ ")) + (fun fmt () -> Format_doc.fprintf fmt " *@ ")) tyl | Some ret_type -> if tyl = [] then - Format.fprintf fmt "@[<2>%s :@ @[%a@]@]" name + Format_doc.fprintf fmt "@[<2>%s :@ @[%a@]@]" name !Oprint.out_type ret_type else - Format.fprintf fmt "@[<2>%s :@ @[%a -> @[%a@]@]@]" + Format_doc.fprintf fmt "@[<2>%s :@ @[%a -> @[%a@]@]@]" name (list !Oprint.out_type - (fun fmt () -> Format.fprintf fmt " *@ ")) + (fun fmt () -> Format_doc.fprintf fmt " *@ ")) tyl !Oprint.out_type ret_type in list print_variant ~left:(fun fmt -> - Format.pp_print_if_newline fmt (); Format.fprintf fmt "| ") - (fun fmt () -> Format.fprintf fmt "@ | ") + Format_doc.pp_print_if_newline fmt (); Format_doc.fprintf fmt "| ") + (fun fmt () -> Format_doc.fprintf fmt "@ | ") fmt constrs | ty -> !Oprint.out_type fmt ty @@ -173,12 +181,12 @@ module IndexFormat = struct | Osig_class_type (_,_,_,ctyp,_) -> !Oprint.out_class_type fmt ctyp | Osig_typext ({ oext_args = [] }, _) -> - Format.pp_print_char fmt '-' + Format_doc.pp_print_char fmt '-' | Osig_typext ({ oext_args }, _) -> list ~paren:true !Oprint.out_type (fun fmt () -> - Format.pp_print_char fmt ','; Format.pp_print_space fmt ()) + Format_doc.pp_print_char fmt ','; Format_doc.pp_print_space fmt ()) fmt oext_args | Osig_modtype (_,mtyp) @@ -190,7 +198,7 @@ module IndexFormat = struct | Osig_value {oval_type} -> !Oprint.out_type fmt oval_type | Osig_ellipsis -> - Format.fprintf fmt "..." + Format_doc.fprintf fmt "..." #elif OCAML_VERSION >= (4,02,0) | Osig_type ({ otype_type },_) -> tydecl fmt otype_type @@ -210,13 +218,13 @@ module IndexFormat = struct let parent_ty ?colorise ?short fmt id = option_iter (IndexMisc.parent_type id) (fun id -> - Format.fprintf fmt "@[%a =@ %a@]" + Format_doc.fprintf fmt "@[%a =@ %a@]" (path ?colorise ?short) id (ty ?colorise) id) let doc ?escaped ?colorise:(_ = no_color) fmt id = option_iter (Lazy.force id.doc) - (Format.fprintf fmt "@[%a@]" (lines ?escaped)) + (Format_doc.fprintf fmt "@[%a@]" (lines ?escaped)) let loc ?root ?(intf=false) ?colorise:(_ = no_color) fmt id = let loc = @@ -224,7 +232,7 @@ module IndexFormat = struct else Lazy.force id.loc_impl in if loc = Location.none then - Format.fprintf fmt "@[@]" + Format_doc.fprintf fmt "@[@]" else let pos = loc.Location.loc_start in let fname = match root with @@ -243,19 +251,19 @@ module IndexFormat = struct pos.Lexing.pos_fname | _ -> pos.Lexing.pos_fname in - Format.fprintf fmt "@[%s:%d:%d@]" + Format_doc.fprintf fmt "@[%s:%d:%d@]" fname pos.Lexing.pos_lnum (pos.Lexing.pos_cnum - pos.Lexing.pos_bol) let file ?colorise:(_ = no_color) fmt id = - Format.fprintf fmt "@[%s@]" + Format_doc.fprintf fmt "@[%s@]" (match id.file with Cmt f | Cmi f | Cmti f -> f) let info ?(colorise = no_color) fmt id = let breakif n fmt = function | None -> () - | Some _ -> Format.pp_print_break fmt 1 n + | Some _ -> Format_doc.pp_print_break fmt 1 n in - Format.fprintf fmt "@[@[%a@ %a%a%a@]%a%a@]@." + Format_doc.fprintf fmt "@[@[%a@ %a%a%a@]%a%a@]@." (path ?short:None ~colorise) id (kind ~colorise) id (breakif 0) id.ty @@ -276,8 +284,8 @@ module IndexFormat = struct | 'f' -> file ?colorise fmt id | 'i' -> info ?colorise fmt id | 'e' -> parent_ty ?colorise fmt id - | '%' -> Format.fprintf fmt "%%" - | c -> Format.fprintf fmt "%%%c" c + | '%' -> Format_doc.fprintf fmt "%%" + | c -> Format_doc.fprintf fmt "%%%c" c let format ?root ?(separate=false) format ?colorise fmt id = let len = String.length format in @@ -288,7 +296,7 @@ module IndexFormat = struct else let fmt = ffmt () in begin match format.[j], format.[j+1] with - | '%', c -> handle_format_char ?root c ?colorise fmt id + | '%', c -> Format_doc.compat (handle_format_char ?root c ?colorise) fmt id | '\\', 'n' -> Format.pp_print_newline fmt () | '\\', 't' -> Format.pp_print_char fmt '\t' | '\\', 'r' -> Format.pp_print_char fmt '\r' @@ -322,7 +330,7 @@ module Print = struct let colorise = if color then IndexFormat.color else IndexFormat.no_color in - f ~colorise Format.str_formatter id; + Format_doc.compat (f ~colorise) Format.str_formatter id; Format.flush_str_formatter () let name = make IndexFormat.name @@ -342,7 +350,7 @@ module Print = struct let info = make IndexFormat.info let format ?root ?separate format = - make (IndexFormat.format ?root ?separate format) + make (fun ?colorise fmt x -> Format_doc.deprecated_printer (fun fmt -> IndexFormat.format ?root ?separate format ?colorise fmt x) fmt) end diff --git a/libs/indexOut.mli b/libs/indexOut.mli index d248da9..0bbf36b 100644 --- a/libs/indexOut.mli +++ b/libs/indexOut.mli @@ -21,35 +21,35 @@ open IndexTypes module Format: sig type coloriser = { f: 'a. kind -> - ('a, Format.formatter, unit) format -> Format.formatter + ('a, Format_doc.formatter, unit) format -> Format_doc.formatter -> 'a } val color: coloriser val no_color: coloriser (** short name of the identifier *) - val name: ?colorise:coloriser -> Format.formatter -> info -> unit + val name: ?colorise:coloriser -> Format_doc.formatter -> info -> unit (** fully qualified name (with [short], returns the path the ident was found at, not the path where it was originally created) *) - val path: ?short:bool -> ?colorise:coloriser -> Format.formatter -> info -> unit + val path: ?short:bool -> ?colorise:coloriser -> Format_doc.formatter -> info -> unit - val kind: ?colorise:coloriser -> Format.formatter -> info -> unit + val kind: ?colorise:coloriser -> Format_doc.formatter -> info -> unit - val ty: ?colorise:coloriser -> Format.formatter -> info -> unit + val ty: ?colorise:coloriser -> Format_doc.formatter -> info -> unit val doc: ?escaped:bool -> - ?colorise:coloriser -> Format.formatter -> info -> unit + ?colorise:coloriser -> Format_doc.formatter -> info -> unit val loc: ?root:string -> ?intf:bool -> - ?colorise:coloriser -> Format.formatter -> info -> unit + ?colorise:coloriser -> Format_doc.formatter -> info -> unit - val file: ?colorise:coloriser -> Format.formatter -> info -> unit + val file: ?colorise:coloriser -> Format_doc.formatter -> info -> unit (** summary of the information *) - val info: ?colorise:coloriser -> Format.formatter -> info -> unit + val info: ?colorise:coloriser -> Format_doc.formatter -> info -> unit (** print following a custom format string (%n,%p,%k,%t,%d,%l,%s,%f,%i are interpreted). If [~separate] is set to [true], escapes are formatted diff --git a/libs/indexTypes.ml b/libs/indexTypes.ml index ddeca07..8da38a6 100644 --- a/libs/indexTypes.ml +++ b/libs/indexTypes.ml @@ -49,3 +49,13 @@ type t = (char, info) IndexTrie.t (* * Raised when cmi/cmt/cmti files can't be loaded. Probably a different version of OCaml *) exception Bad_format of string + +#if OCAML_VERSION >= (5,3,0) + module Format_doc = Format_doc +#else + module Format_doc = struct + include Format + let compat = Fun.id + let deprecated_printer = Fun.id + end +#endif diff --git a/src/browserMain.ml b/src/browserMain.ml index 9677425..b0e4232 100644 --- a/src/browserMain.ml +++ b/src/browserMain.ml @@ -1,6 +1,8 @@ open Lwt.Infix open Lwt_react +module Format_doc = IndexTypes.Format_doc + (* LibIndex.info contains lazy values, we need a specialized equality. *) let rec eq l1 l2 = match l1, l2 with | [], [] -> true @@ -81,10 +83,10 @@ let pp_close_tag = Format.pp_close_tag let pp_with_style to_style = fun style fstr fmt -> let tag = to_style style in - pp_open_tag fmt tag; - Format.kfprintf + Format_doc.deprecated_printer (fun fmt -> pp_open_tag fmt tag) fmt; + Format_doc.kfprintf (fun fmt -> - pp_close_tag fmt ()) + Format_doc.deprecated_printer (fun fmt -> pp_close_tag fmt ()) fmt) fmt fstr let colorise opts = @@ -102,9 +104,9 @@ let sprint_answer ?(extra_info=false) cols colorise id = let print = Format.fprintf fmt in print "@[" ; - LibIndex.Format.kind ~colorise fmt id; + Format_doc.compat (LibIndex.Format.kind ~colorise) fmt id; print " "; - LibIndex.Format.path ~short:true ~colorise fmt id; + Format_doc.compat (LibIndex.Format.path ~short:true ~colorise) fmt id; begin match id with | { LibIndex.ty = None } | { LibIndex.kind = LibIndex.Module | LibIndex.ModuleType | @@ -112,14 +114,14 @@ let sprint_answer ?(extra_info=false) cols colorise id = -> () | { LibIndex.ty = Some _ } -> print "@ @[" ; - LibIndex.Format.ty ~colorise fmt id; + Format_doc.compat (LibIndex.Format.ty ~colorise) fmt id; print "@]" ; end ; print "@]" ; if extra_info && Lazy.force id.LibIndex.doc <> None then begin print "@\n " ; - LibIndex.Format.doc ~colorise fmt id + Format_doc.compat (LibIndex.Format.doc ~colorise) fmt id end ; Format.pp_print_flush fmt () ; get_content () @@ -726,10 +728,10 @@ let pp_kinds fmt options = let pp_kind fmt (c, hash, b) = if b then ( pp_open_tag fmt "Enabled" ; - pp_with_style (fun x -> x) hash "%s" fmt c ; + Format_doc.compat (fun fmt c -> pp_with_style (fun x -> x) hash "%s" fmt c) fmt c ; pp_close_tag fmt () ) else - pp_with_style (fun x -> x) "Disabled" "%s" fmt c ; + Format_doc.compat (fun fmt c -> pp_with_style (fun x -> x) "Disabled" "%s" fmt c) fmt c ; in let open IndexOptions in let { t ; v ; e ; c ; m ; s ; k } = options.filter in diff --git a/src/indexMain.ml b/src/indexMain.ml index 8ad5b6c..e222436 100644 --- a/src/indexMain.ml +++ b/src/indexMain.ml @@ -14,12 +14,13 @@ (** This module contains the run-time for the command-line ocp-index tool *) +module Format_doc = IndexTypes.Format_doc open Cmdliner let common_opts = IndexOptions.common_opts () -let default_cmd = +let default_cmd = Term.(ret (const (fun _ -> `Help (`Pager, None)) $ common_opts)) let default_info = @@ -116,11 +117,11 @@ let complete_cmd = List.iter (fun info -> let (!) f x = f ?colorise:None x in Format.fprintf fmt "(@[\"%a\"" - !(LibIndex.Format.path ~short:true) info; + (Format_doc.compat !(LibIndex.Format.path ~short:true)) info; Format.fprintf fmt "@ (:path . \"%a\")" - !(LibIndex.Format.path ~short:false) info; + (Format_doc.compat !(LibIndex.Format.path ~short:false)) info; Format.fprintf fmt "@ (:type . %S)" (LibIndex.Print.ty info); - Format.fprintf fmt "@ (:kind . \"%a\")" !LibIndex.Format.kind info; + Format.fprintf fmt "@ (:kind . \"%a\")" (Format_doc.compat !LibIndex.Format.kind) info; (if Lazy.force info.LibIndex.doc <> None then Format.fprintf fmt "@ (:doc . %S)" (LibIndex.Print.doc info)); Format.fprintf fmt "@]@ )@ " @@ -133,7 +134,7 @@ let complete_cmd = else LibIndex.Format.no_color in let print fmt i = match format with - | None -> LibIndex.Format.info ~colorise fmt i + | None -> Format_doc.compat (LibIndex.Format.info ~colorise) fmt i | Some fstring -> LibIndex.Format.format ?root:opts.IndexOptions.project_root ~separate @@ -146,7 +147,7 @@ let complete_cmd = Cmd.v (Cmd.info "complete" ~doc ~man) Term.(const print_compl $ common_opts $ sexpr $ format $ separate $ t) - + let type_cmd = let man = [ @@ -171,7 +172,7 @@ let type_cmd = Cmd.v (Cmd.info "type" ~doc ~man) Term.(const print_ty $ common_opts $ t) - + let locate_cmd = let man = [ @@ -215,7 +216,7 @@ let locate_cmd = Cmd.v (Cmd.info "locate" ~doc ~man) Term.(const print_loc $ common_opts $ interface $ t) - + let print_cmd = let man = [ @@ -252,7 +253,7 @@ let print_cmd = let doc = "Print information about an identifier with a custom format." in Cmd.v (Cmd.info "print" ~doc ~man) - Term.(const print $ common_opts $ query $ format $ separate) + Term.(const print $ common_opts $ query $ format $ separate) let full_cmd = Cmd.group ~default:default_cmd default_info