mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-27 12:29:28 +00:00
Correctly associate own-line comments in bodies (#4671)
This commit is contained in:
parent
46c3b3af94
commit
be31d71849
16 changed files with 1747 additions and 144 deletions
|
@ -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)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue