From 81797f86cc84afb83cac71c460142bc6b78c701f Mon Sep 17 00:00:00 2001 From: Anurag Soni Date: Tue, 21 Aug 2018 20:28:40 -0400 Subject: [PATCH] Truncated view for blocks longer than 10 lines Signed-off-by: Anurag Soni --- src/errors.ml | 43 +++++++++++++++---- .../blackbox-tests/test-cases/intf-only/run.t | 4 +- .../test-cases/missing-loc-run/run.t | 12 +++--- .../test-cases/no-installable-mode/run.t | 10 ++--- .../test-cases/several-packages/run.t | 14 +++--- .../test-cases/too-many-parens/run.t | 19 +++++--- test/blackbox-tests/test-cases/variants/run.t | 2 +- 7 files changed, 69 insertions(+), 35 deletions(-) diff --git a/src/errors.ml b/src/errors.ml index 302d0a73..2a3a8061 100644 --- a/src/errors.ml +++ b/src/errors.ml @@ -2,6 +2,10 @@ open! Stdune exception Already_reported +let max_lines_to_print_in_full = 10 + +let context_lines = 2 + let err_buf = Buffer.create 128 let err_ppf = Format.formatter_of_buffer err_buf let kerrf fmt ~f = @@ -75,14 +79,37 @@ let print ppf loc = Format.fprintf pp "%s\n%*s\n" line stop_c (String.make len '^') - else if num_lines <= 10 then - let lines = file_lines path ~start:start.pos_lnum ~stop:stop.pos_lnum in - let last_lnum = Option.map ~f:fst (List.last lines) in - let padding_width = Option.value_exn - (Option.map ~f:String.length last_lnum) in - List.iter ~f:(fun (lnum, l) -> - Format.fprintf pp "%*s: %s\n" padding_width lnum l) - lines + else + let get_padding lines = + let (lnum, _) = Option.value_exn (List.last lines) in + String.length lnum + in + let print_ellipsis padding_width = + (* We add 2 to the width of max line to account for + the extra space and the `|` character at the end + of a line number *) + let line = String.make (padding_width + 2) '.' in + Format.fprintf pp "%s\n" line + in + let print_lines lines padding_width = + List.iter ~f:(fun (lnum, l) -> + Format.fprintf pp "%*s | %s\n" padding_width lnum l) lines; + in + if num_lines <= max_lines_to_print_in_full then + let lines = file_lines path ~start:start.pos_lnum ~stop:stop.pos_lnum in + print_lines lines (get_padding lines) + else + (* We need to send the padding width from the last four lines + so the two blocks of lines align if they have different number + of digits in their line numbers *) + let first_shown_lines = file_lines path ~start:(start.pos_lnum) + ~stop:(start.pos_lnum + context_lines) in + let last_shown_lines = file_lines path ~start:(stop.pos_lnum - context_lines) + ~stop:(stop.pos_lnum) in + let padding_width = get_padding last_shown_lines in + (print_lines first_shown_lines padding_width; + print_ellipsis padding_width; + print_lines last_shown_lines padding_width) in Format.fprintf ppf "@{File \"%s\", line %d, characters %d-%d:@}@\n%a" diff --git a/test/blackbox-tests/test-cases/intf-only/run.t b/test/blackbox-tests/test-cases/intf-only/run.t index cd7dc429..7d3665c5 100644 --- a/test/blackbox-tests/test-cases/intf-only/run.t +++ b/test/blackbox-tests/test-cases/intf-only/run.t @@ -24,8 +24,8 @@ Errors: $ dune build --display short --root a foo.cma Entering directory 'a' File "dune", line 1, characters 0-21: - 1: (library - 2: (name foo)) + 1 | (library + 2 | (name foo)) Warning: Some modules don't have an implementation. You need to add the following field to this stanza: diff --git a/test/blackbox-tests/test-cases/missing-loc-run/run.t b/test/blackbox-tests/test-cases/missing-loc-run/run.t index 3ae2de65..6538a854 100644 --- a/test/blackbox-tests/test-cases/missing-loc-run/run.t +++ b/test/blackbox-tests/test-cases/missing-loc-run/run.t @@ -3,9 +3,9 @@ Exact path provided by the user: $ dune runtest --root precise-path Entering directory 'precise-path' File "dune", line 1, characters 0-49: - 1: (alias - 2: (name runtest) - 3: (action (run ./foo.exe))) + 1 | (alias + 2 | (name runtest) + 3 | (action (run ./foo.exe))) Error: No rule found for foo.exe [1] @@ -24,8 +24,8 @@ Path in deps field of alias stanza $ dune runtest --root alias-deps-field Entering directory 'alias-deps-field' File "dune", line 1, characters 0-38: - 1: (alias - 2: (name runtest) - 3: (deps foobar)) + 1 | (alias + 2 | (name runtest) + 3 | (deps foobar)) Error: No rule found for foobar [1] diff --git a/test/blackbox-tests/test-cases/no-installable-mode/run.t b/test/blackbox-tests/test-cases/no-installable-mode/run.t index 3a29387a..e34aa22a 100644 --- a/test/blackbox-tests/test-cases/no-installable-mode/run.t +++ b/test/blackbox-tests/test-cases/no-installable-mode/run.t @@ -3,11 +3,11 @@ message is displayed: $ dune build --root=public --display=short File "jbuild", line 4, characters 2-74: - 4: ( - 5: (name mylib) - 6: (public_name mylib) - 7: (modes (shared_object)) - 8: ) + 4 | ( + 5 | (name mylib) + 6 | (public_name mylib) + 7 | (modes (shared_object)) + 8 | ) Error: No installable mode found for this executable. One of the following modes is required: - exe diff --git a/test/blackbox-tests/test-cases/several-packages/run.t b/test/blackbox-tests/test-cases/several-packages/run.t index 6e4bc2d7..1a5558a5 100644 --- a/test/blackbox-tests/test-cases/several-packages/run.t +++ b/test/blackbox-tests/test-cases/several-packages/run.t @@ -5,9 +5,9 @@ displayed. This can happen for: $ dune build --root executable File "dune", line 1, characters 0-43: - 1: (executable - 2: (public_name an_executable) - 3: ) + 1 | (executable + 2 | (public_name an_executable) + 3 | ) Error: I can't determine automatically which package this stanza is for. I have the choice between these ones: - pkg1 (because of pkg1.opam) @@ -32,10 +32,10 @@ displayed. This can happen for: $ dune build --root install File "dune", line 1, characters 0-44: - 1: (install - 2: (section etc) - 3: (files file.conf) - 4: ) + 1 | (install + 2 | (section etc) + 3 | (files file.conf) + 4 | ) Error: I can't determine automatically which package this stanza is for. I have the choice between these ones: - pkg1 (because of pkg1.opam) diff --git a/test/blackbox-tests/test-cases/too-many-parens/run.t b/test/blackbox-tests/test-cases/too-many-parens/run.t index 1c67b53a..ddf1f7a3 100644 --- a/test/blackbox-tests/test-cases/too-many-parens/run.t +++ b/test/blackbox-tests/test-cases/too-many-parens/run.t @@ -3,11 +3,11 @@ are readable. $ dune build --root a File "dune", line 1, characters 12-72: - 1: (executable ( - 2: (name hello) - 3: (public_name hello) - 4: (libraries (lib)) - 5: )) + 1 | (executable ( + 2 | (name hello) + 3 | (public_name hello) + 4 | (libraries (lib)) + 5 | )) Error: Atom expected Hint: dune files require less parentheses than jbuild files. If you just converted this file from a jbuild file, try removing these parentheses. @@ -31,9 +31,16 @@ are readable. If you just converted this file from a jbuild file, try removing these parentheses. [1] -Checking that extra long stanzas (over 10 lines) are not printed +Checking that extra long stanzas (over 10 lines) are truncated in the middle, and the two blocks are aligned. $ dune build --root d File "dune", line 3, characters 13-192: + 3 | (libraries (a + 4 | b + 5 | c + .... + 12 | j + 13 | k + 14 | l) Error: 'select' expected Hint: dune files require less parentheses than jbuild files. If you just converted this file from a jbuild file, try removing these parentheses. diff --git a/test/blackbox-tests/test-cases/variants/run.t b/test/blackbox-tests/test-cases/variants/run.t index 376e158d..17f3376f 100644 --- a/test/blackbox-tests/test-cases/variants/run.t +++ b/test/blackbox-tests/test-cases/variants/run.t @@ -9,7 +9,7 @@ Variant feature is auto enabled when virtual_modules is used $ dune build --root variants-using File "dune-project", line 2, characters 42-45: - 2: + 2 | Error: Version 0.1 of in_development_do_not_use_variants is not supported. Supported versions: - 0.0