Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Truncated view for blocks longer than 10 lines #1168

Merged
merged 1 commit into from
Sep 3, 2018
Merged

Truncated view for blocks longer than 10 lines #1168

merged 1 commit into from
Sep 3, 2018

Conversation

anuragsoni
Copy link
Contributor

Signed-off-by: Anurag Soni anuragsoni.13@gmail.com

@anuragsoni
Copy link
Contributor Author

@emillon Just to start the discussion about what will be suitable for stanzas longer than 10 lines.

@emillon
Copy link
Collaborator

emillon commented Aug 22, 2018

I saw that elm and bucklescript both use | as a separator before lines - I agree that it looks better (maybe with a space before).

I had a look at how bucklescript does its formatting (see also these tests) and the main points are:

  1. it truncates the middle instead of the end
  2. it includes some extra context lines
  3. it trims leading whitespace, if any

I don't think (2) and (3) are very useful for now, but (1) seems interesting. What are your thoughts on that?

@anuragsoni anuragsoni changed the title Truncated view for blocks longer than 10 lines [WIP] Truncated view for blocks longer than 10 lines Aug 22, 2018
@anuragsoni
Copy link
Contributor Author

anuragsoni commented Aug 22, 2018

I agree about (2). I don't think extra context lines will help too much when it comes to these fields as we are able to show the exact stanza that has a problem in most cases.

For (3) I'm guessing at some point we might be able to do the same thing that dune unstable-fmt does for formatting?

I like the idea of truncating the middle portions as the trailing ... isn't easy to see. So it'd be like following:

File "dune", line 3, characters 13-192:
   3 |   (libraries (a
   4 |               b
   5 |               c
   6 |               d
   7 |               e
   8 |               f
   9 |               g

-----

  20 |               h
  31 |               i
  32 |               j)

@emillon
Copy link
Collaborator

emillon commented Aug 23, 2018

Yes, that seems right. I agree that there'll probably be a nice synergy with dune fmt at some point.

@anuragsoni anuragsoni requested a review from a user August 24, 2018 01:52
@anuragsoni anuragsoni changed the title [WIP] Truncated view for blocks longer than 10 lines Truncated view for blocks longer than 10 lines Aug 24, 2018
@anuragsoni
Copy link
Contributor Author

@emillon I've tried to come up with an approach to the 1st point discussed above (truncating the portions in the middle)

3 | (libraries (a
4 | b
5 | c
6 | d
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that this should be padded with one extra space so that the vertical bars line up with below (this file is a great test 👍 ).

Copy link
Contributor Author

@anuragsoni anuragsoni Aug 24, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah good catch. I think the problem was that I need to pad the first block of lines with the padding width from the last block of lines for them to line up. I hadn't handled the case that the first four lines might have less digits than the last four 😄

I've updated to take that into account

5 | c
6 | d

------
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's probably a bit too large, can you align it to just before the position of | as in your comment before? I'm also curious about using ... since that's more associated with cutting parts out (and that's what bucklescript uses).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. I've updated to use ... That indeed looks nicer. Also they should now be aligned with the |

$ dune build --root d
File "dune", line 3, characters 13-192:
3 | (libraries (a
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I notice that the truncated view always displays 11 lines. This means that 10 lines will be displayed in full, but 11 lines will be displayed with 3 lines missing, while keeping the same number of lines. It's a bit unfortunate. I can see three things that we can do:

  • increase the threshold (display more messages in full)
  • remove some context lines (e.g. switch to 3)
  • remove the empty lines

I think that 3 context lines would be enough, but that's up to you.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd think that there will always be a scenario where we transition from a full stanza to truncated view where we'd see less lines in the truncated version. I think a combination of increasing the threshold and removing some context lines (I agree that 3 should be enough) will be a good start. I'll update later today

src/errors.ml Outdated
let print_lines ?(trailing_line=false) lines padding_width =
List.iter ~f:(fun (lnum, l) ->
Format.fprintf pp "%*s | %s\n" padding_width lnum l) lines;
if trailing_line then
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That boolean parameter is control coupling. You can remove it by extracting a print_ellipsis function and calling it between the two print_line call sites below.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good catch. I'll update. It's a leftover from a version I had when I wasn't passing this function the extracted lines

src/errors.ml Outdated
let line = String.make (padding_width + 2) '.' in
Format.fprintf pp "\n%s\n\n" line
in
if num_lines <= 10 then
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think about extracting this constant out of this function ? Like let maximum_lines_to_print_in_full or something. Same goes for the 3 below, you can pull out a let context_lines = 4

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good point. I'll address this

src/errors.ml Outdated
(* 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_four = file_lines path ~start:(start.pos_lnum)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these names are a bit too specific - especially if they end up being 3 lines :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree. I couldn't think of any better names at that point since start and stop were already taken. I'll rename this 😄

src/errors.ml Outdated
else
let get_padding lines =
let last_lnum = Option.map ~f:fst (List.last lines) in
Option.value_exn (Option.map ~f:String.length last_lnum)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you call value_exn on the first option, you'll be able to remove the second Option.map.

List.iter ~f:(fun (lnum, l) ->
Format.fprintf pp "%*s: %s\n" padding_width lnum l)
lines
else
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can solve this after the other comments, but I believe that it's the point where this function has enough logic in it. It should be possible to extract a type that determines how to print a file part:

type display_style =
  | No_displayOne_line of int (* line number *)Block of int * int (* start, stop *)

Then it's possible to split the large function into one that determines the display style, and one function per display style. As a bonus, things like determining the padding size can be pulled out and factored a bit.

What do you think about that?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the idea. I'll try this after I address the other comments!

@anuragsoni anuragsoni changed the title Truncated view for blocks longer than 10 lines [WIP] Truncated view for blocks longer than 10 lines Aug 24, 2018
@emillon
Copy link
Collaborator

emillon commented Aug 31, 2018

Hi Anurag, just checking on this - there's no rush but is there anything that I can help you with?

@anuragsoni
Copy link
Contributor Author

Hi! I should have replied earlier!! I had pushed some changes to address all comments apart from adding the new type. I was hoping to get some feedback on that before I refactor to create a new type!

I figured you were busy so thought i'd wait 😄

@emillon
Copy link
Collaborator

emillon commented Aug 31, 2018

Ah, it's good that I checked then :) The changes look good, you can have a go at the new type if you're still up for it!

@anuragsoni
Copy link
Contributor Author

I will definitely try to refactor with a new type. I'll be on vacation for a few days so should be able to update with the new type sometime next week. Also, I can update this branch with the latest master later today if you prefer, so there can be a new PR with the refactor to a new type that should be transparent change for the implementation

@emillon
Copy link
Collaborator

emillon commented Aug 31, 2018

As you prefer; either we can merge this now or after, with the refactoring.

@anuragsoni
Copy link
Contributor Author

I will update this PR later today after syncing this with master. Maybe we can merge this before to avoid conflicts? The refactor should be transparent to all tests so I can do that next week (we can have a separate issue for that, and I can pick that up)

What do you think?

@emillon
Copy link
Collaborator

emillon commented Aug 31, 2018

That works for me!

@anuragsoni
Copy link
Contributor Author

@emillon i've updated the PR to be in sync with the current master.

@rgrinberg
Copy link
Member

@anuragsoni make sure to remove the WIP from the title when this is ready

@anuragsoni anuragsoni changed the title [WIP] Truncated view for blocks longer than 10 lines Truncated view for blocks longer than 10 lines Sep 1, 2018
@anuragsoni
Copy link
Contributor Author

@rgrinberg done! I don't have access to a computer this weekend so I can't rebase with current master

@emillon
Copy link
Collaborator

emillon commented Sep 3, 2018

Great, I'll rebase and merge. Thanks!

Signed-off-by: Anurag Soni <anuragsoni.13@gmail.com>
@emillon emillon merged commit 8dbfd66 into ocaml:master Sep 3, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants