mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-26 11:59:10 +00:00
[pyupgrade
] Do not report when a UTF-8 comment is followed by a non-UTF-8 one (UP009
) (#14728)
## Summary Resolves #14704. ## Test Plan `cargo nextest run` and `cargo insta test`. --------- Co-authored-by: Micha Reiser <micha@reiser.io>
This commit is contained in:
parent
a55722e740
commit
c8d505c8ea
22 changed files with 273 additions and 49 deletions
|
@ -295,6 +295,44 @@ pub trait LineRanges {
|
|||
/// ## Panics
|
||||
/// If the start or end of `range` is out of bounds.
|
||||
fn full_lines_str(&self, range: TextRange) -> &str;
|
||||
|
||||
/// The number of lines `range` spans.
|
||||
///
|
||||
/// ## Examples
|
||||
///
|
||||
/// ```
|
||||
/// # use ruff_text_size::{Ranged, TextRange};
|
||||
/// # use ruff_source_file::LineRanges;
|
||||
///
|
||||
/// assert_eq!("a\nb".count_lines(TextRange::up_to(1.into())), 0);
|
||||
/// assert_eq!("a\nb\r\nc".count_lines(TextRange::up_to(3.into())), 1, "Up to the end of the second line");
|
||||
/// assert_eq!("a\nb\r\nc".count_lines(TextRange::up_to(4.into())), 2, "In between the line break characters");
|
||||
/// assert_eq!("a\nb\r\nc".count_lines(TextRange::up_to(5.into())), 2);
|
||||
/// assert_eq!("Single line".count_lines(TextRange::up_to(13.into())), 0);
|
||||
/// assert_eq!("out\nof\nbounds end".count_lines(TextRange::up_to(55.into())), 2);
|
||||
/// ```
|
||||
fn count_lines(&self, range: TextRange) -> u32 {
|
||||
let mut count = 0;
|
||||
let mut line_end = self.line_end(range.start());
|
||||
|
||||
loop {
|
||||
let next_line_start = self.full_line_end(line_end);
|
||||
|
||||
// Reached the end of the string
|
||||
if next_line_start == line_end {
|
||||
break count;
|
||||
}
|
||||
|
||||
// Range ends at the line boundary
|
||||
if line_end >= range.end() {
|
||||
break count;
|
||||
}
|
||||
|
||||
count += 1;
|
||||
|
||||
line_end = self.line_end(next_line_start);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl LineRanges for str {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue