Skip to content

Commit

Permalink
[messageReporting] add display_source_at util
Browse files Browse the repository at this point in the history
  • Loading branch information
kLabz committed Jan 22, 2024
1 parent 09203c6 commit edec597
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/compiler/messageReporting.ml
Original file line number Diff line number Diff line change
Expand Up @@ -183,14 +183,15 @@ let compiler_pretty_message_string com ectx cm =
(* Error source *)
if display_source then out := List.fold_left (fun out (l, line) ->
let nb_len = String.length (string_of_int l) in
let gutter = gutter_len - nb_len - 1 in

(* Replace tabs with 1 space to avoid column misalignments *)
let line = String.concat " " (ExtString.String.nsplit line "\t") in
let len = String.length line in

out ^ Printf.sprintf "%s%s | %s\n"
(* left-padded line number *)
(String.make (gutter_len-nb_len-1) ' ')
(if gutter < 1 then "" else String.make gutter ' ')
(if l = 0 then "-" else Printf.sprintf "%d" l)
(* Source code at that line *)
(
Expand Down Expand Up @@ -308,6 +309,15 @@ let get_max_line max_lines messages =
else max_lines
) max_lines messages

let display_source_at com p =
let absolute_positions = Define.defined com.defines Define.MessageAbsolutePositions in
let ectx = create_error_context absolute_positions in
let msg = make_compiler_message "" p 0 MessageKind.DKCompilerMessage MessageSeverity.Information in
ectx.max_lines <- get_max_line ectx.max_lines [msg];
match compiler_pretty_message_string com ectx msg with
| None -> ()
| Some s -> prerr_endline s

exception ConfigError of string

let get_formatter com def default =
Expand Down

0 comments on commit edec597

Please sign in to comment.