diff --git a/README.md b/README.md index 5efd89c..9b391b0 100644 --- a/README.md +++ b/README.md @@ -40,7 +40,7 @@ $ switchdocs setup --apply # add it to ~/.opam/config via `opam option` - `switchdocs search [--package PKG]... [-n N] QUERY` — search the generated documentation. Queries every package's `sherlodoc_db.marshal` together (a single query covers the whole switch), printing each match with its owning - package. `--package` restricts to named packages. + library and package. `--package` restricts to named packages. - `switchdocs show REFERENCE` — print an item's documentation as Markdown, resolving an odoc reference against the whole switch. `Stdlib` is open, so bare names work (`List`, `print_endline`); references may also be qualified diff --git a/lib/search.ml b/lib/search.ml index c37299d..30e9ee8 100644 --- a/lib/search.ml +++ b/lib/search.ml @@ -3,6 +3,7 @@ type result = { name : string; rhs : string option; pkg : string; + lib : string option; url : string; } @@ -59,12 +60,24 @@ let string_of_kind : Db.Entry.Kind.t -> string = function | Constructor _ -> "cons" | Field _ -> "field" +(* The driver writes HTML at ///..., so the library is the + second path component of the entry's url — but only for code entries; doc and + standalone pages live directly under the package, not in a library. *) +let library_of_url url = + match String.split_on_char '/' url with + | _pkg :: lib :: _ :: _ -> Some lib + | _ -> None + let result_of_entry (e : Db.Entry.t) = { kind = string_of_kind e.kind; name = unescape e.name; rhs = Option.map unescape e.rhs; pkg = e.pkg.Db.Entry.Package.name; + lib = + (match e.kind with + | Db.Entry.Kind.Doc | Page -> None + | _ -> library_of_url e.url); url = e.url; } @@ -107,6 +120,9 @@ let search ?(warn = fun _ -> ()) ?(limit = 25) ?(packages = []) sw query = Query.Blocking.search ~shards { Query.query; packages = []; limit } |> List.map result_of_entry -let pp ppf { kind; name; rhs; pkg; url = _ } = +let pp ppf { kind; name; rhs; pkg; lib; url = _ } = let rhs = match rhs with None -> "" | Some r -> r in - Format.fprintf ppf "%-6s %s%s (%s)" kind name rhs pkg + (* Show the library before the package, unless it adds nothing (a library + named after its package, or an entry with no library such as a page). *) + let lib = match lib with Some l when l <> pkg -> l ^ " " | _ -> "" in + Format.fprintf ppf "%-6s %s%s %s(%s)" kind name rhs lib pkg diff --git a/lib/search.mli b/lib/search.mli index 77fc74c..a9d7de9 100644 --- a/lib/search.mli +++ b/lib/search.mli @@ -14,6 +14,7 @@ type result = { name : string; rhs : string option; (** type signature / right-hand side, if any *) pkg : string; (** package the entry belongs to *) + lib : string option; (** library the entry belongs to, for code entries *) url : string; (** location of the entry within the package's docs *) }