Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove core types and exceptions from identifiers #1242

Merged
merged 5 commits into from
Nov 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 5 additions & 11 deletions src/document/comment.ml
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ module Reference = struct
let to_ir : ?text:Inline.t -> Reference.t -> Inline.t =
fun ?text ref ->
match ref with
| `Resolved r -> (
| `Resolved r ->
(* IDENTIFIER MUST BE RENAMED TO DEFINITION. *)
let id = Reference.Resolved.identifier r in
let rendered = render_resolved r in
Expand All @@ -119,16 +119,10 @@ module Reference = struct
(* Add a tooltip if the content is not the rendered reference. *)
match text with None -> None | Some _ -> Some rendered
in
match Url.from_identifier ~stop_before:false id with
| Ok url ->
let target = Target.Internal (Resolved url) in
let link = { Link.target; content; tooltip } in
[ inline @@ Inline.Link link ]
| Error (Not_linkable _) -> content
| Error exn ->
(* FIXME: better error message *)
Printf.eprintf "Id.href failed: %S\n%!" (Url.Error.to_string exn);
content)
let url = Url.from_identifier ~stop_before:false id in
let target = Target.Internal (Resolved url) in
let link = { Link.target; content; tooltip } in
[ inline @@ Inline.Link link ]
| _ -> (
let s = render_unresolved ref in
match text with
Expand Down
137 changes: 54 additions & 83 deletions src/document/generator.ml
Original file line number Diff line number Diff line change
Expand Up @@ -43,18 +43,15 @@ let unresolved content =
O.elt [ inline @@ Link link ]

let path_to_id path =
match Url.Anchor.from_identifier (path :> Paths.Identifier.t) with
| Error _ -> None
| Ok url -> Some url
let url = Url.Anchor.from_identifier (path :> Paths.Identifier.t) in
Some url

let source_anchor source_loc =
(* Remove when dropping support for OCaml < 4.08 *)
let to_option = function Result.Ok x -> Some x | Result.Error _ -> None in
match source_loc with
| Some id ->
Url.Anchor.from_identifier
(id : Paths.Identifier.SourceLocation.t :> Paths.Identifier.t)
|> to_option
Some
(Url.Anchor.from_identifier
(id : Paths.Identifier.SourceLocation.t :> Paths.Identifier.t))
| _ -> None

let attach_expansion ?(status = `Default) (eq, o, e) page text =
Expand Down Expand Up @@ -141,14 +138,12 @@ module Make (Syntax : SYNTAX) = struct
| `OpaqueModule _ | `OpaqueModuleType _ -> true
| _ -> false
in
let id = Paths.Path.Resolved.identifier rp in
let txt = Url.render_path path in
match Url.from_identifier ~stop_before id with
| Ok href -> resolved href [ inline @@ Text txt ]
| Error (Url.Error.Not_linkable _) -> O.txt txt
| Error exn ->
Printf.eprintf "Id.href failed: %S\n%!" (Url.Error.to_string exn);
O.txt txt)
let txt = [ inline @@ Text (Url.render_path path) ] in
match Paths.Path.Resolved.identifier rp with
| Some id ->
let href = Url.from_identifier ~stop_before id in
resolved href txt
| None -> O.elt txt)

let dot prefix suffix = prefix ^ "." ^ suffix

Expand Down Expand Up @@ -192,13 +187,8 @@ module Make (Syntax : SYNTAX) = struct
let open Fragment in
let id = Resolved.identifier (fragment :> Resolved.t) in
let txt = render_resolved_fragment (fragment :> Resolved.t) in
match Url.from_identifier ~stop_before:false id with
| Ok href -> resolved href [ inline @@ Text txt ]
| Error (Not_linkable _) -> unresolved [ inline @@ Text txt ]
| Error exn ->
Printf.eprintf "[FRAG] Id.href failed: %S\n%!"
(Url.Error.to_string exn);
unresolved [ inline @@ Text txt ]
let href = Url.from_identifier ~stop_before:false id in
resolved href [ inline @@ Text txt ]

let from_fragment : Fragment.leaf -> text = function
| `Resolved r
Expand Down Expand Up @@ -274,10 +264,8 @@ module Make (Syntax : SYNTAX) = struct
in
let implementation =
match implementation with
| Some (Odoc_model.Lang.Source_info.Resolved id) -> (
match Url.Anchor.from_identifier (id :> Paths.Identifier.t) with
| Ok url -> Some url
| Error _ -> None)
| Some (Odoc_model.Lang.Source_info.Resolved id) ->
Some (Url.Anchor.from_identifier (id :> Paths.Identifier.t))
| _ -> None
in
Some (Source_page.Link { implementation; documentation })
Expand Down Expand Up @@ -517,26 +505,22 @@ module Make (Syntax : SYNTAX) = struct
end = struct
let record fields =
let field mutable_ id typ =
match Url.from_identifier ~stop_before:true id with
| Error e -> failwith (Url.Error.to_string e)
| Ok url ->
let name = Paths.Identifier.name id in
let attrs =
[ "def"; "record"; Url.Anchor.string_of_kind url.kind ]
in
let cell =
(* O.td ~a:[ O.a_class ["def"; kind ] ]
* [O.a ~a:[O.a_href ("#" ^ anchor); O.a_class ["anchor"]] []
* ; *)
O.code
((if mutable_ then O.keyword "mutable" ++ O.txt " " else O.noop)
++ O.txt name
++ O.txt Syntax.Type.annotation_separator
++ type_expr typ
++ O.txt Syntax.Type.Record.field_separator)
(* ] *)
in
(url, attrs, cell)
let url = Url.from_identifier ~stop_before:true id in
let name = Paths.Identifier.name id in
let attrs = [ "def"; "record"; Url.Anchor.string_of_kind url.kind ] in
let cell =
(* O.td ~a:[ O.a_class ["def"; kind ] ]
* [O.a ~a:[O.a_href ("#" ^ anchor); O.a_class ["anchor"]] []
* ; *)
O.code
((if mutable_ then O.keyword "mutable" ++ O.txt " " else O.noop)
++ O.txt name
++ O.txt Syntax.Type.annotation_separator
++ type_expr typ
++ O.txt Syntax.Type.Record.field_separator)
(* ] *)
in
(url, attrs, cell)
in
let rows =
fields
Expand Down Expand Up @@ -603,17 +587,13 @@ module Make (Syntax : SYNTAX) = struct

let variant cstrs : DocumentedSrc.t =
let constructor id args res =
match Url.from_identifier ~stop_before:true id with
| Error e -> failwith (Url.Error.to_string e)
| Ok url ->
let attrs =
[ "def"; "variant"; Url.Anchor.string_of_kind url.kind ]
in
let content =
let doc = constructor id args res in
O.documentedSrc (O.txt "| ") @ doc
in
(url, attrs, content)
let url = Url.from_identifier ~stop_before:true id in
let attrs = [ "def"; "variant"; Url.Anchor.string_of_kind url.kind ] in
let content =
let doc = constructor id args res in
O.documentedSrc (O.txt "| ") @ doc
in
(url, attrs, content)
in
match cstrs with
| [] -> O.documentedSrc (O.txt "|")
Expand All @@ -639,19 +619,13 @@ module Make (Syntax : SYNTAX) = struct

let extension_constructor (t : Odoc_model.Lang.Extension.Constructor.t) =
let id = (t.id :> Paths.Identifier.t) in
match Url.from_identifier ~stop_before:true id with
| Error e -> failwith (Url.Error.to_string e)
| Ok url ->
let anchor = Some url in
let attrs =
[ "def"; "variant"; Url.Anchor.string_of_kind url.kind ]
in
let code =
O.documentedSrc (O.txt "| ") @ constructor id t.args t.res
in
let doc = Comment.to_ir t.doc in
let markers = Syntax.Comment.markers in
DocumentedSrc.Nested { anchor; attrs; code; doc; markers }
let url = Url.from_identifier ~stop_before:true id in
let anchor = Some url in
let attrs = [ "def"; "variant"; Url.Anchor.string_of_kind url.kind ] in
let code = O.documentedSrc (O.txt "| ") @ constructor id t.args t.res in
let doc = Comment.to_ir t.doc in
let markers = Syntax.Comment.markers in
DocumentedSrc.Nested { anchor; attrs; code; doc; markers }

let extension (t : Odoc_model.Lang.Extension.t) =
let prefix =
Expand Down Expand Up @@ -1384,8 +1358,8 @@ module Make (Syntax : SYNTAX) = struct
let content = functor_parameter arg in
let attr = [ "parameter" ] in
let anchor =
Utils.option_of_result
@@ Url.Anchor.from_identifier (arg.id :> Paths.Identifier.t)
Some
(Url.Anchor.from_identifier (arg.id :> Paths.Identifier.t))
in
let doc = [] in
[
Expand Down Expand Up @@ -1612,11 +1586,10 @@ module Make (Syntax : SYNTAX) = struct
let name =
let open Odoc_model.Lang.FunctorParameter in
let name = Paths.Identifier.name arg.id in
match
let href =
Url.from_identifier ~stop_before (arg.id :> Paths.Identifier.t)
with
| Error _ -> O.txt name
| Ok href -> resolved href [ inline @@ Text name ]
in
resolved href [ inline @@ Text name ]
in
(if Syntax.Mod.functor_keyword then O.keyword "functor" else O.noop)
++ (O.box_hv @@ O.span
Expand Down Expand Up @@ -1653,12 +1626,11 @@ module Make (Syntax : SYNTAX) = struct
let name =
let open Odoc_model.Lang.FunctorParameter in
let name = Paths.Identifier.name arg.id in
match
let href =
Url.from_identifier ~stop_before
(arg.id :> Paths.Identifier.t)
with
| Error _ -> O.txt name
| Ok href -> resolved href [ inline @@ Text name ]
in
resolved href [ inline @@ Text name ]
in
O.box_hv
@@ O.txt "(" ++ name
Expand Down Expand Up @@ -1774,8 +1746,7 @@ module Make (Syntax : SYNTAX) = struct
in
let content = O.documentedSrc md_def in
let anchor =
Utils.option_of_result
@@ Url.Anchor.from_identifier (id :> Paths.Identifier.t)
Some (Url.Anchor.from_identifier (id :> Paths.Identifier.t))
in
let attr = [ "modules" ] in
let doc = [] in
Expand Down
12 changes: 3 additions & 9 deletions src/document/sidebar.ml
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,7 @@ end = struct
| None -> None
| Some (index_id, title) ->
let path =
match Url.from_identifier ~stop_before:false (index_id :> Id.t) with
| Ok r -> r
| Error _ -> assert false
(* This error case should never happen since [stop_before] is false, and even less since it's a page id *)
Url.from_identifier ~stop_before:false (index_id :> Id.t)
in
let content = Comment.link_content title in
Some (path, sidebar_toc_entry path content)
Expand Down Expand Up @@ -65,15 +62,12 @@ let of_lang (v : Odoc_index.sidebar) =
let item id =
let content = [ inline @@ Text (Odoc_model.Paths.Identifier.name id) ] in
let path = Url.from_identifier ~stop_before:false (id :> Id.t) in
match path with
| Ok path -> Some (path, sidebar_toc_entry path content)
| Error _ -> None
(* This error case should never happen since [stop_before] is false *)
(path, sidebar_toc_entry path content)
in
let units =
List.map
(fun { Odoc_index.units; name } ->
let units = List.filter_map item units in
let units = List.map item units in
{ name; units })
v.libs
in
Expand Down
Loading