-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Implement a --show-source
setting
#698
Conversation
@@ -13,6 +13,7 @@ edition = "2021" | |||
name = "ruff" | |||
|
|||
[dependencies] | |||
annotate-snippets = { version = "0.9.1", features = ["color"] } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
rustc also uses this crate: rust-lang/rust#59346 (comment)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Another option is https://github.com/zkat/miette. SWC uses this crate to show pretty reports.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks like that uses https://crates.io/crates/ariadne under the hood.
\cc @squiddy |
Wow nice. Is it possible to include the fix annotations / suggestions here too, like Clippy does? |
That's what I'm investigating now :) |
I do like the look and feel! And I love not implementing this ourselves. |
@charliermarsh It looks like clippy relies on |
Could we vendor that crate? Or is it too closely coupled to Clippy / rustc to be extracted? |
This is As you can see, it depends on other private crates: [package]
name = "rustc_errors"
version = "0.0.0"
edition = "2021"
[lib]
[dependencies]
tracing = "0.1"
rustc_ast = { path = "../rustc_ast" }
rustc_ast_pretty = { path = "../rustc_ast_pretty" }
rustc_error_messages = { path = "../rustc_error_messages" }
rustc_serialize = { path = "../rustc_serialize" }
rustc_span = { path = "../rustc_span" }
rustc_macros = { path = "../rustc_macros" }
rustc_data_structures = { path = "../rustc_data_structures" }
rustc_target = { path = "../rustc_target" }
rustc_hir = { path = "../rustc_hir" }
rustc_lint_defs = { path = "../rustc_lint_defs" }
unicode-width = "0.1.4"
atty = "0.2"
termcolor = "1.0"
annotate-snippets = "0.9"
termize = "0.1.1"
serde = { version = "1.0.125", features = [ "derive" ] }
serde_json = "1.0.59"
[target.'cfg(windows)'.dependencies]
winapi = { version = "0.3", features = [ "handleapi", "synchapi", "winbase" ] } |
src/message.rs
Outdated
..Default::default() | ||
}, | ||
}; | ||
DisplayList::from(snippet).to_string() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that if we're going to use this crate for --show-source
, we should probably use the same crate and style even when --show-source
is false (and just omit the source code). What do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds good. I think we can set slices
to an empty vector when --show-source
is false.
Updated the screenshot. |
src/cli.rs
Outdated
@@ -94,6 +94,9 @@ pub struct Cli { | |||
/// The name of the file when passing it through stdin. | |||
#[arg(long)] | |||
pub stdin_filename: Option<String>, | |||
/// Show violations with source code. | |||
#[arg(long)] | |||
pub show_source: bool, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should add this to pyproject.toml
too, wdyt?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like I forgot to update user.rs
. Updated it.
src/message.rs
Outdated
location: check.location, | ||
end_location: check.end_location, | ||
}) | ||
.len(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this needs to be .chars().len()
, or we need to use the Rope
in locator
to get the start and end character positions.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed
src/message.rs
Outdated
); | ||
let slices = if self.show_source && self.source.is_some() && self.range.is_some() { | ||
vec![Slice { | ||
source: self.source.as_ref().unwrap(), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: maybe if let
to avoid some of the unwraps?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds good. Will fix.
Sweet! Will try to merge this tomorrow (or maybe the next day -- I have to move apartments tomorrow so might be busy). I just want to do some performance testing before merging. |
(Sorry, I'm a little behind, I haven't had a chance to benchmark yet.) |
@charliermarsh Not a problem at all :) |
@charliermarsh Thanks for the clean-up and merge! This feature does help when verifying new lint rules. |
See #525.