Skip to main content

rustpython_ruff_python_ast/
whitespace.rs

1use ruff_python_trivia::{PythonWhitespace, indentation_at_offset, is_python_whitespace};
2use ruff_source_file::{LineRanges, UniversalNewlineIterator};
3use ruff_text_size::{Ranged, TextRange, TextSize};
4
5use crate::Stmt;
6
7/// Extract the leading indentation from a line.
8#[inline]
9pub fn indentation<'a, T>(source: &'a str, located: &T) -> Option<&'a str>
10where
11    T: Ranged,
12{
13    indentation_at_offset(located.start(), source)
14}
15
16/// Return the end offset at which the empty lines following a statement.
17pub fn trailing_lines_end(stmt: &Stmt, source: &str) -> TextSize {
18    let line_end = source.full_line_end(stmt.end());
19    UniversalNewlineIterator::with_offset(&source[line_end.to_usize()..], line_end)
20        .take_while(|line| line.trim_whitespace().is_empty())
21        .last()
22        .map_or(line_end, |line| line.full_end())
23}
24
25/// If a [`Ranged`] has a trailing comment, return the index of the hash.
26pub fn trailing_comment_start_offset<T>(located: &T, source: &str) -> Option<TextSize>
27where
28    T: Ranged,
29{
30    let line_end = source.line_end(located.end());
31
32    let trailing = &source[TextRange::new(located.end(), line_end)];
33
34    for (index, char) in trailing.char_indices() {
35        if char == '#' {
36            return TextSize::try_from(index).ok();
37        }
38        if !is_python_whitespace(char) {
39            return None;
40        }
41    }
42
43    None
44}