Print line numbers for -match-only option (#152)

This commit is contained in:
Rijnard van Tonder 2019-12-02 18:34:09 -07:00 committed by GitHub
parent e6810b9ebb
commit 3fee93b352
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 7 deletions

View File

@ -30,14 +30,18 @@ let pp_source_path ppf source_path =
| Some path -> Format.fprintf ppf "%s:" path
| None -> Format.fprintf ppf ""
let pp_line_number ppf start_line =
Format.fprintf ppf "%d:" start_line
let pp ppf (source_path, matches) =
if matches = [] then
()
else
let matched =
List.map matches ~f:(fun { matched; _ } ->
List.map matches ~f:(fun { matched; range; _ } ->
let matched = String.substr_replace_all matched ~pattern:"\n" ~with_:"\\n" in
Format.asprintf "%a%s" pp_source_path source_path matched)
let line = range.match_start.line in
Format.asprintf "%a%a%s" pp_source_path source_path pp_line_number line matched)
|> String.concat ~sep:"\n"
in
Format.fprintf ppf "%s@." matched

View File

@ -804,8 +804,8 @@ let%expect_test "print_single_line_matches" =
let command = Format.sprintf "%s %s" binary_path command_args in
read_expect_stdin_and_stdout command source
|> print_string;
[%expect_exact {|let ()
let ()
[%expect_exact {|2:let ()
3:let ()
|}]
@ -828,9 +828,9 @@ in
let command = Format.sprintf "%s %s" binary_path command_args in
read_expect_stdin_and_stdout command source
|> print_string;
[%expect_exact {|let ()
let\n\n ()
let ()
[%expect_exact {|2:let ()
3:let\n\n ()
7:let ()
|}];