annotate-snippets-rs
annotate-snippets-rs copied to clipboard
docs: Fix and add docs for `Level`
Have the documentation for Level reflect actual appearance.
The output from this file:
(43 lines)
//! Provides a user-friendly error if the given file has the string `tab`
//!
//! A silly utility to test annotate_snippets (and anstream)
use annotate_snippets::{Level, Renderer, Snippet};
use clap::Parser;
#[derive(Parser)]
struct Cli {
file_path: std::path::PathBuf,
}
fn main() {
let Cli { file_path } = Cli::parse();
let path_string = file_path.to_string_lossy().to_string();
// Bad error message; don't do this!
let contents =
std::fs::read_to_string(file_path).expect("Should have been able to read the file");
let renderer = Renderer::styled();
let message = if let Some(index) = contents.find("tab") {
Level::Error.title("no tabs allowed!").snippet(
Snippet::source(&contents)
.line_start(1)
.origin(&path_string)
.fold(true)
.annotation(
Level::Error
.span(index..(index + 3))
.label("Found `tab` here"),
)
.annotation(Level::Warning.span(index..(index + 3)).label("Warning"))
.annotation(Level::Info.span(index..(index + 3)).label("Info"))
.annotation(Level::Note.span(index..(index + 3)).label("Note"))
.annotation(Level::Help.span(index..(index + 3)).label("remove `tab`")),
)
} else {
Level::Info.title("No \"tabs\" found, congrats!")
};
anstream::println!("{}", renderer.render(message));
}
Results in this output:
Note: this is potentially inconsistent with cargo and clippy
Note: This is cyan rather than blue:
(found by using manual output while trying to work around #101:)
If a reviewer comments, I will change the wording to "cyan"
Thank you for wanting to make them consistent. However, I think the original doc comments mentioning the color and character was not the right call. This is primarily due to three reasons:
- Colors are configurable by end users
- Colors are platform dependent
- Characters being tied to a
Levelis for historical reasons and will be removed in the future when we matchrustc's idea ofPrimaryandSecondaryforAnnotations
I do think it is a good idea to document the Level text and default color, but graphically and in a way that won't go stale. This can be done by utilizing fixture tests and including the SVGs as doc comments. The rough process would be:
- Add a new
docsfolder inside oftests/fixturesfor the new tests - Add a basic example for each
Level - Link to the newly created SVGs in the same way as we do in the lib docs
As Level no longer affects annotation underlining and the color is determined by the Renderer, I'm going to go ahead and close this.