diff --git a/Cargo.lock b/Cargo.lock index ba4dccc5aa4e0..1d1c0b1983b56 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2239,7 +2239,6 @@ dependencies = [ "ruff_python_parser", "ruff_python_stdlib", "ruff_python_trivia", - "ruff_text_size", "ruff_workspace", "schemars", "serde", diff --git a/crates/ruff_dev/Cargo.toml b/crates/ruff_dev/Cargo.toml index d5ccc937fd9c5..632c12f473786 100644 --- a/crates/ruff_dev/Cargo.toml +++ b/crates/ruff_dev/Cargo.toml @@ -22,7 +22,6 @@ ruff_python_formatter = { workspace = true } ruff_python_parser = { workspace = true } ruff_python_stdlib = { workspace = true } ruff_python_trivia = { workspace = true } -ruff_text_size = { workspace = true } ruff_workspace = { workspace = true, features = ["schemars"] } anyhow = { workspace = true } diff --git a/crates/ruff_dev/src/print_tokens.rs b/crates/ruff_dev/src/print_tokens.rs index c767727fdd2b1..2c83affbb5f39 100644 --- a/crates/ruff_dev/src/print_tokens.rs +++ b/crates/ruff_dev/src/print_tokens.rs @@ -8,7 +8,6 @@ use anyhow::Result; use ruff_linter::source_kind::SourceKind; use ruff_python_ast::PySourceType; use ruff_python_parser::parse_unchecked_source; -use ruff_text_size::Ranged; #[derive(clap::Args)] pub(crate) struct Args { @@ -27,12 +26,7 @@ pub(crate) fn main(args: &Args) -> Result<()> { })?; let parsed = parse_unchecked_source(source_kind.source_code(), source_type); for token in parsed.tokens() { - println!( - "{start:#?} {kind:#?} {end:#?}", - start = token.start(), - end = token.end(), - kind = token.kind(), - ); + println!("{token:#?}"); } Ok(()) } diff --git a/crates/ruff_python_parser/src/token.rs b/crates/ruff_python_parser/src/token.rs index cedda221ba230..4e6ee1bcc13fe 100644 --- a/crates/ruff_python_parser/src/token.rs +++ b/crates/ruff_python_parser/src/token.rs @@ -16,7 +16,7 @@ use ruff_python_ast::str_prefix::{ use ruff_python_ast::{AnyStringFlags, BoolOp, Int, IpyEscapeKind, Operator, StringFlags, UnaryOp}; use ruff_text_size::{Ranged, TextRange}; -#[derive(Debug, Clone, Copy, PartialEq, Eq)] +#[derive(Clone, Copy, PartialEq, Eq)] pub struct Token { /// The kind of the token. kind: TokenKind, @@ -81,6 +81,26 @@ impl Ranged for Token { } } +impl fmt::Debug for Token { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "{:?} {:?}", self.kind, self.range)?; + if !self.flags.is_empty() { + f.write_str(" (flags = ")?; + let mut first = true; + for (name, _) in self.flags.iter_names() { + if first { + first = false; + } else { + f.write_str(" | ")?; + } + f.write_str(name)?; + } + f.write_str(")")?; + } + Ok(()) + } +} + /// A kind of a token. #[derive(Copy, Clone, PartialEq, Eq, Hash, Debug, PartialOrd, Ord)] pub enum TokenKind { diff --git a/crates/ruff_wasm/src/lib.rs b/crates/ruff_wasm/src/lib.rs index e2b86508a205f..9a7e30f0df88b 100644 --- a/crates/ruff_wasm/src/lib.rs +++ b/crates/ruff_wasm/src/lib.rs @@ -261,7 +261,7 @@ impl Workspace { pub fn tokens(&self, contents: &str) -> Result { let parsed = parse_unchecked(contents, Mode::Module); - Ok(format!("{:#?}", parsed.tokens())) + Ok(format!("{:#?}", parsed.tokens().as_ref())) } }