11open ! Compat
22
33module Code_block = struct
4- type t = { location : Odoc_model.Location_ .span ; contents : string }
4+ type t = { location : Odoc_parser.Loc .span ; contents : string }
55end
66
77let drop_last lst =
@@ -14,8 +14,8 @@ let drop_first_and_last = function
1414 | [] -> None
1515 | first :: tl -> Some (first, drop_last tl)
1616
17- let slice lines ~(start : Odoc_model.Location_ .point )
18- ~( end_ : Odoc_model.Location_.point ) =
17+ let slice lines ~(start : Odoc_parser.Loc.point ) ~( end_ : Odoc_parser.Loc .point )
18+ =
1919 let lines_to_include =
2020 Util.Array. slice lines ~from: (start.line - 1 ) ~to_: (end_.line - 1 )
2121 |> Array. to_list
@@ -46,62 +46,29 @@ let slice lines ~(start : Odoc_model.Location_.point)
4646 let last_line = String. sub last_line 0 end_.column in
4747 String. concat " \n " ([ first_line ] @ stripped @ [ last_line ])
4848
49- (* Imagine a docstring that is within a file with four characters # of indentation. (I'll
50- use square brackets rather than parens to avoid escaping):
51-
52- ####[** foo
53- ####
54- ####bar
55- ####
56- ####baz *]
57- ####val x : int
58- ####val y : int
59-
60- According to odoc, the "b" in "bar" is at column 0 inside the docstring and at column 4
61- within the broader file. That is correct. But it says the "f" in "foo" is at column 1
62- inside the docstring and column 5 within the file. This isn't right.
63-
64- The problem is that it starts counting the inside-the-docstring column number from the
65- end of "[**", but doesn't add those three characters to the within-the-file column
66- number. Here, we make the adjustment.
67- *)
68- let account_for_docstring_open_token (location : Odoc_model.Location_.span ) =
69- let start_shift = 3 in
70- let end_shift = if location.start.line = location.end_.line then 3 else 0 in
71- {
72- location with
73- start = { location.start with column = location.start.column + start_shift };
74- end_ = { location.end_ with column = location.end_.column + end_shift };
75- }
76-
7749let extract_code_blocks ~(location : Lexing.position ) ~docstring =
7850 let rec acc blocks =
7951 List. map
8052 (fun block ->
81- match Odoc_model.Location_. value block with
82- | `Code_block contents ->
83- let location =
84- if location.pos_lnum = block.location.start.line then
85- account_for_docstring_open_token block.location
86- else block.location
87- in
88- [ { Code_block. location; contents } ]
53+ match Odoc_parser.Loc. value block with
54+ | `Code_block (_metadata , { Odoc_parser.Loc. value = contents ; _ } ) ->
55+ [ { Code_block. location = block.location; contents } ]
8956 | `List (_ , _ , lists ) -> List. map acc lists |> List. concat
9057 | _ -> [] )
9158 blocks
9259 |> List. concat
9360 in
94- let parsed = Odoc_parser. parse_comment_raw ~location ~text: docstring in
61+ let parsed = Odoc_parser. parse_comment ~location ~text: docstring in
9562 List. iter
96- (fun error -> failwith (Odoc_model.Error . to_string error))
97- parsed .warnings;
63+ (fun error -> failwith (Odoc_parser.Warning . to_string error))
64+ ( Odoc_parser . warnings parsed) ;
9865 List. map
9966 (fun element ->
100- match Odoc_model.Location_. value element with
101- | #Odoc_parser.Ast. nestable_block_element as e ->
102- acc
103- [ { Odoc_model.Location_. location = element.location; value = e } ]
104- | `Tag tag -> (
67+ match element with
68+ | { Odoc_parser.Loc. value = #Odoc_parser.Ast. nestable_block_element; _ }
69+ as e ->
70+ acc [ e ]
71+ | { value = `Tag tag ; _ } -> (
10572 match tag with
10673 | `Deprecated blocks -> acc blocks
10774 | `Param (_ , blocks ) -> acc blocks
@@ -110,8 +77,8 @@ let extract_code_blocks ~(location : Lexing.position) ~docstring =
11077 | `See (_ , _ , blocks ) -> acc blocks
11178 | `Before (_ , blocks ) -> acc blocks
11279 | _ -> [] )
113- | `Heading _ -> [] )
114- parsed.value
80+ | { value = `Heading _ ; _ } -> [] )
81+ ( Odoc_parser. ast parsed)
11582 |> List. concat
11683
11784let docstrings lexbuf =
@@ -128,10 +95,10 @@ let docstrings lexbuf =
12895 in
12996 loop [] |> List. rev
13097
131- let convert_pos (p : Lexing.position ) (pt : Odoc_model.Location_ .point ) =
98+ let convert_pos (p : Lexing.position ) (pt : Odoc_parser.Loc .point ) =
13299 { p with pos_lnum = pt.line; pos_cnum = pt.column }
133100
134- let convert_loc (loc : Location.t ) (sp : Odoc_model.Location_ .span ) =
101+ let convert_loc (loc : Location.t ) (sp : Odoc_parser.Loc .span ) =
135102 let loc_start = convert_pos loc.loc_start sp.start in
136103 let loc_end = convert_pos loc.loc_end sp.end_ in
137104 { loc with loc_start; loc_end }
@@ -140,12 +107,13 @@ let docstring_code_blocks str =
140107 Lexer. handle_docstrings := true ;
141108 Lexer. init () ;
142109 List. map
143- (fun (docstring , (location : Location.t )) ->
144- let blocks =
145- extract_code_blocks ~location: location .loc_start ~docstring
110+ (fun (docstring , (cmt_loc : Location.t )) ->
111+ let location =
112+ { cmt_loc .loc_start with pos_cnum = cmt_loc.loc_start.pos_cnum + 3 }
146113 in
114+ let blocks = extract_code_blocks ~location ~docstring in
147115 List. map
148- (fun (b : Code_block.t ) -> (b, convert_loc location b.location))
116+ (fun (b : Code_block.t ) -> (b, convert_loc cmt_loc b.location))
149117 blocks)
150118 (docstrings (Lexing. from_string str))
151119 |> List. concat
@@ -155,7 +123,7 @@ let parse_mli file_contents =
155123 [Text] and [Block] parts by using the starts and ends of those blocks as
156124 boundaries. *)
157125 let code_blocks = docstring_code_blocks file_contents in
158- let cursor = ref { Odoc_model.Location_ . line = 1 ; column = 0 } in
126+ let cursor = ref { Odoc_parser.Loc . line = 1 ; column = 0 } in
159127 let lines = String. split_on_char '\n' file_contents |> Array. of_list in
160128 let tokens =
161129 List. map
@@ -185,11 +153,11 @@ let parse_mli file_contents =
185153 in
186154 let eof =
187155 {
188- Odoc_model.Location_ . line = Array. length lines;
156+ Odoc_parser.Loc . line = Array. length lines;
189157 column = String. length lines.(Array. length lines - 1 );
190158 }
191159 in
192- let eof_is_beyond_location (loc : Odoc_model.Location_ .point ) =
160+ let eof_is_beyond_location (loc : Odoc_parser.Loc .point ) =
193161 eof.line > loc.line || (eof.line = loc.line && eof.column > loc.column)
194162 in
195163 if eof_is_beyond_location ! cursor then
0 commit comments