Correctly associate own-line comments in bodies (#4671)

This commit is contained in:
Micha Reiser 2023-06-01 08:12:53 +02:00 committed by GitHub
parent 46c3b3af94
commit be31d71849
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
16 changed files with 1747 additions and 144 deletions

View file

@ -1,15 +1,21 @@
use ruff_text_size::TextRange;
use ruff_text_size::{TextRange, TextSize};
use rustpython_parser::ast::Ranged;
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,
{
let line_start = locator.line_start(located.start());
let indentation = &locator.contents()[TextRange::new(line_start, located.start())];
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(char::is_whitespace) {
Some(indentation)