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 gutter misalignments #139

Merged
merged 1 commit into from Dec 30, 2019
Merged

Fix gutter misalignments #139

merged 1 commit into from Dec 30, 2019

Conversation

dtolnay
Copy link
Contributor

@dtolnay dtolnay commented Dec 29, 2019

Fixes #130.

This code attempts to print a value of type codespan::LineNumber with padding and alignment:

write!(
writer,
"{line: >width$}",
line = line_number,
width = self.gutter_padding,
)?;

But the Display impl for LineNumber just ignores the requested padding and alignment:

impl fmt::Display for LineNumber {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.0)
}
}

Repro:

use codespan::Files;
use codespan_reporting::diagnostic::{Diagnostic, Label};
use codespan_reporting::term::emit;
use codespan_reporting::term::termcolor::{ColorChoice, StandardStream};

fn main() {
    let mut files = Files::new();
    let content = "\n\n\n\n\n\naaaaaaaaaa\n\nbbbbbbbbbb\ncccccccccc\n";
    let file = files.add("path/to", content);
    let diagnostic = Diagnostic::new_error("it failed", Label::new(file, 8..34, "error"));
    let writer = StandardStream::stderr(ColorChoice::Auto);
    let config = codespan_reporting::term::Config::default();
    emit(&mut writer.lock(), &config, &files, &diagnostic).unwrap();
}

Before:

error: it failed

    ┌── path/to:7:3 ───

 7 │   aaaaaaaaaa
    │ ╭───^
 8 │ │ 
 9 │ │ bbbbbbbbbb
 10 │ │ cccccccccc
    │ ╰─────^ error

After:

error: it failed

    ┌── path/to:7:3 ───

  7 │   aaaaaaaaaa
    │ ╭───^
  8 │ │ 
  9 │ │ bbbbbbbbbb
 10 │ │ cccccccccc
    │ ╰─────^ error

@dtolnay
Copy link
Contributor Author

dtolnay commented Dec 29, 2019

Would it be possible to get a patch release once this is merged?

@ebkalderon
Copy link
Contributor

@dtolnay Excellent sleuthing! I would love to see at least a patch release for this as well.

@brendanzab
Copy link
Owner

Ahhhhh thanks for this! ❤️

@brendanzab
Copy link
Owner

We should probably add a test for this... 🤔

@brendanzab brendanzab merged commit 23f88c3 into brendanzab:master Dec 30, 2019
@dtolnay dtolnay mentioned this pull request Jan 1, 2020
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.

Incorrect number alignment in rendered diagnostics
3 participants