Refactor range from Attributed to Nodes (#4422)

This commit is contained in:
Micha Reiser 2023-05-16 08:36:32 +02:00 committed by GitHub
parent 140e0acf54
commit fa26860296
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
330 changed files with 4816 additions and 3946 deletions

View file

@ -1,5 +1,6 @@
use ruff_python_ast::source_code::Locator;
use ruff_text_size::{TextLen, TextRange, TextSize};
use rustpython_parser::ast::Ranged;
/// Return `true` if the given string is a radix literal (e.g., `0b101`).
pub(crate) fn is_radix_literal(content: &str) -> bool {
@ -127,7 +128,7 @@ pub(crate) fn expand_indented_block(
/// Return true if the `orelse` block of an `if` statement is an `elif` statement.
pub(crate) fn is_elif(orelse: &[rustpython_parser::ast::Stmt], locator: &Locator) -> bool {
if orelse.len() == 1 && matches!(orelse[0].node, rustpython_parser::ast::StmtKind::If { .. }) {
if orelse.len() == 1 && matches!(orelse[0], rustpython_parser::ast::Stmt::If { .. }) {
let contents = locator.after(orelse[0].start());
if contents.starts_with("elif") {
return true;

File diff suppressed because it is too large Load diff

View file

@ -23,8 +23,8 @@ impl Format<ASTFormatContext> for FormatAlias<'_> {
fn fmt(&self, f: &mut Formatter<ASTFormatContext>) -> FormatResult<()> {
let alias = self.item;
write!(f, [dynamic_text(&alias.node.name, TextSize::default())])?;
if let Some(asname) = &alias.node.asname {
write!(f, [dynamic_text(&alias.name, TextSize::default())])?;
if let Some(asname) = &alias.asname {
write!(f, [text(" as ")])?;
write!(f, [dynamic_text(asname, TextSize::default())])?;
}

View file

@ -23,8 +23,8 @@ impl Format<ASTFormatContext> for FormatArg<'_> {
fn fmt(&self, f: &mut Formatter<ASTFormatContext>) -> FormatResult<()> {
let arg = self.item;
write!(f, [dynamic_text(&arg.node.arg, TextSize::default())])?;
if let Some(annotation) = &arg.node.annotation {
write!(f, [dynamic_text(&arg.arg, TextSize::default())])?;
if let Some(annotation) = &arg.annotation {
write!(f, [text(": ")])?;
write!(f, [annotation.format()])?;
}

View file

@ -35,7 +35,7 @@ impl Format<ASTFormatContext> for FormatArguments<'_> {
[group(&format_args![format_with(|f| {
write!(f, [arg.format()])?;
if let Some(i) = i.checked_sub(defaults_start) {
if arg.node.annotation.is_some() {
if arg.annotation.is_some() {
write!(f, [space()])?;
write!(f, [text("=")])?;
write!(f, [space()])?;
@ -91,7 +91,7 @@ impl Format<ASTFormatContext> for FormatArguments<'_> {
.checked_sub(defaults_start)
.and_then(|i| args.kw_defaults.get(i))
{
if kwarg.node.annotation.is_some() {
if kwarg.annotation.is_some() {
write!(f, [space()])?;
write!(f, [text("=")])?;
write!(f, [space()])?;

View file

@ -14,7 +14,7 @@ pub(crate) struct Block<'a> {
impl Format<ASTFormatContext> for Block<'_> {
fn fmt(&self, f: &mut Formatter<ASTFormatContext>) -> FormatResult<()> {
for (i, stmt) in self.body.node.iter().enumerate() {
for (i, stmt) in self.body.iter().enumerate() {
if i > 0 {
write!(f, [hard_line_break()])?;
}

View file

@ -24,13 +24,13 @@ impl Format<ASTFormatContext> for FormatKeyword<'_> {
let keyword = self.item;
write!(f, [leading_comments(keyword)])?;
if let Some(arg) = &keyword.node.arg {
if let Some(arg) = &keyword.arg {
write!(f, [dynamic_text(arg, TextSize::default())])?;
write!(f, [text("=")])?;
write!(f, [keyword.node.value.format()])?;
write!(f, [keyword.value.format()])?;
} else {
write!(f, [text("**")])?;
write!(f, [keyword.node.value.format()])?;
write!(f, [keyword.value.format()])?;
}
write!(f, [end_of_line_comments(keyword)])?;
write!(f, [trailing_comments(keyword)])?;

View file

@ -127,7 +127,7 @@ impl Format<ASTFormatContext> for FormatPattern<'_> {
}
}
PatternKind::MatchStar { name } => {
if let Some(name) = &name {
if let Some(name) = name {
write!(f, [text("*"), dynamic_text(name, TextSize::default())])?;
} else {
write!(f, [text("*_")])?;
@ -140,7 +140,7 @@ impl Format<ASTFormatContext> for FormatPattern<'_> {
write!(f, [text("as")])?;
write!(f, [space()])?;
}
if let Some(name) = &name {
if let Some(name) = name {
write!(f, [dynamic_text(name, TextSize::default())])?;
} else {
write!(f, [text("_")])?;