Remove source position from FormatElement::DynamicText (#4619)

This commit is contained in:
Micha Reiser 2023-05-24 16:36:14 +02:00 committed by GitHub
parent 85f094f592
commit 6943beee66
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
20 changed files with 165 additions and 145 deletions

View file

@ -1,6 +1,5 @@
use ruff_formatter::prelude::*;
use ruff_formatter::write;
use ruff_text_size::TextSize;
use crate::context::ASTFormatContext;
use crate::cst::Alias;
@ -23,10 +22,10 @@ impl Format<ASTFormatContext> for FormatAlias<'_> {
fn fmt(&self, f: &mut Formatter<ASTFormatContext>) -> FormatResult<()> {
let alias = self.item;
write!(f, [dynamic_text(&alias.name, TextSize::default())])?;
write!(f, [dynamic_text(&alias.name, None)])?;
if let Some(asname) = &alias.asname {
write!(f, [text(" as ")])?;
write!(f, [dynamic_text(asname, TextSize::default())])?;
write!(f, [dynamic_text(asname, None)])?;
}
write!(f, [end_of_line_comments(alias)])?;

View file

@ -1,6 +1,5 @@
use ruff_formatter::prelude::*;
use ruff_formatter::write;
use ruff_text_size::TextSize;
use crate::context::ASTFormatContext;
use crate::cst::Arg;
@ -23,7 +22,7 @@ impl Format<ASTFormatContext> for FormatArg<'_> {
fn fmt(&self, f: &mut Formatter<ASTFormatContext>) -> FormatResult<()> {
let arg = self.item;
write!(f, [dynamic_text(&arg.arg, TextSize::default())])?;
write!(f, [dynamic_text(&arg.arg, None)])?;
if let Some(annotation) = &arg.annotation {
write!(f, [text(": ")])?;
write!(f, [annotation.format()])?;

View file

@ -1,6 +1,6 @@
use ruff_formatter::prelude::*;
use ruff_formatter::{write, Format};
use ruff_text_size::{TextRange, TextSize};
use ruff_text_size::TextRange;
use crate::context::ASTFormatContext;
use crate::cst::{Body, Stmt};
@ -98,7 +98,7 @@ impl<Context> Format<Context> for JoinNames<'_> {
fn fmt(&self, f: &mut Formatter<Context>) -> FormatResult<()> {
let mut join = f.join_with(text(", "));
for name in self.names {
join.entry(&dynamic_text(name, TextSize::default()));
join.entry(&dynamic_text(name, None));
}
join.finish()
}

View file

@ -1,6 +1,5 @@
use ruff_formatter::prelude::*;
use ruff_formatter::write;
use ruff_text_size::TextSize;
use crate::context::ASTFormatContext;
use crate::cst::{Excepthandler, ExcepthandlerKind};
@ -29,15 +28,7 @@ impl Format<ASTFormatContext> for FormatExcepthandler<'_> {
if let Some(type_) = &type_ {
write!(f, [space(), type_.format()])?;
if let Some(name) = &name {
write!(
f,
[
space(),
text("as"),
space(),
dynamic_text(name, TextSize::default()),
]
)?;
write!(f, [space(), text("as"), space(), dynamic_text(name, None)])?;
}
}
write!(f, [text(":")])?;

View file

@ -4,7 +4,6 @@ use rustpython_parser::ast::{Constant, ConversionFlag};
use ruff_formatter::prelude::*;
use ruff_formatter::{format_args, write};
use ruff_text_size::TextSize;
use crate::context::ASTFormatContext;
use crate::cst::{
@ -676,7 +675,7 @@ fn format_attribute(
) -> FormatResult<()> {
write!(f, [value.format()])?;
write!(f, [text(".")])?;
write!(f, [dynamic_text(attr, TextSize::default())])?;
write!(f, [dynamic_text(attr, None)])?;
write!(f, [end_of_line_comments(expr)])?;
Ok(())
}

View file

@ -1,6 +1,5 @@
use ruff_formatter::prelude::*;
use ruff_formatter::write;
use ruff_text_size::TextSize;
use crate::context::ASTFormatContext;
use crate::cst::Keyword;
@ -25,7 +24,7 @@ impl Format<ASTFormatContext> for FormatKeyword<'_> {
write!(f, [leading_comments(keyword)])?;
if let Some(arg) = &keyword.arg {
write!(f, [dynamic_text(arg, TextSize::default())])?;
write!(f, [dynamic_text(arg, None)])?;
write!(f, [text("=")])?;
write!(f, [keyword.value.format()])?;
} else {

View file

@ -136,8 +136,8 @@ impl Format<ASTFormatContext> for IntLiteral {
write!(
f,
[
dynamic_text(&prefix.to_lowercase(), TextSize::default()),
dynamic_text(&suffix.to_uppercase(), TextSize::default())
dynamic_text(&prefix.to_lowercase(), None),
dynamic_text(&suffix.to_uppercase(), None)
]
)?;
} else {

View file

@ -2,7 +2,6 @@ use rustpython_parser::ast::Constant;
use ruff_formatter::prelude::*;
use ruff_formatter::write;
use ruff_text_size::TextSize;
use crate::context::ASTFormatContext;
use crate::cst::{Pattern, PatternKind};
@ -79,7 +78,7 @@ impl Format<ASTFormatContext> for FormatPattern<'_> {
space(),
text("**"),
space(),
dynamic_text(rest, TextSize::default())
dynamic_text(rest, None)
]
)?;
}
@ -105,13 +104,10 @@ impl Format<ASTFormatContext> for FormatPattern<'_> {
if !kwd_attrs.is_empty() {
write!(f, [text("(")])?;
if let Some(attr) = kwd_attrs.first() {
write!(f, [dynamic_text(attr, TextSize::default())])?;
write!(f, [dynamic_text(attr, None)])?;
}
for attr in kwd_attrs.iter().skip(1) {
write!(
f,
[text(","), space(), dynamic_text(attr, TextSize::default())]
)?;
write!(f, [text(","), space(), dynamic_text(attr, None)])?;
}
write!(f, [text(")")])?;
}
@ -128,7 +124,7 @@ impl Format<ASTFormatContext> for FormatPattern<'_> {
}
PatternKind::MatchStar { name } => {
if let Some(name) = name {
write!(f, [text("*"), dynamic_text(name, TextSize::default())])?;
write!(f, [text("*"), dynamic_text(name, None)])?;
} else {
write!(f, [text("*_")])?;
}
@ -141,7 +137,7 @@ impl Format<ASTFormatContext> for FormatPattern<'_> {
write!(f, [space()])?;
}
if let Some(name) = name {
write!(f, [dynamic_text(name, TextSize::default())])?;
write!(f, [dynamic_text(name, None)])?;
} else {
write!(f, [text("_")])?;
}

View file

@ -2,7 +2,6 @@
use ruff_formatter::prelude::*;
use ruff_formatter::{format_args, write};
use ruff_text_size::TextSize;
use crate::context::ASTFormatContext;
use crate::cst::{
@ -110,14 +109,7 @@ fn format_class_def(
write!(f, [leading_comments(body)])?;
write!(
f,
[
text("class"),
space(),
dynamic_text(name, TextSize::default())
]
)?;
write!(f, [text("class"), space(), dynamic_text(name, None)])?;
if !bases.is_empty() || !keywords.is_empty() {
let format_bases = format_with(|f| {
@ -180,7 +172,7 @@ fn format_func_def(
[
text("def"),
space(),
dynamic_text(name, TextSize::default()),
dynamic_text(name, None),
text("("),
group(&soft_block_indent(&format_with(|f| {
if stmt.trivia.iter().any(|c| c.kind.is_magic_trailing_comma()) {
@ -653,7 +645,7 @@ fn format_import_from(
}
}
if let Some(module) = module {
write!(f, [dynamic_text(module, TextSize::default())])?;
write!(f, [dynamic_text(module, None)])?;
}
write!(f, [space()])?;

View file

@ -3,7 +3,7 @@ use rustpython_parser::{Mode, Tok};
use ruff_formatter::prelude::*;
use ruff_formatter::{write, Format};
use ruff_python_ast::str::{leading_quote, trailing_quote};
use ruff_text_size::{TextRange, TextSize};
use ruff_text_size::TextRange;
use crate::context::ASTFormatContext;
use crate::cst::Expr;
@ -64,7 +64,6 @@ impl Format<ASTFormatContext> for StringLiteralPart {
} else {
double_escape(body).into()
},
source_position: TextSize::default(),
})?;
f.write_element(FormatElement::StaticText { text: "\"" })?;
Ok(())
@ -76,7 +75,6 @@ impl Format<ASTFormatContext> for StringLiteralPart {
} else {
single_escape(body).into()
},
source_position: TextSize::default(),
})?;
f.write_element(FormatElement::StaticText { text: "'" })?;
Ok(())
@ -90,7 +88,6 @@ impl Format<ASTFormatContext> for StringLiteralPart {
f.write_element(FormatElement::StaticText { text: "'''" })?;
f.write_element(FormatElement::DynamicText {
text: body.to_string().into_boxed_str(),
source_position: TextSize::default(),
})?;
f.write_element(FormatElement::StaticText { text: "'''" })?;
Ok(())
@ -98,7 +95,6 @@ impl Format<ASTFormatContext> for StringLiteralPart {
f.write_element(FormatElement::StaticText { text: "\"\"\"" })?;
f.write_element(FormatElement::DynamicText {
text: body.to_string().into_boxed_str(),
source_position: TextSize::default(),
})?;
f.write_element(FormatElement::StaticText { text: "\"\"\"" })?;
Ok(())

View file

@ -67,7 +67,6 @@ mod tests {
use insta::assert_snapshot;
use ruff_testing_macros::fixture;
use ruff_text_size::TextSize;
use similar::TextDiff;
use crate::fmt;
@ -208,7 +207,7 @@ mod tests {
while let Some(word) = words.next() {
let is_last = words.peek().is_none();
let format_word = format_with(|f| {
write!(f, [dynamic_text(word, TextSize::default())])?;
write!(f, [dynamic_text(word, None)])?;
if is_last {
write!(f, [text("\"")])?;