Implement From<Located> for Range (#3377)

This commit is contained in:
Charlie Marsh 2023-03-08 13:50:20 -05:00 committed by GitHub
parent ff2c0dd491
commit 130e733023
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
232 changed files with 612 additions and 719 deletions

View file

@ -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,

View file

@ -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)])?;

View file

@ -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()

View file

@ -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;