|
| 1 | +(* Define a type for a range with start and finish indices *) |
| 2 | +type range = {start: int; finish: int} |
| 3 | + |
| 4 | +(* --- Helper function to find the 0-based line index containing a given 0-based character index --- *) |
| 5 | +let get_line_index_from_char_index code char_index = |
| 6 | + let lines = String.split_on_char '\n' code in |
| 7 | + let rec find_line_idx current_char_idx current_line_num remaining_lines = |
| 8 | + match remaining_lines with |
| 9 | + | [] -> |
| 10 | + max 0 (current_line_num - 1) |
| 11 | + (* If char_index is beyond the end, return last line index *) |
| 12 | + | line :: tl -> |
| 13 | + let line_length = String.length line in |
| 14 | + (* Check if char_index is within the current line (including the newline char) *) |
| 15 | + if |
| 16 | + char_index >= current_char_idx |
| 17 | + && char_index <= current_char_idx + line_length |
| 18 | + then current_line_num |
| 19 | + else |
| 20 | + (* Move to the next line, account for the newline character (+1) *) |
| 21 | + find_line_idx |
| 22 | + (current_char_idx + line_length + 1) |
| 23 | + (current_line_num + 1) tl |
| 24 | + in |
| 25 | + find_line_idx 0 0 lines |
| 26 | + |
| 27 | +(* --- Helper function to calculate the 0-based character index of the start of a given 0-based line index --- *) |
| 28 | +let get_char_index_from_line_index code target_line_index = |
| 29 | + let lines = String.split_on_char '\n' code in |
| 30 | + let rec calculate_start_index_impl current_char_idx current_line_num |
| 31 | + lines_to_process = |
| 32 | + if current_line_num >= target_line_index then current_char_idx |
| 33 | + else |
| 34 | + match lines_to_process with |
| 35 | + | [] -> current_char_idx (* Target line index is out of bounds *) |
| 36 | + | line :: tl -> |
| 37 | + (* Move past the current line and its newline character *) |
| 38 | + calculate_start_index_impl |
| 39 | + (current_char_idx + String.length line + 1) |
| 40 | + (current_line_num + 1) tl |
| 41 | + in |
| 42 | + calculate_start_index_impl 0 0 lines |
| 43 | + |
| 44 | +(* --- Main formatting function --- *) |
| 45 | +let format_code_snippet_cropped code (underline_range : range option) |
| 46 | + lines_around_annotation = |
| 47 | + let lines = String.split_on_char '\n' code in |
| 48 | + let total_lines = List.length lines in |
| 49 | + let formatted_output = Buffer.create (String.length code) in |
| 50 | + (* Initial capacity *) |
| 51 | + |
| 52 | + (* Determine the central line index for cropping *) |
| 53 | + let target_line_index = |
| 54 | + match underline_range with |
| 55 | + | Some {start; finish = _} -> get_line_index_from_char_index code start |
| 56 | + | None -> 0 (* Default to first line if no annotations *) |
| 57 | + in |
| 58 | + |
| 59 | + (* Determine the cropping window (0-based line indices) *) |
| 60 | + let start_line_index = max 0 (target_line_index - lines_around_annotation) in |
| 61 | + let end_line_index = |
| 62 | + min (total_lines - 1) (target_line_index + lines_around_annotation) |
| 63 | + in |
| 64 | + |
| 65 | + (* Keep track of the global character index corresponding to the start of the *current* line being iterated over *) |
| 66 | + let current_char_index = ref 0 in |
| 67 | + |
| 68 | + (* Iterate through all original lines to correctly track current_char_index *) |
| 69 | + List.iteri |
| 70 | + (fun original_line_idx line -> |
| 71 | + let line_length = String.length line in |
| 72 | + (* Check if the current original line is within our cropping window *) |
| 73 | + if |
| 74 | + original_line_idx >= start_line_index |
| 75 | + && original_line_idx <= end_line_index |
| 76 | + then ( |
| 77 | + let original_line_number = original_line_idx + 1 in |
| 78 | + (* 1-based for display *) |
| 79 | + let line_number_prefix = Printf.sprintf "%d + " original_line_number in |
| 80 | + let prefix_length = String.length line_number_prefix in |
| 81 | + |
| 82 | + (* Add the code line *) |
| 83 | + Buffer.add_string formatted_output line_number_prefix; |
| 84 | + Buffer.add_string formatted_output line; |
| 85 | + Buffer.add_char formatted_output '\n'; |
| 86 | + |
| 87 | + (* Prepare the annotation line buffer *) |
| 88 | + let annotation_line_buffer = |
| 89 | + Buffer.create (prefix_length + line_length) |
| 90 | + in |
| 91 | + Buffer.add_string annotation_line_buffer (String.make prefix_length ' '); |
| 92 | + |
| 93 | + (* Initial padding *) |
| 94 | + let has_annotation_on_this_line = ref false in |
| 95 | + |
| 96 | + (* Check each character position within this line for annotations *) |
| 97 | + for i = 0 to line_length - 1 do |
| 98 | + let global_char_index = !current_char_index + i in |
| 99 | + let annotation_char = ref ' ' in |
| 100 | + (* Default to space *) |
| 101 | + |
| 102 | + (* Check for underline using Option.iter *) |
| 103 | + Option.iter |
| 104 | + (fun {start; finish} -> |
| 105 | + if global_char_index >= start && global_char_index < finish then ( |
| 106 | + annotation_char := '-' (* '¯' *); |
| 107 | + (* Macron symbol *) |
| 108 | + has_annotation_on_this_line := true)) |
| 109 | + underline_range; |
| 110 | + |
| 111 | + Buffer.add_char annotation_line_buffer !annotation_char |
| 112 | + done; |
| 113 | + |
| 114 | + (* Add the annotation line to the main output if needed *) |
| 115 | + if !has_annotation_on_this_line then ( |
| 116 | + Buffer.add_buffer formatted_output annotation_line_buffer; |
| 117 | + Buffer.add_char formatted_output '\n')); |
| 118 | + |
| 119 | + (* Update the global character index to the start of the next line *) |
| 120 | + (* This happens regardless of whether the line was in the cropped window *) |
| 121 | + current_char_index := !current_char_index + line_length + 1 |
| 122 | + (* +1 for the newline *)) |
| 123 | + lines; |
| 124 | + |
| 125 | + Buffer.contents formatted_output |
0 commit comments