mirror of
https://github.com/astral-sh/ruff
synced 2026-01-22 14:00:51 -05:00
## Summary This crate now contains utilities for dealing with trivia more broadly: whitespace, newlines, "simple" trivia lexing, etc. So renaming it to reflect its increased responsibilities. To avoid conflicts, I've also renamed `Token` and `TokenKind` to `SimpleToken` and `SimpleTokenKind`.
28 lines
765 B
Rust
28 lines
765 B
Rust
use ruff_text_size::{TextRange, TextSize};
|
|
use rustpython_parser::ast::Ranged;
|
|
|
|
use ruff_python_trivia::is_python_whitespace;
|
|
|
|
use crate::source_code::Locator;
|
|
|
|
/// 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(locator, located.start())
|
|
}
|
|
|
|
/// Extract the leading indentation from a line.
|
|
pub fn indentation_at_offset<'a>(locator: &'a Locator, offset: TextSize) -> Option<&'a str> {
|
|
let line_start = locator.line_start(offset);
|
|
let indentation = &locator.contents()[TextRange::new(line_start, offset)];
|
|
|
|
if indentation.chars().all(is_python_whitespace) {
|
|
Some(indentation)
|
|
} else {
|
|
None
|
|
}
|
|
}
|