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

Stable anchors in links to implementation. #993

Merged
merged 43 commits into from
Sep 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
5a816d2
Add `SourceLocationInt for an internal source location
jonludlam Jul 6, 2023
65aace9
Use char offsets in source for local source links/anchors
jonludlam Jul 6, 2023
e75fa97
Separate syntax from other source annotations
jonludlam Jul 16, 2023
1997858
Add `full_name` to Paths.Identifier
jonludlam Jul 17, 2023
a9e404c
Semantic anchors for source locations
jonludlam Jul 14, 2023
8efefd9
Refactoring and compatibility
panglesd Aug 28, 2023
19dff47
Add exception to [uid_to_loc] table
panglesd Aug 29, 2023
c412072
Add local jump to definition for constructors
panglesd Aug 29, 2023
ce0334a
optimize list concatenation
panglesd Aug 29, 2023
44fa491
Never add links from ghost locations
panglesd Aug 29, 2023
4637797
Add semantic anchors for extension constructors
panglesd Aug 29, 2023
45f63a1
Fix module with constraints not being folded over
panglesd Aug 30, 2023
090d0fb
fix wrong location taken on types
panglesd Aug 30, 2023
d463927
Use compiler-independent anchor for functor argument
panglesd Aug 30, 2023
ae41cb8
Do not add anchors to ghost locations from `uid_to_loc` table
panglesd Aug 30, 2023
4256697
ocamlformat
panglesd Aug 30, 2023
55e60a4
Clean unused values with no side-effect
panglesd Aug 30, 2023
91d3930
Remove useless call to `add_parameter`
panglesd Aug 30, 2023
e00767d
Add doc-comment for the implementation module
panglesd Aug 30, 2023
d619ffc
Generate identifiers for exceptions and extensions in ident_env
panglesd Aug 30, 2023
da06546
Unexpose unused values from ident_env
panglesd Aug 30, 2023
c0c5e85
Add anchor to the content of functor parameters
panglesd Aug 30, 2023
b86bd15
Style-related review comments
panglesd Aug 31, 2023
4505b82
Compatibility
panglesd Aug 31, 2023
5f6f304
Use `Odoc_document.Url.Anchor` to generate anchors in source code
panglesd Sep 4, 2023
e7f7278
Loader: Add constructors to the environment
panglesd Sep 5, 2023
6a109ce
Loader: Rename shape_.ml into lookup_def.ml
panglesd Sep 5, 2023
dc4b564
Rename SourceLocationInt to SourceLocationInternal
panglesd Sep 5, 2023
b15f452
Remove constraint on source tests
panglesd Sep 6, 2023
8088e83
Move the shape lookup code into xref2, where other resolution happens
jonludlam Sep 14, 2023
de7641f
Update mdx tests
jonludlam Sep 14, 2023
4500c04
Update driver to take cmt argument
jonludlam Sep 14, 2023
0121576
Formatting and backwards compat
jonludlam Sep 14, 2023
105c500
ocamlformat ignore updates
jonludlam Sep 14, 2023
f2bafc2
Remove shape from odocl files
jonludlam Sep 14, 2023
c848d55
ocamlformating `loader/implementation.ml` files
panglesd Sep 15, 2023
11c1bd0
Source loading: turned traverse from "lift" to "fold".
panglesd Sep 15, 2023
652931c
Source rendering: anchors do not depend on locations
panglesd Sep 15, 2023
ae5bc8b
Source rendering: Anchor counter should increase
panglesd Sep 15, 2023
6e51617
Add explanatory comment
jonludlam Sep 15, 2023
f6cbd64
Move version-dependent code from odoc_loader to implementation
jonludlam Sep 15, 2023
3cd4f2f
Combine shape and uid_to_id to one field in Lang
jonludlam Sep 15, 2023
008efb3
Update tests
jonludlam Sep 15, 2023
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
1,448 changes: 744 additions & 704 deletions doc/driver.mld

Large diffs are not rendered by default.

10 changes: 6 additions & 4 deletions src/.ocamlformat-ignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ loader/cmi.mli
loader/cmt.ml
loader/cmti.ml
loader/doc_attr.ml
loader/local_jmp.ml
loader/implementation.ml
loader/lookup_def.ml
loader/uid.ml
loader/uid.mli
loader/lookup_def.mli
loader/odoc_loader.ml
syntax_highlighter/syntax_highlighter.ml
model/*.cppo.ml
html_support_files/*.ml
html_support_files/*.ml
xref2/shape_tools.*

1 change: 1 addition & 0 deletions src/document/ML.mli
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ val source_tree : Lang.SourceTree.t -> Types.Document.t list

val source_page :
Identifier.SourcePage.t ->
Syntax_highlighter.infos ->
Lang.Source_info.infos ->
string ->
Types.Document.t
2 changes: 1 addition & 1 deletion src/document/dune
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@
(public_name odoc.document)
(instrumentation
(backend bisect_ppx))
(libraries odoc_model fpath astring))
(libraries odoc_model fpath astring syntax_highlighter))
43 changes: 31 additions & 12 deletions src/document/generator.ml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ open O.Infix

let tag tag t = O.span ~attr:tag t

let rec filter_map acc f = function
| hd :: tl ->
let acc = match f hd with Some x -> x :: acc | None -> acc in
filter_map acc f tl
| [] -> List.rev acc

let label t =
match t with
| Odoc_model.Lang.TypeExpr.Label s -> tag "label" (O.txt s)
Expand Down Expand Up @@ -239,24 +245,37 @@ module Make (Syntax : SYNTAX) = struct
val url : Paths.Identifier.SourcePage.t -> Url.t
val source :
Paths.Identifier.SourcePage.t ->
Syntax_highlighter.infos ->
Lang.Source_info.infos ->
string ->
Source_page.t
end = struct
let path id = Url.Path.from_identifier id
let url id = Url.from_path (path id)

let info_of_info url = function
| Lang.Source_info.Syntax s -> Source_page.Syntax s
| Local_jmp (Occurence { anchor }) ->
Link (Url.Anchor.source_anchor url anchor)
| Local_jmp (Def string) -> Anchor string

let source id infos source_code =
let info_of_info : Lang.Source_info.annotation -> Source_page.info option =
function
| Value id -> (
match Url.Anchor.from_identifier (id :> Paths.Identifier.t) with
| Ok url -> Some (Link url)
| Error _ -> None)
| Definition id -> (
match id.iv with
| `SourceLocation (_, def) -> Some (Anchor (DefName.to_string def))
| `SourceLocationInternal (_, local) ->
Some (Anchor (LocalName.to_string local))
| _ -> None)

let source id syntax_info infos source_code =
let url = path id in
let mapper (info, loc) = (info_of_info url info, loc) in
let infos = List.map mapper infos in
let contents = Impl.impl ~infos source_code in
let mapper (info, loc) =
match info_of_info info with Some x -> Some (x, loc) | None -> None
in
let infos = filter_map [] mapper infos in
let syntax_info =
List.map (fun (ty, loc) -> (Source_page.Syntax ty, loc)) syntax_info
in
let contents = Impl.impl ~infos:(infos @ syntax_info) source_code in
{ Source_page.url; contents }
end

Expand Down Expand Up @@ -1863,6 +1882,6 @@ module Make (Syntax : SYNTAX) = struct

include Page

let source_page id infos source_code =
Document.Source_page (Source_page.source id infos source_code)
let source_page id syntax_info infos source_code =
Document.Source_page (Source_page.source id syntax_info infos source_code)
end
1 change: 1 addition & 0 deletions src/document/generator_signatures.ml
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ module type GENERATOR = sig

val source_page :
Odoc_model.Paths.Identifier.SourcePage.t ->
Syntax_highlighter.infos ->
Lang.Source_info.infos ->
string ->
Document.t
Expand Down
1 change: 1 addition & 0 deletions src/document/reason.mli
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ val source_tree : Lang.SourceTree.t -> Types.Document.t list

val source_page :
Identifier.SourcePage.t ->
Syntax_highlighter.infos ->
Lang.Source_info.infos ->
string ->
Types.Document.t
Expand Down
14 changes: 4 additions & 10 deletions src/document/url.ml
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,6 @@ open Odoc_model.Paths
open Odoc_model.Names
module Root = Odoc_model.Root

let functor_arg_pos : Odoc_model.Paths.Identifier.FunctorParameter.t -> int =
let open Odoc_model.Paths.Identifier in
fun { iv = `Parameter (p, _); _ } ->
let rec inner_sig = function
| `Result { iv = p; _ } -> 1 + inner_sig p
| `Module _ | `ModuleType _ | `Root _ | `Parameter _ -> 1
in
inner_sig p.iv

let render_path : Odoc_model.Paths.Path.t -> string =
let open Odoc_model.Paths.Path in
let rec render_resolved : Odoc_model.Paths.Path.Resolved.t -> string =
Expand Down Expand Up @@ -160,7 +151,7 @@ module Path = struct
mk ~parent kind name
| { iv = `Parameter (functor_id, arg_name); _ } as p ->
let parent = from_identifier (functor_id :> any) in
let arg_num = functor_arg_pos p in
let arg_num = Identifier.FunctorParameter.functor_arg_pos p in
let kind = `Parameter arg_num in
let name = ModuleName.to_string arg_name in
mk ~parent kind name
Expand Down Expand Up @@ -380,6 +371,9 @@ module Anchor = struct
| { iv = `SourceLocation (parent, loc); _ } ->
let page = Path.from_identifier (parent :> Path.any) in
Ok { page; kind = `SourceAnchor; anchor = DefName.to_string loc }
| { iv = `SourceLocationInternal (parent, loc); _ } ->
let page = Path.from_identifier (parent :> Path.any) in
Ok { page; kind = `SourceAnchor; anchor = LocalName.to_string loc }
| { iv = `SourceLocationMod parent; _ } ->
let page = Path.from_identifier (parent :> Path.any) in
Ok { page; kind = `SourceAnchor; anchor = "" }
Expand Down
23 changes: 9 additions & 14 deletions src/loader/cmi.ml
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,9 @@
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*)

module Loader_Uid = Uid
open Asttypes
open Types
module OCamlPath = Path
module Uid = Loader_Uid

open Odoc_model.Paths
open Odoc_model.Lang
Expand Down Expand Up @@ -629,8 +627,7 @@ let read_constructor_declaration_arguments env parent arg =

let read_constructor_declaration env parent cd =
let open TypeDecl.Constructor in
let name = Ident.name cd.cd_id in
let id = Identifier.Mk.constructor (parent, Odoc_model.Names.ConstructorName.make_std name) in
let id = Ident_env.find_constructor_identifier env cd.cd_id in
let container = (parent : Identifier.DataType.t :> Identifier.LabelParent.t) in
let doc = Doc_attr.attached_no_tag container cd.cd_attributes in
let args =
Expand Down Expand Up @@ -742,8 +739,7 @@ let read_type_declaration env parent id decl =

let read_extension_constructor env parent id ext =
let open Extension.Constructor in
let name = Ident.name id in
let id = Identifier.Mk.extension(parent, Odoc_model.Names.ExtensionName.make_std name) in
let id = Env.find_extension_identifier env id in
let locs = None in
let container = (parent : Identifier.Signature.t :> Identifier.LabelParent.t) in
let doc = Doc_attr.attached_no_tag container ext.ext_attributes in
Expand Down Expand Up @@ -776,8 +772,7 @@ let read_type_extension env parent id ext rest =

let read_exception env parent id ext =
let open Exception in
let name = Ident.name id in
let id = Identifier.Mk.exception_(parent, Odoc_model.Names.ExceptionName.make_std name) in
let id = Env.find_exception_identifier env id in
let locs = None in
let container = (parent : Identifier.Signature.t :> Identifier.LabelParent.t) in
let doc = Doc_attr.attached_no_tag container ext.ext_attributes in
Expand Down Expand Up @@ -933,12 +928,12 @@ let rec read_module_type env parent (mty : Odoc_model.Compat.module_type) =
match parameter with
| Unit -> Odoc_model.Lang.FunctorParameter.Unit, env
| Named (id_opt, arg) ->
let name, env = match id_opt with
| Some id -> Ident.name id, Env.add_parameter parent id (ModuleName.of_ident id) env
| None -> "_", env
let id, env = match id_opt with
| None -> Identifier.Mk.parameter(parent, Odoc_model.Names.ModuleName.make_std "_"), env
| Some id -> let env = Env.add_parameter parent id (ModuleName.of_ident id) env in
Ident_env.find_parameter_identifier env id, env
in
let id = Identifier.Mk.parameter(parent, Odoc_model.Names.ModuleName.make_std name) in
let arg = read_module_type env id arg in
let arg = read_module_type env (id :> Identifier.Signature.t) arg in
Odoc_model.Lang.FunctorParameter.Named ({ FunctorParameter. id; expr = arg }), env
in
let res = read_module_type env (Identifier.Mk.result parent) res in
Expand Down Expand Up @@ -1082,5 +1077,5 @@ and read_signature env parent (items : Odoc_model.Compat.signature) =

let read_interface root name intf =
let id = Identifier.Mk.root (root, Odoc_model.Names.ModuleName.make_std name) in
let items = read_signature Env.empty id intf in
let items = read_signature (Env.empty ()) id intf in
(id, items)
23 changes: 11 additions & 12 deletions src/loader/cmt.ml
Original file line number Diff line number Diff line change
Expand Up @@ -367,31 +367,30 @@ let rec read_module_expr env parent label_parent mexpr =
match parameter with
| Unit -> FunctorParameter.Unit, env
| Named (id_opt, _, arg) ->
let name, env =
let id, env =
match id_opt with
| Some id -> Ident.name id, Env.add_parameter parent id (ModuleName.of_ident id) env
| None -> "_", env
| None -> Identifier.Mk.parameter (parent, Odoc_model.Names.ModuleName.make_std "_"), env
| Some id -> let env = Env.add_parameter parent id (ModuleName.of_ident id) env in
Env.find_parameter_identifier env id, env
in
let id = Identifier.Mk.parameter (parent, Odoc_model.Names.ModuleName.make_std name) in
let arg = Cmti.read_module_type env id label_parent arg in
let arg = Cmti.read_module_type env (id :> Identifier.Signature.t) label_parent arg in

Named { id; expr=arg }, env
in
let res = read_module_expr env (Identifier.Mk.result parent) label_parent res in
Functor (f_parameter, res)
#else
| Tmod_functor(id, _, arg, res) ->
let new_env = Env.add_parameter parent id (ModuleName.of_ident id) env in
let f_parameter =
match arg with
| None -> FunctorParameter.Unit
| Some arg ->
let name = Ident.name id in
let id = Identifier.Mk.parameter (parent, ModuleName.make_std name) in
let arg = Cmti.read_module_type env id label_parent arg in
Named { FunctorParameter. id; expr = arg; }
let id = Env.find_parameter_identifier new_env id in
let arg = Cmti.read_module_type env (id :> Identifier.Signature.t) label_parent arg in
Named { FunctorParameter. id; expr = arg; }
in
let env = Env.add_parameter parent id (ModuleName.of_ident id) env in
let res = read_module_expr env (Identifier.Mk.result parent) label_parent res in
let res = read_module_expr new_env (Identifier.Mk.result parent) label_parent res in
Functor(f_parameter, res)
#endif
| Tmod_apply _ ->
Expand Down Expand Up @@ -603,7 +602,7 @@ and read_structure :
let read_implementation root name impl =
let id = Identifier.Mk.root (root, Odoc_model.Names.ModuleName.make_std name) in
let sg, canonical =
read_structure Odoc_model.Semantics.Expect_canonical Env.empty id impl
read_structure Odoc_model.Semantics.Expect_canonical (Env.empty ()) id impl
in
(id, sg, (canonical :> Odoc_model.Paths.Path.Module.t option))

Expand Down
33 changes: 13 additions & 20 deletions src/loader/cmti.ml
Original file line number Diff line number Diff line change
Expand Up @@ -213,9 +213,7 @@ let read_constructor_declaration_arguments env parent label_parent arg =

let read_constructor_declaration env parent cd =
let open TypeDecl.Constructor in
let open Odoc_model.Names in
let name = Ident.name cd.cd_id in
let id = Identifier.Mk.constructor(parent, ConstructorName.make_std name) in
let id = Ident_env.find_constructor_identifier env cd.cd_id in
let container = (parent : Identifier.DataType.t :> Identifier.Parent.t) in
let label_container = (container :> Identifier.LabelParent.t) in
let doc = Doc_attr.attached_no_tag label_container cd.cd_attributes in
Expand Down Expand Up @@ -292,9 +290,7 @@ let read_type_substitutions env parent decls =

let read_extension_constructor env parent ext =
let open Extension.Constructor in
let open Odoc_model.Names in
let name = Ident.name ext.ext_id in
let id = Identifier.Mk.extension(parent, ExtensionName.make_std name) in
let id = Env.find_extension_identifier env ext.ext_id in
let locs = None in
let container = (parent : Identifier.Signature.t :> Identifier.Parent.t) in
let label_container = (container :> Identifier.LabelParent.t) in
Expand Down Expand Up @@ -327,9 +323,7 @@ let read_type_extension env parent tyext =

let read_exception env parent (ext : extension_constructor) =
let open Exception in
let open Odoc_model.Names in
let name = Ident.name ext.ext_id in
let id = Identifier.Mk.exception_(parent, ExceptionName.make_std name) in
let id = Env.find_exception_identifier env ext.ext_id in
let locs = None in
let container = (parent : Identifier.Signature.t :> Identifier.Parent.t) in
let label_container = (container :> Identifier.LabelParent.t) in
Expand Down Expand Up @@ -523,31 +517,30 @@ and read_module_type env parent label_parent mty =
match parameter with
| Unit -> FunctorParameter.Unit, env
| Named (id_opt, _, arg) ->
let name, env =
let id, env =
match id_opt with
| None -> Identifier.Mk.parameter (parent, ModuleName.make_std "_"), env
| Some id ->
Ident.name id, Env.add_parameter parent id (ModuleName.of_ident id) env
| None -> "_", env
let env = Env.add_parameter parent id (ModuleName.of_ident id) env in
Env.find_parameter_identifier env id, env
in
let id = Identifier.Mk.parameter (parent, ModuleName.make_std name) in
let arg = read_module_type env id label_parent arg in
let arg = read_module_type env (id :> Identifier.Signature.t) label_parent arg in
Named { id; expr = arg; }, env
in
let res = read_module_type env (Identifier.Mk.result parent) label_parent res in
Functor (f_parameter, res)
#else
| Tmty_functor(id, _, arg, res) ->
let new_env = Env.add_parameter parent id (ModuleName.of_ident id) env in
let f_parameter =
match arg with
| None -> Odoc_model.Lang.FunctorParameter.Unit
| Some arg ->
let name = Ident.name id in
let id = Identifier.Mk.parameter (parent, Odoc_model.Names.ModuleName.make_std name) in
let arg = read_module_type env id label_parent arg in
let id = Ident_env.find_parameter_identifier new_env id in
let arg = read_module_type env (id :> Identifier.Signature.t) label_parent arg in
Named { FunctorParameter. id; expr = arg }
in
let env = Env.add_parameter parent id (ModuleName.of_ident id) env in
let res = read_module_type env (Identifier.Mk.result parent) label_parent res in
let res = read_module_type new_env (Identifier.Mk.result parent) label_parent res in
Functor( f_parameter, res)
#endif
| Tmty_with(body, subs) -> (
Expand Down Expand Up @@ -805,6 +798,6 @@ and read_signature :
let read_interface root name intf =
let id = Identifier.Mk.root (root, Odoc_model.Names.ModuleName.make_std name) in
let sg, canonical =
read_signature Odoc_model.Semantics.Expect_canonical Env.empty id intf
read_signature Odoc_model.Semantics.Expect_canonical (Env.empty ()) id intf
in
(id, sg, (canonical :> Odoc_model.Paths.Path.Module.t option))
2 changes: 1 addition & 1 deletion src/loader/dune
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@
(preprocess
(action
(run %{bin:cppo} -V OCAML:%{ocaml_version} %{input-file})))
(libraries odoc_model odoc-parser syntax_highlighter))
(libraries odoc_model odoc-parser syntax_highlighter odoc_document))
Loading
Loading