|
| 1 | +open Haz3lcore; |
| 2 | +//open Virtual_dom.Vdom; |
| 3 | +open Util; |
| 4 | +open WebUtil; |
| 5 | + |
| 6 | +/* |
| 7 | + Used to display line numbering alongside cells |
| 8 | + */ |
| 9 | +module Model = CodeWithStatics.Model; |
| 10 | + |
| 11 | +module View = { |
| 12 | + let view = (model: Model.t, show_relative_numbers: bool, selected: bool) => { |
| 13 | + let {editor: {syntax: {measured, _}, state: {zipper, _}, _}, _}: Model.t = model; |
| 14 | + let num_rows = List.length(measured.piece_rows); |
| 15 | + let empty_row = row => { |
| 16 | + let result = List.nth_opt(List.rev(measured.piece_rows), row); |
| 17 | + switch (result) { |
| 18 | + | Some(value) => |
| 19 | + switch (value) { |
| 20 | + | [] => true |
| 21 | + | _ => false |
| 22 | + } |
| 23 | + | None => true // The row doesn't actually exist, hence it's empty |
| 24 | + }; |
| 25 | + }; |
| 26 | + let Point.{row, _} = Zipper.Caret.point(measured, zipper); |
| 27 | + let cursor_row_index = row; |
| 28 | + /* |
| 29 | + Recursively builds a list of line numbers for display, skipping empty rows. |
| 30 | +
|
| 31 | + Parameters: |
| 32 | + - row_index: Current row being processed (0-indexed) |
| 33 | + - line_count: The line number to assign to the current non-empty row |
| 34 | +
|
| 35 | + Returns: (line_numbers, cursor_line_number) where: |
| 36 | + - line_numbers: List where 0 indicates skip this row, non-zero is the display line number |
| 37 | + - cursor_line_number: The line number where the cursor is located (0 if not found yet) |
| 38 | + */ |
| 39 | + let rec processed_line_numbers = |
| 40 | + (row_index: int, line_count: int): (list(int), int) => |
| 41 | + if (row_index == num_rows) { |
| 42 | + ([], 0); |
| 43 | + } else { |
| 44 | + let is_row_empty = empty_row(row_index); |
| 45 | + let (returned_processed_list, returned_cursor_line_number) = |
| 46 | + is_row_empty |
| 47 | + ? processed_line_numbers(row_index + 1, line_count) |
| 48 | + : processed_line_numbers(row_index + 1, line_count + 1); |
| 49 | + let current_line_number = is_row_empty ? 0 : line_count; |
| 50 | + let cursor_line_number = |
| 51 | + if (returned_cursor_line_number == 0 && row_index == cursor_row_index) { |
| 52 | + line_count; |
| 53 | + } else { |
| 54 | + returned_cursor_line_number; |
| 55 | + }; |
| 56 | + ( |
| 57 | + [current_line_number] @ returned_processed_list, |
| 58 | + cursor_line_number, |
| 59 | + ); |
| 60 | + }; |
| 61 | + let (processed_list, cursor_line_number) = processed_line_numbers(0, 1); |
| 62 | + /* |
| 63 | + Converts a row index to its display text. |
| 64 | + Returns "\n" for empty rows, or the line number (absolute or relative) with newline. |
| 65 | + */ |
| 66 | + let index_to_text = (i): string => { |
| 67 | + let line_number = List.nth(processed_list, i); |
| 68 | + line_number == 0 |
| 69 | + ? "\n" // if this is a line we want to skip |
| 70 | + : { |
| 71 | + ( |
| 72 | + if (show_relative_numbers && selected) { |
| 73 | + string_of_int( |
| 74 | + abs(line_number - cursor_line_number) == 0 |
| 75 | + ? line_number : abs(line_number - cursor_line_number), |
| 76 | + ); |
| 77 | + } else { |
| 78 | + string_of_int(line_number); |
| 79 | + } |
| 80 | + ) |
| 81 | + ++ (i == num_rows ? "" : "\n"); // Add a line break if this is not the last row |
| 82 | + }; |
| 83 | + }; |
| 84 | + let index_to_span = (i): Node.t => { |
| 85 | + Node.span( |
| 86 | + ~attrs= |
| 87 | + i == row && selected ? [Attr.classes(["line-numbers-bold"])] : [], |
| 88 | + [Text(index_to_text(i))], |
| 89 | + ); |
| 90 | + }; |
| 91 | + [ |
| 92 | + Node.div( |
| 93 | + ~attrs=[ |
| 94 | + Attr.classes([ |
| 95 | + "code", |
| 96 | + "line-numbers", |
| 97 | + selected ? "line-numbers-selected" : "", |
| 98 | + ]), |
| 99 | + ], |
| 100 | + [ |
| 101 | + Node.span( |
| 102 | + ~attrs=[Attr.classes(["code-text", "line-numbers-text"])], |
| 103 | + List.init(num_rows, (i): Node.t => {index_to_span(i)}), |
| 104 | + ), |
| 105 | + ], |
| 106 | + ), |
| 107 | + ]; |
| 108 | + }; |
| 109 | +}; |
0 commit comments