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

Fix formatting bug when an empty span is not aligned to a char boundary #314

Merged
merged 2 commits into from
Nov 9, 2023

Conversation

Benjamin-L
Copy link
Contributor

This bug was originally identified in #312 (comment).

The bad output looked like this:

   ╭─[bad_file.rs:2:4]
 1 │ source
 2 │   👼🏼text
   ·   ─▲
   ·    ╰── this bit here
 3 │     here
   ╰────

Another possible option for the output (other than the one I implemented) would be

   ╭─[bad_file.rs:2:4]
 1 │ source
 2 │   👼🏼text
   ·    ▲
   ·    ╰── this bit here
 3 │     here
   ╰────

I prefer pointing the at the first column of the wide char rather than the second, but can see an argument that pointing at the second column is visually communicates that the span is unaligned. I think that's pretty niche, and would be surprised if any users are generating unaligned spans on purpose.


I also included a minor refactor in the PR, removing an unnecessary branch. The branch was always taken both before and after the bugfix. I can remove that bit if you want.

Previous output looked like this:

---- single_line_with_wide_char_unaligned_span_empty stdout ----
Error: oops::my::bad

  × oops!
   ╭─[bad_file.rs:2:4]
 1 │ source
 2 │   👼🏼text
   ·   ─▲
   ·    ╰── this bit here
 3 │     here
   ╰────
  help: try doing it better next time?

Note that the .max(start + 1) term is still necessary in the nonempty
branch, since it's possible to have a nonempty span covering zero-width
text.
start > end in all cases.
}
underlines.push_str(
&format!(
"{:width$}{}{}{}",
Copy link
Contributor

Choose a reason for hiding this comment

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

I just fixed a bug in #316 that also uses these :width$ format specifiers. It seems that they're not such a good idea when working with ansi. I do see that you apply style after you do that here though, so it might work fine here actually. I just noticed it in this diff as well and maybe this should be checked everywhere 🤔

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 believe it's fine in this case, since the actual format arg is "". format!("{:width$}", "", width = x) should just expand to x space chars. I might be misunderstanding what the problem is, but I don't think this is the same bug as the one you found in #316.

Copy link
Contributor

Choose a reason for hiding this comment

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

ohh yes that is correct, I do that too somewhere I think. You're right, this works :D

@zkat zkat merged commit 3d6f903 into zkat:main Nov 9, 2023
11 checks passed
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