mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-28 04:45:01 +00:00
Extract LineIndex
independent methods from Locator
(#13938)
This commit is contained in:
parent
f8eb547fb4
commit
9f3a38d408
171 changed files with 1348 additions and 1284 deletions
|
@ -1,29 +1,28 @@
|
|||
use ruff_source_file::Locator;
|
||||
use ruff_source_file::LineRanges;
|
||||
use ruff_text_size::{TextRange, TextSize};
|
||||
|
||||
/// Extract the leading indentation from a line.
|
||||
pub fn indentation_at_offset<'a>(offset: TextSize, locator: &'a Locator) -> Option<&'a str> {
|
||||
let line_start = locator.line_start(offset);
|
||||
let indentation = locator.slice(TextRange::new(line_start, offset));
|
||||
pub fn indentation_at_offset(offset: TextSize, source: &str) -> Option<&str> {
|
||||
let line_start = source.line_start(offset);
|
||||
let indentation = &source[TextRange::new(line_start, offset)];
|
||||
|
||||
if indentation.chars().all(is_python_whitespace) {
|
||||
Some(indentation)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
indentation
|
||||
.chars()
|
||||
.all(is_python_whitespace)
|
||||
.then_some(indentation)
|
||||
}
|
||||
|
||||
/// Return `true` if the node starting the given [`TextSize`] has leading content.
|
||||
pub fn has_leading_content(offset: TextSize, locator: &Locator) -> bool {
|
||||
let line_start = locator.line_start(offset);
|
||||
let leading = locator.slice(TextRange::new(line_start, offset));
|
||||
pub fn has_leading_content(offset: TextSize, source: &str) -> bool {
|
||||
let line_start = source.line_start(offset);
|
||||
let leading = &source[TextRange::new(line_start, offset)];
|
||||
leading.chars().any(|char| !is_python_whitespace(char))
|
||||
}
|
||||
|
||||
/// Return `true` if the node ending at the given [`TextSize`] has trailing content.
|
||||
pub fn has_trailing_content(offset: TextSize, locator: &Locator) -> bool {
|
||||
let line_end = locator.line_end(offset);
|
||||
let trailing = locator.slice(TextRange::new(offset, line_end));
|
||||
pub fn has_trailing_content(offset: TextSize, source: &str) -> bool {
|
||||
let line_end = source.line_end(offset);
|
||||
let trailing = &source[TextRange::new(offset, line_end)];
|
||||
|
||||
for char in trailing.chars() {
|
||||
if char == '#' {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue