@@ -20,7 +20,12 @@ module Log = (val Logs.src_log src : Logs.LOG)
2020open Astring
2121open Misc
2222
23- type t = { command : string list ; output : Output .t list ; exit_code : int }
23+ type t = {
24+ command : string list ;
25+ output : Output .t list ;
26+ exit_code : int ;
27+ vpad : int ;
28+ }
2429
2530let dump_line ppf = function
2631 | #Output. t as o -> Output. dump ppf o
@@ -37,10 +42,20 @@ let dump ppf (t : t) =
3742 Fmt. (Dump. list Output. dump)
3843 t.output t.exit_code
3944
45+ let pp_vpad ppf t =
46+ let rec loop = function
47+ | 0 -> ()
48+ | n ->
49+ Fmt. pf ppf " \n " ;
50+ loop (Int. pred n)
51+ in
52+ loop t.vpad
53+
4054let pp_command ?(pad = 0 ) ppf (t : t ) =
4155 match t.command with
4256 | [] -> ()
4357 | l ->
58+ pp_vpad ppf t;
4459 let sep ppf () = Fmt. pf ppf " \\\n %a> " pp_pad pad in
4560 Fmt. pf ppf " %a$ %a\n " pp_pad pad Fmt. (list ~sep string ) l
4661
@@ -52,7 +67,7 @@ let pp ?pad ppf (t : t) =
5267 pp_lines (Output. pp ?pad) ppf t.output;
5368 pp_exit_code ?pad ppf t.exit_code
5469
55- let pad_of_lines = function
70+ let hpad_of_lines = function
5671 | [] -> 0
5772 | h :: _ ->
5873 let i = ref 0 in
@@ -61,22 +76,29 @@ let pad_of_lines = function
6176 done ;
6277 ! i
6378
64- let of_lines t =
65- let pad = pad_of_lines t in
79+ let of_lines ~syntax ~(loc : Location.t ) t =
80+ let pos = loc.loc_start in
81+ let hpad =
82+ match syntax with Syntax. Mli -> pos.pos_cnum + 2 | _ -> hpad_of_lines t
83+ in
6684 let unpad line =
67- if String. is_empty line then line
68- else if String. length line < pad then
69- Fmt. failwith " invalid padding: %S" line
70- else String. with_index_range line ~first: pad
85+ match syntax with
86+ | Syntax. Mli -> String. trim line
87+ | _ ->
88+ if String. is_empty line then line
89+ else if String. length line < hpad then
90+ Fmt. failwith " invalid padding: %S" line
91+ else String. with_index_range line ~first: hpad
7192 in
7293 let lines = List. map unpad t in
7394 let lines =
7495 Lexer_cram. token (Lexing. from_string (String. concat ~sep: " \n " lines))
7596 in
97+ let vpad = match syntax with Syntax. Mli -> 1 | _ -> 0 in
7698 Log. debug (fun l ->
77- l " Cram.of_lines (pad=%d) %a" pad Fmt. (Dump. list dump_line) lines);
99+ l " Cram.of_lines (pad=%d) %a" hpad Fmt. (Dump. list dump_line) lines);
78100 let mk command output exit_code =
79- { command; output = List. rev output; exit_code }
101+ { command; output = List. rev output; exit_code; vpad }
80102 in
81103 let rec command_cont acc = function
82104 | `Command_cont c :: t -> command_cont (c :: acc) t
@@ -101,8 +123,8 @@ let of_lines t =
101123 match lines with
102124 | `Command_first cmd :: t ->
103125 let cmd, t = command_cont [ cmd ] t in
104- (pad , aux cmd [] [] t)
105- | `Command cmd :: t -> (pad , aux [ cmd ] [] [] t)
126+ (hpad , aux cmd [] [] t)
127+ | `Command cmd :: t -> (hpad , aux [ cmd ] [] [] t)
106128 | [] -> (0 , [] )
107129 | `Output line :: _ ->
108130 if String. length line > 0 && line.[0 ] = '$' then
0 commit comments