mirror of
https://github.com/astral-sh/ruff.git
synced 2025-07-21 20:15:11 +00:00
Implement From<Located>
for Range
(#3377)
This commit is contained in:
parent
ff2c0dd491
commit
130e733023
232 changed files with 612 additions and 719 deletions
|
@ -43,6 +43,18 @@ impl<T> Located<T> {
|
|||
}
|
||||
}
|
||||
|
||||
impl<T> From<&Located<T>> for Range {
|
||||
fn from(located: &Located<T>) -> Self {
|
||||
Range::new(located.location, located.end_location.unwrap())
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> From<&Box<Located<T>>> for Range {
|
||||
fn from(located: &Box<Located<T>>) -> Self {
|
||||
Range::new(located.location, located.end_location.unwrap())
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub enum ExprContext {
|
||||
Load,
|
||||
|
|
|
@ -39,7 +39,7 @@ fn format_name(
|
|||
expr: &Expr,
|
||||
_id: &str,
|
||||
) -> FormatResult<()> {
|
||||
write!(f, [literal(Range::from_located(expr))])?;
|
||||
write!(f, [literal(Range::from(expr))])?;
|
||||
write!(f, [end_of_line_comments(expr)])?;
|
||||
Ok(())
|
||||
}
|
||||
|
@ -577,7 +577,7 @@ fn format_joined_str(
|
|||
expr: &Expr,
|
||||
_values: &[Expr],
|
||||
) -> FormatResult<()> {
|
||||
write!(f, [literal(Range::from_located(expr))])?;
|
||||
write!(f, [literal(Range::from(expr))])?;
|
||||
write!(f, [end_of_line_comments(expr)])?;
|
||||
Ok(())
|
||||
}
|
||||
|
@ -598,11 +598,11 @@ fn format_constant(
|
|||
write!(f, [text("False")])?;
|
||||
}
|
||||
}
|
||||
Constant::Int(_) => write!(f, [int_literal(Range::from_located(expr))])?,
|
||||
Constant::Float(_) => write!(f, [float_literal(Range::from_located(expr))])?,
|
||||
Constant::Int(_) => write!(f, [int_literal(Range::from(expr))])?,
|
||||
Constant::Float(_) => write!(f, [float_literal(Range::from(expr))])?,
|
||||
Constant::Str(_) => write!(f, [string_literal(expr)])?,
|
||||
Constant::Bytes(_) => write!(f, [string_literal(expr)])?,
|
||||
Constant::Complex { .. } => write!(f, [complex_literal(Range::from_located(expr))])?,
|
||||
Constant::Complex { .. } => write!(f, [complex_literal(Range::from(expr))])?,
|
||||
Constant::Tuple(_) => unreachable!("Constant::Tuple should be handled by format_tuple"),
|
||||
}
|
||||
write!(f, [end_of_line_comments(expr)])?;
|
||||
|
|
|
@ -126,7 +126,7 @@ impl Format<ASTFormatContext<'_>> for StringLiteral<'_> {
|
|||
|
||||
// TODO(charlie): This tokenization needs to happen earlier, so that we can attach
|
||||
// comments to individual string literals.
|
||||
let (source, start, end) = f.context().locator().slice(Range::from_located(expr));
|
||||
let (source, start, end) = f.context().locator().slice(Range::from(expr));
|
||||
let elts =
|
||||
rustpython_parser::lexer::lex_located(&source[start..end], Mode::Module, expr.location)
|
||||
.flatten()
|
||||
|
|
|
@ -154,7 +154,7 @@ impl<'a> Visitor<'a> for ParenthesesNormalizer<'_> {
|
|||
..
|
||||
},
|
||||
) {
|
||||
let (source, start, end) = self.locator.slice(Range::from_located(value));
|
||||
let (source, start, end) = self.locator.slice(Range::from(&*value));
|
||||
// TODO(charlie): Encode this in the AST via separate node types.
|
||||
if !is_radix_literal(&source[start..end]) {
|
||||
value.parentheses = Parenthesize::Always;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue