From 43e972a299517f0913134d4c1497f5b35c253414 Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Fri, 2 Feb 2024 13:02:40 -0500 Subject: [PATCH] Rename methods --- crates/ruff_linter/src/fix/edits.rs | 9 +++++++++ .../src/rules/pycodestyle/rules/trailing_whitespace.rs | 2 +- crates/ruff_python_index/src/fstring_ranges.rs | 4 ++-- crates/ruff_python_index/src/multiline_ranges.rs | 6 +++--- crates/ruff_python_trivia/src/comment_ranges.rs | 4 ++-- 5 files changed, 17 insertions(+), 8 deletions(-) diff --git a/crates/ruff_linter/src/fix/edits.rs b/crates/ruff_linter/src/fix/edits.rs index 0212579bf9e5a..69d698fae6219 100644 --- a/crates/ruff_linter/src/fix/edits.rs +++ b/crates/ruff_linter/src/fix/edits.rs @@ -170,6 +170,15 @@ pub(crate) fn add_argument( } /// Safely adjust the indentation of the indented block at [`TextRange`]. +/// +/// The [`TextRange`] is assumed to represent an entire indented block, including the leading +/// indentation of that block. For example, to dedent the body here: +/// ```python +/// if True: +/// print("Hello, world!") +/// ``` +/// +/// The range would be the entirety of ` print("Hello, world!")`. pub(crate) fn adjust_indentation( range: TextRange, indentation: &str, diff --git a/crates/ruff_linter/src/rules/pycodestyle/rules/trailing_whitespace.rs b/crates/ruff_linter/src/rules/pycodestyle/rules/trailing_whitespace.rs index 6e9a83e1bd9fd..42fb45ec64da4 100644 --- a/crates/ruff_linter/src/rules/pycodestyle/rules/trailing_whitespace.rs +++ b/crates/ruff_linter/src/rules/pycodestyle/rules/trailing_whitespace.rs @@ -105,7 +105,7 @@ pub(crate) fn trailing_whitespace( diagnostic.set_fix(Fix::applicable_edit( Edit::range_deletion(range), // Removing trailing whitespace is not safe inside multiline strings. - if indexer.multiline_ranges().contains(range) { + if indexer.multiline_ranges().contains_range(range) { Applicability::Unsafe } else { Applicability::Safe diff --git a/crates/ruff_python_index/src/fstring_ranges.rs b/crates/ruff_python_index/src/fstring_ranges.rs index 714c971fcd526..c34b68a0c11bf 100644 --- a/crates/ruff_python_index/src/fstring_ranges.rs +++ b/crates/ruff_python_index/src/fstring_ranges.rs @@ -14,12 +14,12 @@ pub struct FStringRanges { } impl FStringRanges { - /// Returns `true` if the given range contains an f-string. + /// Returns `true` if the given range intersects with any f-string range. pub fn intersects(&self, target: TextRange) -> bool { self.raw .values() .take_while(|range| range.start() < target.end()) - .any(|range| target.contains_range(*range)) + .any(|range| target.intersect(*range).is_some()) } /// Return the [`TextRange`] of the innermost f-string at the given offset. diff --git a/crates/ruff_python_index/src/multiline_ranges.rs b/crates/ruff_python_index/src/multiline_ranges.rs index f0516c9d6ab42..3461211e3d2d4 100644 --- a/crates/ruff_python_index/src/multiline_ranges.rs +++ b/crates/ruff_python_index/src/multiline_ranges.rs @@ -9,7 +9,7 @@ pub struct MultilineRanges { impl MultilineRanges { /// Returns `true` if the given range is inside a multiline string. - pub fn contains(&self, target: TextRange) -> bool { + pub fn contains_range(&self, target: TextRange) -> bool { self.ranges .binary_search_by(|range| { if range.contains_range(target) { @@ -23,11 +23,11 @@ impl MultilineRanges { .is_ok() } - /// Returns `true` if the given range contains a multiline string. + /// Returns `true` if the given range intersects with any multiline string. pub fn intersects(&self, target: TextRange) -> bool { self.ranges .binary_search_by(|range| { - if target.contains_range(*range) { + if target.intersect(*range).is_some() { std::cmp::Ordering::Equal } else if range.end() < target.start() { std::cmp::Ordering::Less diff --git a/crates/ruff_python_trivia/src/comment_ranges.rs b/crates/ruff_python_trivia/src/comment_ranges.rs index 1cd8c5487b453..bdec7c428e1d4 100644 --- a/crates/ruff_python_trivia/src/comment_ranges.rs +++ b/crates/ruff_python_trivia/src/comment_ranges.rs @@ -19,11 +19,11 @@ impl CommentRanges { Self { raw: ranges } } - /// Returns `true` if the given range includes a comment. + /// Returns `true` if the given range intersects with any comment range. pub fn intersects(&self, target: TextRange) -> bool { self.raw .binary_search_by(|range| { - if target.contains_range(*range) { + if target.intersect(*range).is_some() { std::cmp::Ordering::Equal } else if range.end() < target.start() { std::cmp::Ordering::Less