Skip to content

Commit

Permalink
upgrade ocamlformat
Browse files Browse the repository at this point in the history
  • Loading branch information
art-w committed Dec 21, 2023
1 parent 514a67f commit b9bb6a2
Show file tree
Hide file tree
Showing 34 changed files with 844 additions and 905 deletions.
12 changes: 2 additions & 10 deletions .ocamlformat
Original file line number Diff line number Diff line change
@@ -1,17 +1,9 @@
version = 0.25.1
version = 0.26.1
profile = janestreet
let-binding-spacing = compact
sequence-style = separator
doc-comments = after-when-possible
exp-grouping = preserve
break-cases = toplevel
break-separators = before
cases-exp-indent = 4
cases-matching-exp-indent = normal
if-then-else = keyword-first
parens-tuple = multi-line-only
type-decl = sparse
field-space = loose
space-around-arrays = true
space-around-lists = true
space-around-records = true
dock-collection-brackets = false
64 changes: 31 additions & 33 deletions cli/main.ml
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,17 @@ let string_of_kind =
| Field _ -> "field"
| Val _ -> "val"

let print_result ~print_cost ~no_rhs
Db.Entry.
{ name
; rhs
; url = _
; kind
; cost
; doc_html = _
; pkg = _
; is_from_module_type = _
} =
let print_result
~print_cost
~no_rhs
Db.Entry.
{ name; rhs; url = _; kind; cost; doc_html = _; pkg = _; is_from_module_type = _ }
=
let cost = if print_cost then string_of_int cost ^ " " else "" in
let typedecl_params =
(match kind with
| Db.Entry.Kind.TypeDecl args -> args
| _ -> None)
| Db.Entry.Kind.TypeDecl args -> args
| _ -> None)
|> Option.map (fun str -> str ^ " ")
|> Option.value ~default:""
in
Expand All @@ -54,31 +49,30 @@ let search ~print_cost ~static_sort ~limit ~db ~no_rhs ~pretty_query query =
match Query.(search ~shards:db ~dynamic_sort:(not static_sort) query) with
| [] -> print_endline "[No results]"
| _ :: _ as results ->
List.iter (print_result ~print_cost ~no_rhs) results ;
flush stdout
List.iter (print_result ~print_cost ~no_rhs) results ;
flush stdout

let rec search_loop ~print_cost ~no_rhs ~pretty_query ~static_sort ~limit ~db =
match In_channel.input_line stdin with
| Some query ->
search ~print_cost ~static_sort ~limit ~db ~no_rhs ~pretty_query query ;
search_loop ~print_cost ~no_rhs ~pretty_query ~static_sort ~limit ~db
search ~print_cost ~static_sort ~limit ~db ~no_rhs ~pretty_query query ;
search_loop ~print_cost ~no_rhs ~pretty_query ~static_sort ~limit ~db
| None -> print_endline "[Search session ended]"

let main db query print_cost no_rhs static_sort limit pretty_query =
match db with
| None ->
output_string stderr
"No database provided. Provide one by exporting the SHERLODOC_DB \
variable, or using the --db option\n" ;
exit 1
| Some db -> (
let db = Storage_marshal.load db in
match query with
| None ->
search_loop ~print_cost ~no_rhs ~pretty_query ~static_sort ~limit ~db
| Some query ->
search ~print_cost ~no_rhs ~pretty_query ~static_sort ~limit ~db query
)
output_string
stderr
"No database provided. Provide one by exporting the SHERLODOC_DB variable, or \
using the --db option\n" ;
exit 1
| Some db ->
let db = Storage_marshal.load db in
(match query with
| None -> search_loop ~print_cost ~no_rhs ~pretty_query ~static_sort ~limit ~db
| Some query ->
search ~print_cost ~no_rhs ~pretty_query ~static_sort ~limit ~db query)

open Cmdliner

Expand All @@ -94,9 +88,7 @@ let limit =
Arg.(value & opt int 50 & info [ "limit"; "n" ] ~docv:"N" ~doc)

let query =
let doc =
"The query. If absent, sherlodoc will read queries in the standard input."
in
let doc = "The query. If absent, sherlodoc will read queries in the standard input." in
Arg.(value & pos 0 (some string) None & info [] ~docv:"QUERY" ~doc)

let print_cost =
Expand All @@ -121,7 +113,13 @@ let pretty_query =

let main =
Term.(
const main $ db_filename $ query $ print_cost $ no_rhs $ static_sort $ limit
const main
$ db_filename
$ query
$ print_cost
$ no_rhs
$ static_sort
$ limit
$ pretty_query)

let cmd =
Expand Down
3 changes: 1 addition & 2 deletions db/db.ml
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,4 @@ let store db name elt ~count =
let store_type_polarities db elt polarities =
List.iter (fun (word, count) -> store db ~count word elt) polarities

let store_word db word elt =
Suffix_tree.With_elts.add_suffixes db.writer_names word elt
let store_word db word elt = Suffix_tree.With_elts.add_suffixes db.writer_names word elt
17 changes: 8 additions & 9 deletions db/db.mli
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,15 @@ type t = Db_typedef.t =
}
(** The type of a search database.
[db_names] is for text-based part of the query and [db_types] for the
type-based part.
[db_names] is for text-based part of the query and [db_types] for the
type-based part.
[db_types] has [Entry.t array Int_map.t] ([Occ.t]) as a payload because we want
the query [blabla : int -> int -> _] to return only entries that take at
least two ints as arguments, an entry of type [int -> string] is invalid.
The [Int_map.t] maps a number of occurences to a set of entries. See {!Occ}.
[db_types] still is a suffix tree, so you can search in it only for text. The
way we transform types into searchable text is in {!Type_polarity}.
*)
[db_types] has [Entry.t array Int_map.t] ([Occ.t]) as a payload because we want
the query [blabla : int -> int -> _] to return only entries that take at
least two ints as arguments, an entry of type [int -> string] is invalid.
The [Int_map.t] maps a number of occurences to a set of entries. See {!Occ}.
[db_types] still is a suffix tree, so you can search in it only for text. The
way we transform types into searchable text is in {!Type_polarity}. *)

type writer
(** The type that builds a database. You can use it to add things to it, but
Expand Down
41 changes: 20 additions & 21 deletions db/entry.ml
Original file line number Diff line number Diff line change
Expand Up @@ -60,27 +60,27 @@ module T = struct
begin
match Int.compare (String.length a.name) (String.length b.name) with
| 0 -> begin
match String.compare a.name b.name with
match String.compare a.name b.name with
| 0 -> begin
match Option.compare compare_pkg a.pkg b.pkg with
| 0 -> begin
match Option.compare compare_pkg a.pkg b.pkg with
| 0 -> begin
match Stdlib.compare a.kind b.kind with
| 0 -> Stdlib.compare a.url b.url
| c -> c
end
| c -> c
end
match Stdlib.compare a.kind b.kind with
| 0 -> Stdlib.compare a.url b.url
| c -> c
end
| c -> c
end
| c -> c
end
| c -> c
end

let compare a b =
if a == b
then 0
else
else (
let cmp = Int.compare a.cost b.cost in
if cmp = 0 then structural_compare a b else cmp
if cmp = 0 then structural_compare a b else cmp)
end

include T
Expand Down Expand Up @@ -108,19 +108,18 @@ let pkg_link { pkg; _ } =
match pkg with
| None -> None
| Some { name; version } ->
Some (Printf.sprintf "https://ocaml.org/p/%s/%s" name version)
Some (Printf.sprintf "https://ocaml.org/p/%s/%s" name version)

let link t =
match pkg_link t with
| None -> None
| Some pkg_link ->
let name, path =
match List.rev (String.split_on_char '.' t.name) with
| name :: path -> name, String.concat "/" (List.rev path)
| _ -> "", ""
in
Some (pkg_link ^ "/doc/" ^ path ^ "/index.html#val-" ^ name)

let v ~name ~kind ~cost ~rhs ~doc_html ~url ~is_from_module_type ?(pkg = None)
() =
let name, path =
match List.rev (String.split_on_char '.' t.name) with
| name :: path -> name, String.concat "/" (List.rev path)
| _ -> "", ""
in
Some (pkg_link ^ "/doc/" ^ path ^ "/index.html#val-" ^ name)

let v ~name ~kind ~cost ~rhs ~doc_html ~url ~is_from_module_type ?(pkg = None) () =
{ name; kind; url; cost; doc_html; pkg; rhs; is_from_module_type }
4 changes: 2 additions & 2 deletions db/entry.mli
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ end
val pkg_link : t -> string option
val link : t -> string option

val v :
name:string
val v
: name:string
-> kind:Kind.t
-> cost:int
-> rhs:string option
Expand Down
17 changes: 9 additions & 8 deletions db/occ.ml
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ let is_empty = Int_map.is_empty
let equal_elt (a_count, a) (b_count, b) = a_count = b_count && Entry.equal a b

(*
let of_list li =
List.fold_left
(fun acc (count, elt) ->
let elts = try Int_map.find count acc with Not_found -> [] in
Int_map.add count (elt :: elts) acc)
Int_map.empty li
|> Int_map.map Entry.Array.of_list
let of_list li =
List.fold_left
(fun acc (count, elt) ->
let elts = try Int_map.find count acc with Not_found -> [] in
Int_map.add count (elt :: elts) acc)
Int_map.empty li
|> Int_map.map Entry.Array.of_list
*)

let of_list li =
Expand All @@ -24,5 +24,6 @@ let of_list li =
match Int_map.find_opt count acc with
| None -> Int_map.add count (Entry.Set.singleton elt) acc
| Some set -> Int_map.add count (Entry.Set.add elt set) acc)
Int_map.empty li
Int_map.empty
li
|> Int_map.map (fun set -> set |> Entry.Set.to_seq |> Array.of_seq)
109 changes: 53 additions & 56 deletions db/occ.mli
Original file line number Diff line number Diff line change
@@ -1,60 +1,57 @@
(** [Occ] stands for occurences. It associate sets of elements to the number of
time members of the set occurs.
The list [[a, a, b, b, c]] would correspond to [[(2, [a; b]); (1, [c]) ]]. It is
used or type search : you want to be able to return every function that takes
two ints as an argument. Without this datastrucure, we would only be able to
search for functions that take ints, without specifying the amount.
This datastructure is used at the leafs of the suffix tree : so when doing type
search, we first perform a type search ignoring occurences, and afterwards
filter the results according to them.
I will give an example bellow, it is probably better to read {!Type_polarities}
first to understand it completely.
If you have the following entries :
{[
val a : string -> int
val b : string -> string -> int
val c : string -> string -> (int * int)
val d : (string * string) -> float -> (int * int)
]}
Their polarities will be :
{[
val a : {(-string, 1); (+int, 1)}
val b : {(-string, 2); (+int, 1)}
val c : {(-string, 2); (+int, 2)}
val d : {(-string, 2); (+int, 2); (-float, 1)}
]}
We can combine them into a database that will look like this :
{[
+int ->
{ 1 -> {a; b}
2 -> {c; d}
}
-string ->
{ 1 -> {a}
2 -> {b; c; d}
}
-float ->
{ 1 -> {d}
}
]}
If there is a query for type [string -> string -> (int * int)], the polarities
of the query are [(-string, 2)], [(+int, 2)].
The entries of [(-string, 2)] are [{b; c; d}], and the entries of [(+int, 2)]
are [{c; d}]. The intersection of the two is [{c; d}].
*)
time members of the set occurs.
The list [[a, a, b, b, c]] would correspond to [[(2, [a; b]); (1, [c]) ]]. It is
used or type search : you want to be able to return every function that takes
two ints as an argument. Without this datastrucure, we would only be able to
search for functions that take ints, without specifying the amount.
This datastructure is used at the leafs of the suffix tree : so when doing type
search, we first perform a type search ignoring occurences, and afterwards
filter the results according to them.
I will give an example bellow, it is probably better to read {!Type_polarities}
first to understand it completely.
If you have the following entries :
{[
val a : string -> int
val b : string -> string -> int
val c : string -> string -> int * int
val d : string * string -> float -> int * int
]}
Their polarities will be :
{[
val a : {(-string, 1); (+int, 1)}
val b : {(-string, 2); (+int, 1)}
val c : {(-string, 2); (+int, 2)}
val d : {(-string, 2); (+int, 2); (-float, 1)}
]}
We can combine them into a database that will look like this :
{[
+int ->
{ 1 -> {a; b}
2 -> {c; d}
}
-string ->
{ 1 -> {a}
2 -> {b; c; d}
}
-float ->
{ 1 -> {d}
}
]}
If there is a query for type [string -> string -> (int * int)], the polarities
of the query are [(-string, 2)], [(+int, 2)].
The entries of [(-string, 2)] are [{b; c; d}], and the entries of [(+int, 2)]
are [{c; d}]. The intersection of the two is [{c; d}]. *)

type t
type elt = int * Entry.t
Expand Down
Loading

0 comments on commit b9bb6a2

Please sign in to comment.