rustpython_ruff_python_ast/
whitespace.rs1use 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#[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
16pub 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
25pub 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}