diff --git a/Cargo.lock b/Cargo.lock index f7698c8d1f3d87..32dd41d0d3f5ae 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2043,9 +2043,7 @@ version = "0.0.0" dependencies = [ "anyhow", "log", - "ruff_python_ast", "ruff_text_size", - "rustpython-parser", "serde", ] @@ -2164,7 +2162,7 @@ dependencies = [ [[package]] name = "ruff_text_size" version = "0.0.0" -source = "git+https://github.com/charliermarsh/RustPython.git?rev=834a2c356bbb31c33af900d9d02ce0228a0f6145#834a2c356bbb31c33af900d9d02ce0228a0f6145" +source = "git+https://github.com/charliermarsh/RustPython.git?rev=50b8570bc262098b84dac86092133d3ee26caf23#50b8570bc262098b84dac86092133d3ee26caf23" dependencies = [ "schemars", "serde", @@ -2235,7 +2233,7 @@ dependencies = [ [[package]] name = "rustpython-ast" version = "0.2.0" -source = "git+https://github.com/charliermarsh/RustPython.git?rev=834a2c356bbb31c33af900d9d02ce0228a0f6145#834a2c356bbb31c33af900d9d02ce0228a0f6145" +source = "git+https://github.com/charliermarsh/RustPython.git?rev=50b8570bc262098b84dac86092133d3ee26caf23#50b8570bc262098b84dac86092133d3ee26caf23" dependencies = [ "num-bigint", "ruff_text_size", @@ -2244,7 +2242,7 @@ dependencies = [ [[package]] name = "rustpython-common" version = "0.2.0" -source = "git+https://github.com/charliermarsh/RustPython.git?rev=834a2c356bbb31c33af900d9d02ce0228a0f6145#834a2c356bbb31c33af900d9d02ce0228a0f6145" +source = "git+https://github.com/charliermarsh/RustPython.git?rev=50b8570bc262098b84dac86092133d3ee26caf23#50b8570bc262098b84dac86092133d3ee26caf23" dependencies = [ "ascii", "bitflags", @@ -2270,7 +2268,7 @@ dependencies = [ [[package]] name = "rustpython-compiler-core" version = "0.2.0" -source = "git+https://github.com/charliermarsh/RustPython.git?rev=834a2c356bbb31c33af900d9d02ce0228a0f6145#834a2c356bbb31c33af900d9d02ce0228a0f6145" +source = "git+https://github.com/charliermarsh/RustPython.git?rev=50b8570bc262098b84dac86092133d3ee26caf23#50b8570bc262098b84dac86092133d3ee26caf23" dependencies = [ "bitflags", "bstr 0.2.17", @@ -2285,7 +2283,7 @@ dependencies = [ [[package]] name = "rustpython-parser" version = "0.2.0" -source = "git+https://github.com/charliermarsh/RustPython.git?rev=834a2c356bbb31c33af900d9d02ce0228a0f6145#834a2c356bbb31c33af900d9d02ce0228a0f6145" +source = "git+https://github.com/charliermarsh/RustPython.git?rev=50b8570bc262098b84dac86092133d3ee26caf23#50b8570bc262098b84dac86092133d3ee26caf23" dependencies = [ "ahash", "anyhow", diff --git a/Cargo.toml b/Cargo.toml index bff073d0397ba0..ca51d38bfdef40 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -30,13 +30,13 @@ path-absolutize = { version = "3.0.14" } proc-macro2 = { version = "1.0.51" } quote = { version = "1.0.23" } regex = { version = "1.7.1" } -ruff_text_size = { git = "https://github.com/charliermarsh/RustPython.git", rev = "834a2c356bbb31c33af900d9d02ce0228a0f6145" } +ruff_text_size = { git = "https://github.com/charliermarsh/RustPython.git", rev = "50b8570bc262098b84dac86092133d3ee26caf23" } rustc-hash = { version = "1.1.0" } -rustpython-common = { git = "https://github.com/charliermarsh/RustPython.git", rev = "834a2c356bbb31c33af900d9d02ce0228a0f6145" } +rustpython-common = { git = "https://github.com/charliermarsh/RustPython.git", rev = "50b8570bc262098b84dac86092133d3ee26caf23" } rustpython-parser = { features = [ "lalrpop", "serde", -], git = "https://github.com/charliermarsh/RustPython.git", rev = "834a2c356bbb31c33af900d9d02ce0228a0f6145" } +], git = "https://github.com/charliermarsh/RustPython.git", rev = "50b8570bc262098b84dac86092133d3ee26caf23" } schemars = { version = "0.8.12" } serde = { version = "1.0.152", features = ["derive"] } serde_json = { version = "1.0.93", features = ["preserve_order"] } diff --git a/crates/ruff/src/jupyter/notebook.rs b/crates/ruff/src/jupyter/notebook.rs index fe96aeccfa153b..85a97dd6e6b5d1 100644 --- a/crates/ruff/src/jupyter/notebook.rs +++ b/crates/ruff/src/jupyter/notebook.rs @@ -18,7 +18,7 @@ pub const JUPYTER_NOTEBOOK_EXT: &str = "ipynb"; /// Jupyter Notebook indexing table /// /// When we lint a jupyter notebook, we have to translate the row/column based on -/// [`crate::message::Location`] +/// [`ruff_text_size::TextSize`] /// to jupyter notebook cell/row/column. #[derive(Debug, Eq, PartialEq)] pub struct JupyterIndex { diff --git a/crates/ruff/src/rules/pycodestyle/rules/doc_line_too_long.rs b/crates/ruff/src/rules/pycodestyle/rules/doc_line_too_long.rs index 0a354c2b7f40a1..f7e185871a736d 100644 --- a/crates/ruff/src/rules/pycodestyle/rules/doc_line_too_long.rs +++ b/crates/ruff/src/rules/pycodestyle/rules/doc_line_too_long.rs @@ -58,7 +58,7 @@ pub(crate) fn doc_line_too_long(line: &Line, settings: &Settings) -> Option Option LogicalLine<'a> { } } - /// Returns the [`Location`] of the first token on the line or [`None`]. + /// Returns the [`TextSize`] of the first token on the line or [`None`]. pub fn first_token_location(&self) -> Option { self.tokens().first().map(|t| t.start()) } diff --git a/crates/ruff/src/rules/pycodestyle/snapshots/ruff__rules__pycodestyle__tests__E501_E501.py.snap b/crates/ruff/src/rules/pycodestyle/snapshots/ruff__rules__pycodestyle__tests__E501_E501.py.snap index 5bbb817ae7ec1e..c7e156569d4600 100644 --- a/crates/ruff/src/rules/pycodestyle/snapshots/ruff__rules__pycodestyle__tests__E501_E501.py.snap +++ b/crates/ruff/src/rules/pycodestyle/snapshots/ruff__rules__pycodestyle__tests__E501_E501.py.snap @@ -10,11 +10,11 @@ E501.py:5:89: E501 Line too long (123 > 88 characters) 8 | """ | -E501.py:16:86: E501 Line too long (95 > 88 characters) +E501.py:16:85: E501 Line too long (95 > 88 characters) | 16 | _ = "---------------------------------------------------------------------------AAAAAAA" 17 | _ = "---------------------------------------------------------------------------亜亜亜亜亜亜亜" - | ^^^^^ E501 + | ^^^^^^^ E501 | E501.py:25:89: E501 Line too long (127 > 88 characters) diff --git a/crates/ruff/src/rules/pyflakes/rules/unused_variable.rs b/crates/ruff/src/rules/pyflakes/rules/unused_variable.rs index 0ee624f534fa5c..ca4230563ee602 100644 --- a/crates/ruff/src/rules/pyflakes/rules/unused_variable.rs +++ b/crates/ruff/src/rules/pyflakes/rules/unused_variable.rs @@ -61,7 +61,7 @@ impl AlwaysAutofixableViolation for UnusedVariable { } } -/// Return the start and end [`Location`] of the token after the next match of +/// Return the [`TextRange`] of the token after the next match of /// the predicate, skipping over any bracketed expressions. fn match_token_after(located: &Located, locator: &Locator, f: F) -> TextRange where @@ -124,7 +124,7 @@ where unreachable!("No token after matched"); } -/// Return the start and end [`Location`] of the token matching the predicate, +/// Return the [`TextRange`] of the token matching the predicate, /// skipping over any bracketed expressions. fn match_token(located: &Located, locator: &Locator, f: F) -> TextRange where @@ -190,7 +190,7 @@ enum DeletionKind { } /// Generate a [`Edit`] to remove an unused variable assignment, given the -/// enclosing [`Stmt`] and the [`Range`] of the variable binding. +/// enclosing [`Stmt`] and the [`TextRange`] of the variable binding. fn remove_unused_variable( stmt: &Stmt, range: TextRange, diff --git a/crates/ruff/src/rules/pyupgrade/fixes.rs b/crates/ruff/src/rules/pyupgrade/fixes.rs index 77a60467107829..7678236b64fec9 100644 --- a/crates/ruff/src/rules/pyupgrade/fixes.rs +++ b/crates/ruff/src/rules/pyupgrade/fixes.rs @@ -12,7 +12,7 @@ use ruff_python_ast::source_code::{Locator, Stylist}; use crate::cst::matchers::match_module; -/// Safely adjust the indentation of the indented block at [`Range`]. +/// Safely adjust the indentation of the indented block at [`TextRange`]. pub fn adjust_indentation( range: TextRange, indentation: &str, diff --git a/crates/ruff_cli/src/cache.rs b/crates/ruff_cli/src/cache.rs index 1adec2c73410a5..5e9ce0e6d4cee7 100644 --- a/crates/ruff_cli/src/cache.rs +++ b/crates/ruff_cli/src/cache.rs @@ -108,7 +108,7 @@ impl Serialize for SerializeMessage<'_> { noqa_offset: noqa_row, } = self.message; - let mut s = serializer.serialize_struct("Message", 6)?; + let mut s = serializer.serialize_struct("Message", 5)?; s.serialize_field("kind", &kind)?; s.serialize_field("range", &range)?; @@ -222,11 +222,9 @@ pub fn get( let source_files: Vec<_> = sources .into_iter() .map(|(filename, text)| { - let mut builder = SourceFileBuilder::from_string(filename); - - builder.set_source_text_string(text); - - builder.finish() + SourceFileBuilder::from_string(filename) + .source_text_string(text) + .finish() }) .collect(); diff --git a/crates/ruff_cli/src/diagnostics.rs b/crates/ruff_cli/src/diagnostics.rs index df51f120cf2a33..1aba61f8267be2 100644 --- a/crates/ruff_cli/src/diagnostics.rs +++ b/crates/ruff_cli/src/diagnostics.rs @@ -200,6 +200,7 @@ pub fn lint_path( let imports = imports.unwrap_or_default(); if let Some(err) = parse_error { + // FIXME micha manually print parse errors to get line and column numbers // Notify the user of any parse errors. error!( "{}{}{} {err}", diff --git a/crates/ruff_diagnostics/Cargo.toml b/crates/ruff_diagnostics/Cargo.toml index 83e291ac2b2e11..79963561b6926b 100644 --- a/crates/ruff_diagnostics/Cargo.toml +++ b/crates/ruff_diagnostics/Cargo.toml @@ -10,8 +10,5 @@ rust-version = { workspace = true } [dependencies] anyhow = { workspace = true } log = { workspace = true } -ruff_python_ast = { path = "../ruff_python_ast" } ruff_text_size = { workspace = true } - -rustpython-parser = { workspace = true } serde = { workspace = true, optional = true, default_features = false, features = [] } diff --git a/crates/ruff_diagnostics/src/edit.rs b/crates/ruff_diagnostics/src/edit.rs index 2a7f43342670b8..733d5c049d6c3c 100644 --- a/crates/ruff_diagnostics/src/edit.rs +++ b/crates/ruff_diagnostics/src/edit.rs @@ -46,7 +46,7 @@ impl Edit { } } - /// Creates an edit that inserts `content` at the [`Location`] `at`. + /// Creates an edit that inserts `content` at the [`TextSize`] `at`. pub fn insertion(content: String, at: TextSize) -> Self { debug_assert!(!content.is_empty(), "Insert content is empty"); diff --git a/crates/ruff_python_ast/src/helpers.rs b/crates/ruff_python_ast/src/helpers.rs index d661e9c1e83b45..dbacea34dd874c 100644 --- a/crates/ruff_python_ast/src/helpers.rs +++ b/crates/ruff_python_ast/src/helpers.rs @@ -626,7 +626,7 @@ pub fn has_comments(located: &Located, locator: &Locator) -> bool { has_comments_in(TextRange::new(start, end), locator) } -/// Returns `true` if a [`Range`] includes at least one comment. +/// Returns `true` if a [`TextRange`] includes at least one comment. pub fn has_comments_in(range: TextRange, locator: &Locator) -> bool { let source = &locator.contents()[range]; @@ -887,7 +887,7 @@ pub fn trailing_lines_end(stmt: &Stmt, locator: &Locator) -> TextSize { .map_or(line_end, |l| l.full_end()) } -/// Return the range of the first parenthesis pair after a given [`Location`]. +/// Return the range of the first parenthesis pair after a given [`TextSize`]. pub fn match_parens(start: TextSize, locator: &Locator) -> Option { let contents = &locator.contents()[usize::from(start)..]; diff --git a/crates/ruff_python_ast/src/source_code/line_index.rs b/crates/ruff_python_ast/src/source_code/line_index.rs index 9198fd673090ce..ac3ce82d881994 100644 --- a/crates/ruff_python_ast/src/source_code/line_index.rs +++ b/crates/ruff_python_ast/src/source_code/line_index.rs @@ -8,7 +8,7 @@ use std::num::NonZeroUsize; use std::ops::Deref; use std::sync::Arc; -/// Index for fast [`Location`] to [byte offset](TextSize) conversions. +/// Index for fast [byte offset](TextSize) to [`SourceLocation`] conversions. /// /// Cloning a [`LineIndex`] is cheap because it only requires bumping a reference count. #[derive(Clone)] @@ -105,7 +105,7 @@ impl LineIndex { } else { // Don't count the BOM character as a column. if line_start == TextSize::from(0) && content.starts_with('\u{feff}') { - line_start += '\u{feff}'.text_len(); + line_start = '\u{feff}'.text_len(); } content[TextRange::new(line_start, offset)].chars().count() diff --git a/crates/ruff_python_ast/src/source_code/locator.rs b/crates/ruff_python_ast/src/source_code/locator.rs index 64f090c7a92a41..d3694ef49003da 100644 --- a/crates/ruff_python_ast/src/source_code/locator.rs +++ b/crates/ruff_python_ast/src/source_code/locator.rs @@ -375,23 +375,22 @@ impl<'a> Locator<'a> { &self.contents[self.full_lines_range(range)] } - // TODO remove - /// Take the source code up to the given [`Location`]. + /// Take the source code up to the given [`TextSize`]. #[inline] pub fn up_to(&self, offset: TextSize) -> &'a str { &self.contents[TextRange::up_to(offset)] } - /// Take the source code after the given [`Location`]. + /// Take the source code after the given [`TextSize`]. #[inline] pub fn after(&self, offset: TextSize) -> &'a str { &self.contents[usize::from(offset)..] } - /// Take the source code between the given [`Range`]. + /// Take the source code between the given [`TextRange`]. #[inline] - pub fn slice>(&self, range: R) -> &'a str { - &self.contents[range.into()] + pub fn slice(&self, range: TextRange) -> &'a str { + &self.contents[range] } /// Return the underlying source code. diff --git a/crates/ruff_python_ast/src/source_code/mod.rs b/crates/ruff_python_ast/src/source_code/mod.rs index ab04d5911e8a5f..9b8969e3616fd4 100644 --- a/crates/ruff_python_ast/src/source_code/mod.rs +++ b/crates/ruff_python_ast/src/source_code/mod.rs @@ -29,7 +29,7 @@ pub fn round_trip(code: &str, source_path: &str) -> Result { Ok(generator.generate()) } -/// Gives access to the source code of a file and allows mapping between [`Location`] and byte offsets. +/// Gives access to the source code of a file and allows mapping between [`TextSize`] and [`SourceLocation`]. #[derive(Debug)] pub struct SourceCode<'src, 'index> { text: &'src str, @@ -45,28 +45,31 @@ impl<'src, 'index> SourceCode<'src, 'index> { } /// Computes the one indexed row and column numbers for `offset`. + #[inline] pub fn source_location(&self, offset: TextSize) -> SourceLocation { self.index.source_location(offset, self.text) } + #[inline] pub fn line_index(&self, offset: TextSize) -> OneIndexed { self.index.line_index(offset) } - // TODO inline - /// Take the source code up to the given [`Location`]. + /// Take the source code up to the given [`TextSize`]. + #[inline] pub fn up_to(&self, offset: TextSize) -> &'src str { &self.text[TextRange::up_to(offset)] } - /// Take the source code after the given [`Location`]. + /// Take the source code after the given [`TextSize`]. + #[inline] pub fn after(&self, offset: TextSize) -> &'src str { &self.text[usize::from(offset)..] } - /// Take the source code between the given [`Range`]. - pub fn slice>(&self, range: R) -> &'src str { - &self.text[range.into()] + /// Take the source code between the given [`TextRange`]. + pub fn slice(&self, range: TextRange) -> &'src str { + &self.text[range] } pub fn line_start(&self, line: OneIndexed) -> TextSize { diff --git a/crates/ruff_wasm/tests/api.rs b/crates/ruff_wasm/tests/api.rs index 4b971c1f042a09..02d497515492f1 100644 --- a/crates/ruff_wasm/tests/api.rs +++ b/crates/ruff_wasm/tests/api.rs @@ -5,6 +5,7 @@ use js_sys; use wasm_bindgen_test::*; use ruff::registry::Rule; +use ruff_python_ast::source_code::{OneIndexed, SourceLocation}; use ruff_wasm::*; macro_rules! check {