mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-19 01:51:30 +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
|
@ -4,7 +4,7 @@ use std::path::Path;
|
|||
use rustc_hash::FxHashMap;
|
||||
|
||||
use ruff_python_trivia::{indentation_at_offset, CommentRanges, SimpleTokenKind, SimpleTokenizer};
|
||||
use ruff_source_file::Locator;
|
||||
use ruff_source_file::LineRanges;
|
||||
use ruff_text_size::{Ranged, TextLen, TextRange, TextSize};
|
||||
|
||||
use crate::name::{Name, QualifiedName, QualifiedNameBuilder};
|
||||
|
@ -1333,7 +1333,7 @@ pub fn generate_comparison(
|
|||
comparators: &[Expr],
|
||||
parent: AnyNodeRef,
|
||||
comment_ranges: &CommentRanges,
|
||||
locator: &Locator,
|
||||
source: &str,
|
||||
) -> String {
|
||||
let start = left.start();
|
||||
let end = comparators.last().map_or_else(|| left.end(), Ranged::end);
|
||||
|
@ -1341,10 +1341,8 @@ pub fn generate_comparison(
|
|||
|
||||
// Add the left side of the comparison.
|
||||
contents.push_str(
|
||||
locator.slice(
|
||||
parenthesized_range(left.into(), parent, comment_ranges, locator.contents())
|
||||
.unwrap_or(left.range()),
|
||||
),
|
||||
&source[parenthesized_range(left.into(), parent, comment_ranges, source)
|
||||
.unwrap_or(left.range())],
|
||||
);
|
||||
|
||||
for (op, comparator) in ops.iter().zip(comparators) {
|
||||
|
@ -1364,15 +1362,8 @@ pub fn generate_comparison(
|
|||
|
||||
// Add the right side of the comparison.
|
||||
contents.push_str(
|
||||
locator.slice(
|
||||
parenthesized_range(
|
||||
comparator.into(),
|
||||
parent,
|
||||
comment_ranges,
|
||||
locator.contents(),
|
||||
)
|
||||
.unwrap_or(comparator.range()),
|
||||
),
|
||||
&source[parenthesized_range(comparator.into(), parent, comment_ranges, source)
|
||||
.unwrap_or(comparator.range())],
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -1512,17 +1503,17 @@ pub fn typing_union(elts: &[Expr], binding: Name) -> Expr {
|
|||
pub fn comment_indentation_after(
|
||||
preceding: AnyNodeRef,
|
||||
comment_range: TextRange,
|
||||
locator: &Locator,
|
||||
source: &str,
|
||||
) -> TextSize {
|
||||
let tokenizer = SimpleTokenizer::new(
|
||||
locator.contents(),
|
||||
TextRange::new(locator.full_line_end(preceding.end()), comment_range.end()),
|
||||
source,
|
||||
TextRange::new(source.full_line_end(preceding.end()), comment_range.end()),
|
||||
);
|
||||
|
||||
tokenizer
|
||||
.filter_map(|token| {
|
||||
if token.kind() == SimpleTokenKind::Comment {
|
||||
indentation_at_offset(token.start(), locator).map(TextLen::text_len)
|
||||
indentation_at_offset(token.start(), source).map(TextLen::text_len)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
|
|
|
@ -1,35 +1,35 @@
|
|||
use ruff_python_trivia::{indentation_at_offset, is_python_whitespace, PythonWhitespace};
|
||||
use ruff_source_file::{Locator, UniversalNewlineIterator};
|
||||
use ruff_source_file::{LineRanges, UniversalNewlineIterator};
|
||||
use ruff_text_size::{Ranged, TextRange, TextSize};
|
||||
|
||||
use crate::Stmt;
|
||||
|
||||
/// Extract the leading indentation from a line.
|
||||
#[inline]
|
||||
pub fn indentation<'a, T>(locator: &'a Locator, located: &T) -> Option<&'a str>
|
||||
pub fn indentation<'a, T>(source: &'a str, located: &T) -> Option<&'a str>
|
||||
where
|
||||
T: Ranged,
|
||||
{
|
||||
indentation_at_offset(located.start(), locator)
|
||||
indentation_at_offset(located.start(), source)
|
||||
}
|
||||
|
||||
/// Return the end offset at which the empty lines following a statement.
|
||||
pub fn trailing_lines_end(stmt: &Stmt, locator: &Locator) -> TextSize {
|
||||
let line_end = locator.full_line_end(stmt.end());
|
||||
UniversalNewlineIterator::with_offset(locator.after(line_end), line_end)
|
||||
pub fn trailing_lines_end(stmt: &Stmt, source: &str) -> TextSize {
|
||||
let line_end = source.full_line_end(stmt.end());
|
||||
UniversalNewlineIterator::with_offset(&source[line_end.to_usize()..], line_end)
|
||||
.take_while(|line| line.trim_whitespace().is_empty())
|
||||
.last()
|
||||
.map_or(line_end, |line| line.full_end())
|
||||
}
|
||||
|
||||
/// If a [`Ranged`] has a trailing comment, return the index of the hash.
|
||||
pub fn trailing_comment_start_offset<T>(located: &T, locator: &Locator) -> Option<TextSize>
|
||||
pub fn trailing_comment_start_offset<T>(located: &T, source: &str) -> Option<TextSize>
|
||||
where
|
||||
T: Ranged,
|
||||
{
|
||||
let line_end = locator.line_end(located.end());
|
||||
let line_end = source.line_end(located.end());
|
||||
|
||||
let trailing = locator.slice(TextRange::new(located.end(), line_end));
|
||||
let trailing = &source[TextRange::new(located.end(), line_end)];
|
||||
|
||||
for (index, char) in trailing.char_indices() {
|
||||
if char == '#' {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue