mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-27 04:19:18 +00:00

## Summary The motivation here is that this enables us to implement `Ranged` in crates that don't depend on `ruff_python_ast`. Largely a mechanical refactor with a lot of regex, Clippy help, and manual fixups. ## Test Plan `cargo test`
44 lines
1.4 KiB
Rust
44 lines
1.4 KiB
Rust
use ruff_python_trivia::{indentation_at_offset, is_python_whitespace, PythonWhitespace};
|
|
use ruff_source_file::{newlines::UniversalNewlineIterator, Locator};
|
|
use ruff_text_size::{Ranged, TextRange, TextSize};
|
|
|
|
use crate::Stmt;
|
|
|
|
/// Extract the leading indentation from a line.
|
|
#[inline]
|
|
pub fn indentation<'a, T>(locator: &'a Locator, located: &T) -> Option<&'a str>
|
|
where
|
|
T: Ranged,
|
|
{
|
|
indentation_at_offset(located.start(), locator)
|
|
}
|
|
|
|
/// Return the end offset at which the empty lines following a statement.
|
|
pub fn trailing_lines_end(stmt: &Stmt, locator: &Locator) -> TextSize {
|
|
let line_end = locator.full_line_end(stmt.end());
|
|
UniversalNewlineIterator::with_offset(locator.after(line_end), line_end)
|
|
.take_while(|line| line.trim_whitespace().is_empty())
|
|
.last()
|
|
.map_or(line_end, |line| line.full_end())
|
|
}
|
|
|
|
/// If a [`Ranged`] has a trailing comment, return the index of the hash.
|
|
pub fn trailing_comment_start_offset<T>(located: &T, locator: &Locator) -> Option<TextSize>
|
|
where
|
|
T: Ranged,
|
|
{
|
|
let line_end = locator.line_end(located.end());
|
|
|
|
let trailing = locator.slice(TextRange::new(located.end(), line_end));
|
|
|
|
for (index, char) in trailing.char_indices() {
|
|
if char == '#' {
|
|
return TextSize::try_from(index).ok();
|
|
}
|
|
if !is_python_whitespace(char) {
|
|
return None;
|
|
}
|
|
}
|
|
|
|
None
|
|
}
|