package grace
A fancy diagnostics library that allows your compilers to exit with grace
Install
dune-project
Dependency
Authors
Maintainers
Sources
grace-0.1.0.tbz
sha256=19576d3f32e4a69c7ebad26a801b568a2e3bff24a0e4d5ddf3b8bf4eac479d4c
sha512=436db3699126eec797da1be9f530759547804cc081ed365a75ba8ae9b053c05999ae820d294dd20f6a68e0712084579c585105a3855d71b459efc1367172bd66
doc/src/grace.rendering/snippet.ml.html
Source file snippet.ml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395
open! Import open! Diagnostic module Multi_line_label = struct module Id = Int type t = | Top of { id : Id.t ; start : Byte_index.t ; priority : Priority.t } | Bottom of { id : Id.t ; stop : Byte_index.t ; priority : Priority.t ; label : Message.t } [@@deriving sexp] end module Line = struct type stag = { priority : Priority.t ; inline_labels : (Priority.t * Message.t) list } and segment = { content : string ; range : Range.t ; stag : stag option } and t = { start : Byte_index.t ; segments : segment list ; multi_line_labels : Multi_line_label.t list } [@@deriving sexp] end type block = { start : Line_index.t ; lines : Line.t list } and source = { source : Source.t ; labels : Label.t list ; blocks : block list } and t = { severity : Severity.t ; message : Message.t ; sources : source list ; notes : Message.t list } [@@deriving sexp] module Of_diagnostic = struct module Source_descr = Source_reader.Source_descr type 'a with_line = { line : Line_index.t ; it : 'a } let range_of_labels = function | [] -> assert false | { Label.range; _ } :: _ as labels -> List.fold_left labels ~f:(fun range label -> Range.merge range label.range) ~init:range ;; let line_of_range ~sd (line : Source_reader.Line.t) = { line = line.idx ; it = Line. { start = Source_reader.Line.start line ; segments = [ { content = Source_reader.Line.slice ~sd line ; stag = None ; range = line.range } ] ; multi_line_labels = [] } } ;; let line_of_idx ~sd (line_idx : Line_index.t) = line_of_range ~sd @@ Source_reader.Line.of_line_index sd line_idx ;; module Priority_count = struct type t = { primary : int ; secondary : int } let zero = { primary = 0; secondary = 0 } let start t ~priority : t = match priority with | Priority.Primary -> { t with primary = t.primary + 1 } | Secondary -> { t with secondary = t.secondary + 1 } ;; let stop t ~priority : t = match priority with | Priority.Primary -> assert (t.primary > 0); { t with primary = t.primary - 1 } | Secondary -> assert (t.secondary > 0); { t with secondary = t.secondary - 1 } ;; let priority = function | { primary = 0; secondary = 0 } -> None | { primary = 0; secondary = _ } -> Some Priority.Secondary | { primary = _; secondary = _ } -> Some Primary ;; end let add_eol_segment ~sd ~(line : Source_reader.Line.t) cursor rev_segments = let stop = Source_reader.Line.stop line in if (* If [cursor < stop -1], then we require a non-empty end-of-line segment *) Byte_index.(add cursor 1 < stop) then Line. { content = Source_reader.slicei sd cursor stop ; range = Range.create ~source:(Source_descr.source sd) cursor stop ; stag = None } :: rev_segments else rev_segments ;; let end_segments ~sd ~(line : Source_reader.Line.t) cursor rev_segments = rev_segments |> add_eol_segment ~sd ~line cursor |> List.rev ;; let segments_of_labels ~sd ~(line : Source_reader.Line.t) (labels : Label.t list) = (* 1. Convert labels into a interval set consisting of start and stop points *) let points = let compare_point = (* We lexicographically compare the idx and then we order starting points before stopping points *) Comparable.lift [%compare: Byte_index.t * [ `Start | `Stop ]] ~f:(fun (idx, _, start_or_stop) -> let start_or_stop = match start_or_stop with | `Start _ -> `Start | `Stop -> `Stop in idx, start_or_stop) in labels |> List.concat_map ~f:(fun Label.{ range; priority; message } -> let start, stop = Range.split range in [ start, priority, `Start message; stop, priority, `Stop ]) |> List.sort ~compare:compare_point in let rev_segments, _, cursor, cursor_labels = (* 2. Iterate through the interval set, maintaining a: + List of (reversed) segments + A priority counter -- a cheap way of deducing the priority of the cursor + A cursor -- the current byte position we're in the interval set + A list of labels starting at the cursor ('cursor labels') *) List.fold_left points ~init:([], Priority_count.zero, Source_reader.Line.start line, []) ~f: (fun (rev_segments, priority_count, cursor, cursor_labels) (idx, priority, start_or_stop) -> (* If the next point is at the current cursor ... *) if Byte_index.(cursor = idx) then ( let priority_count, cursor_msgs = match start_or_stop with | `Start msg -> (* and the point defined a start of a message, we increment the priority counter and add the label to the cursor labels. *) ( Priority_count.start priority_count ~priority , (priority, msg) :: cursor_labels ) | `Stop -> (* and the point defined a end of a message, we decrement the priority counter. *) Priority_count.stop priority_count ~priority, cursor_labels in rev_segments, priority_count, cursor, cursor_msgs) else ( (* otherwise, we create a segment from 'cursor' to 'idx' and set 'cursor' to 'idx' *) let segment = Line. { content = Source_reader.slicei sd cursor idx ; range = Range.create ~source:(Source_descr.source sd) cursor idx ; stag = Option.( Priority_count.priority priority_count >>| fun priority -> { priority; inline_labels = cursor_labels }) } in let priority_count, cursor_labels = match start_or_stop with | `Start msg -> Priority_count.start priority_count ~priority, [ priority, msg ] | `Stop -> Priority_count.stop priority_count ~priority, [] in segment :: rev_segments, priority_count, idx, cursor_labels)) in assert (List.is_empty cursor_labels); (* 3. Add end-of-line segment to end the line *) let segments = end_segments ~sd ~line cursor rev_segments in segments ;; let line_of_labels ~sd ~line labels multi_line_labels = let segments = segments_of_labels ~sd ~line labels in { line = line.idx ; it = Line.{ start = Source_reader.Line.start line; segments; multi_line_labels } } ;; let add_contextual_lines ~sd lines = (* A contextual line is a line satisfying one of: + withing +/-1 lines of a multi-line label (top or bottom) ('multi line label contextual lines') + between two other rendered lines ('gap' contextual lines) *) let add_multi_line_label_contextual_lines = List.concat_map_with_next_and_prev ~f:(fun (l2 : Line.t with_line) ~prev:l1 ~next:l3 -> if List.is_empty l2.it.multi_line_labels then [ l2 ] else ( let next_line = Line_index.(add l2.line 1) in let prefix = match l1 with | None when Line_index.(l2.line > initial) -> (* No preceeding line and [l2] isn't the first line *) [ line_of_idx ~sd Line_index.(sub l2.line 1) ] | Some l1 when Line_index.(diff l2.line l1.line) > 1 -> (* Preceeding line is not the immediately preceeding line to [l2] *) [ line_of_idx ~sd Line_index.(sub l2.line 1) ] | _ -> [] in let postfix = match l3 with | None when Line_index.(next_line < (Source_reader.Line.last sd).idx) -> [ line_of_idx ~sd next_line ] | Some l3 when Line_index.(diff l3.line l2.line) > 1 -> [ line_of_idx ~sd next_line ] | _ -> [] in prefix @ [ l2 ] @ postfix)) in let add_gap_contextual_lines = List.concat_map_with_next ~f:(fun l1 ~next:l2 -> match l2 with | None -> [ l1 ] | Some l2 -> let line_delta = Line_index.(diff l2.line l1.line) in if line_delta <= 0 then assert false else if line_delta = 1 then [ l1 ] else if line_delta = 2 then [ l1; line_of_idx ~sd Line_index.(add l1.line 1) ] else (* [line_delta > 2] *) [ l1 ]) in lines |> add_multi_line_label_contextual_lines |> add_gap_contextual_lines ;; let group = let splitting_threshold = 2 in let block ~start ~rest = { start = start.line; lines = start.it :: rest } in let rec loop = function | [] -> assert false | [ l ] -> l, [], [] | l1 :: ls -> let l2, ls, blocks = loop ls in if Line_index.(diff l2.line l1.line) >= splitting_threshold then l1, [], block ~start:l2 ~rest:ls :: blocks else l1, l2.it :: ls, blocks in function | [] -> [] | ls -> let l, ls, blocks = loop ls in block ~start:l ~rest:ls :: blocks ;; module Line_labels = struct type t = { mutable inline_labels : Label.t list ; mutable multi_line_labels : Multi_line_label.t list } let create () = { inline_labels = []; multi_line_labels = [] } let add_inline_label t inline_label = t.inline_labels <- inline_label :: t.inline_labels ;; let add_multi_line_label t multi_line_label = t.multi_line_labels <- multi_line_label :: t.multi_line_labels ;; end let lines_of_labels ~sd labels = let range = range_of_labels labels in let enumerated_labels = List.mapi labels ~f:(fun i label -> i, label) in let _, rev_lines = Iter.fold (Source_reader.lines_in_range sd range) ~init:(enumerated_labels, []) ~f:(fun (labels, rev_lines) (line : Source_reader.Line.t) -> let line_start, line_stop = Source_reader.Line.split line in (* 1. Initialize per-line state *) let line_labels = Line_labels.create () in (* 2. Split labels into [inline_labels] and [multi_line_labels] and filter labels that end at this line *) let labels = List.filter labels ~f:(fun ((id, label) : _ * Label.t) -> let label_start, label_stop = Range.split label.range in if Byte_index.(line_start <= label_start && label_stop <= line_stop) then ( (* Inline label *) Line_labels.add_inline_label line_labels label; false) else if Byte_index.(line_start <= label_start && label_start <= line_stop) then ( (* Multi-line label that starts *) Line_labels.add_multi_line_label line_labels @@ Multi_line_label.Top { id; start = label_start; priority = label.priority }; true) else if Byte_index.(label_start < line_start && line_stop < label_stop) then (* Multi-line label that goes through this line *) true else if Byte_index.(label_start < line_start && label_stop <= line_stop) then ( (* Multi-line label that stops through this line *) Line_labels.add_multi_line_label line_labels @@ Multi_line_label.Bottom { id ; stop = label_stop ; priority = label.priority ; label = label.message }; false) else (* Label starts on a later line *) true) in (* Add to [rev_lines] *) ( labels , line_of_labels ~sd ~line line_labels.inline_labels line_labels.multi_line_labels :: rev_lines )) in List.rev rev_lines ;; let block_of_labels ~sd labels = labels |> lines_of_labels ~sd |> add_contextual_lines ~sd |> group ;; let of_diagnostic Diagnostic.{ severity; message; labels; notes } = let sources = labels |> List.sort_and_group ~compare: (Comparable.lift Source.compare ~f:(fun (label : Label.t) -> Range.source label.range)) |> List.map ~f:(fun labels -> (* Invariants: + [List.length labels > 0] + Sources for each label are equal *) let source = Range.source (List.hd_exn labels).range in let sd = Source_reader.open_source source in { source; labels; blocks = block_of_labels ~sd labels }) in { severity; message; notes; sources } ;; end let of_diagnostic = Of_diagnostic.of_diagnostic
sectionYPositions = computeSectionYPositions($el), 10)"
x-init="setTimeout(() => sectionYPositions = computeSectionYPositions($el), 10)"
>