mirror of
https://github.com/astral-sh/ruff.git
synced 2025-07-24 13:33:50 +00:00
More enum work (#3212)
This commit is contained in:
parent
248590224a
commit
84e96cdcd9
12 changed files with 93 additions and 224 deletions
1
Cargo.lock
generated
1
Cargo.lock
generated
|
@ -2140,6 +2140,7 @@ dependencies = [
|
||||||
"anyhow",
|
"anyhow",
|
||||||
"clap 4.1.6",
|
"clap 4.1.6",
|
||||||
"insta",
|
"insta",
|
||||||
|
"is-macro",
|
||||||
"once_cell",
|
"once_cell",
|
||||||
"ruff_formatter",
|
"ruff_formatter",
|
||||||
"ruff_python",
|
"ruff_python",
|
||||||
|
|
|
@ -31,7 +31,7 @@ use crate::fix::Fix;
|
||||||
use crate::registry::Diagnostic;
|
use crate::registry::Diagnostic;
|
||||||
use crate::violation::{AutofixKind, Availability, Violation};
|
use crate::violation::{AutofixKind, Availability, Violation};
|
||||||
|
|
||||||
#[derive(Debug, Copy, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
#[derive(Debug, Copy, Clone, PartialEq, Eq, Serialize, Deserialize, result_like::BoolLike)]
|
||||||
pub enum Certainty {
|
pub enum Certainty {
|
||||||
Certain,
|
Certain,
|
||||||
Uncertain,
|
Uncertain,
|
||||||
|
@ -59,7 +59,7 @@ impl Violation for UnusedLoopControlVariable {
|
||||||
let UnusedLoopControlVariable {
|
let UnusedLoopControlVariable {
|
||||||
name, certainty, ..
|
name, certainty, ..
|
||||||
} = self;
|
} = self;
|
||||||
if matches!(certainty, Certainty::Certain) {
|
if certainty.to_bool() {
|
||||||
format!("Loop control variable `{name}` not used within loop body")
|
format!("Loop control variable `{name}` not used within loop body")
|
||||||
} else {
|
} else {
|
||||||
format!("Loop control variable `{name}` may not be used within loop body")
|
format!("Loop control variable `{name}` may not be used within loop body")
|
||||||
|
@ -70,7 +70,7 @@ impl Violation for UnusedLoopControlVariable {
|
||||||
let UnusedLoopControlVariable {
|
let UnusedLoopControlVariable {
|
||||||
certainty, rename, ..
|
certainty, rename, ..
|
||||||
} = self;
|
} = self;
|
||||||
if matches!(certainty, Certainty::Certain) && rename.is_some() {
|
if certainty.to_bool() && rename.is_some() {
|
||||||
Some(|UnusedLoopControlVariable { name, rename, .. }| {
|
Some(|UnusedLoopControlVariable { name, rename, .. }| {
|
||||||
let rename = rename.as_ref().unwrap();
|
let rename = rename.as_ref().unwrap();
|
||||||
format!("Rename unused `{name}` to `{rename}`")
|
format!("Rename unused `{name}` to `{rename}`")
|
||||||
|
@ -140,25 +140,17 @@ pub fn unused_loop_control_variable(
|
||||||
}
|
}
|
||||||
|
|
||||||
// Avoid fixing any variables that _may_ be used, but undetectably so.
|
// Avoid fixing any variables that _may_ be used, but undetectably so.
|
||||||
let certainty = if helpers::uses_magic_variable_access(checker, body) {
|
let certainty = Certainty::from(!helpers::uses_magic_variable_access(checker, body));
|
||||||
Certainty::Uncertain
|
|
||||||
} else {
|
|
||||||
Certainty::Certain
|
|
||||||
};
|
|
||||||
|
|
||||||
// Attempt to rename the variable by prepending an underscore, but avoid
|
// Attempt to rename the variable by prepending an underscore, but avoid
|
||||||
// applying the fix if doing so wouldn't actually cause us to ignore the
|
// applying the fix if doing so wouldn't actually cause us to ignore the
|
||||||
// violation in the next pass.
|
// violation in the next pass.
|
||||||
let rename = format!("_{name}");
|
let rename = format!("_{name}");
|
||||||
let rename = if checker
|
let rename = checker
|
||||||
.settings
|
.settings
|
||||||
.dummy_variable_rgx
|
.dummy_variable_rgx
|
||||||
.is_match(rename.as_str())
|
.is_match(rename.as_str())
|
||||||
{
|
.then_some(rename);
|
||||||
Some(rename)
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
};
|
|
||||||
|
|
||||||
let mut diagnostic = Diagnostic::new(
|
let mut diagnostic = Diagnostic::new(
|
||||||
UnusedLoopControlVariable {
|
UnusedLoopControlVariable {
|
||||||
|
@ -169,7 +161,7 @@ pub fn unused_loop_control_variable(
|
||||||
Range::from_located(expr),
|
Range::from_located(expr),
|
||||||
);
|
);
|
||||||
if let Some(rename) = rename {
|
if let Some(rename) = rename {
|
||||||
if matches!(certainty, Certainty::Certain) && checker.patch(diagnostic.kind.rule()) {
|
if certainty.into() && checker.patch(diagnostic.kind.rule()) {
|
||||||
// Find the `BindingKind::LoopVar` corresponding to the name.
|
// Find the `BindingKind::LoopVar` corresponding to the name.
|
||||||
let scope = checker.current_scope();
|
let scope = checker.current_scope();
|
||||||
let binding = scope
|
let binding = scope
|
||||||
|
|
|
@ -13,6 +13,7 @@ ruff_text_size = { path = "../ruff_text_size" }
|
||||||
|
|
||||||
anyhow = { workspace = true }
|
anyhow = { workspace = true }
|
||||||
clap = { workspace = true }
|
clap = { workspace = true }
|
||||||
|
is-macro = { workspace = true }
|
||||||
once_cell = { workspace = true }
|
once_cell = { workspace = true }
|
||||||
rustc-hash = { workspace = true }
|
rustc-hash = { workspace = true }
|
||||||
rustpython-common = { workspace = true }
|
rustpython-common = { workspace = true }
|
||||||
|
|
|
@ -4,7 +4,7 @@ use ruff_text_size::TextRange;
|
||||||
|
|
||||||
use crate::context::ASTFormatContext;
|
use crate::context::ASTFormatContext;
|
||||||
use crate::core::types::Range;
|
use crate::core::types::Range;
|
||||||
use crate::trivia::{Relationship, Trivia, TriviaKind};
|
use crate::trivia::{Trivia, TriviaKind};
|
||||||
|
|
||||||
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
|
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
|
||||||
pub struct Literal {
|
pub struct Literal {
|
||||||
|
@ -36,7 +36,7 @@ pub struct LeadingComments<'a> {
|
||||||
impl Format<ASTFormatContext<'_>> for LeadingComments<'_> {
|
impl Format<ASTFormatContext<'_>> for LeadingComments<'_> {
|
||||||
fn fmt(&self, f: &mut Formatter<ASTFormatContext<'_>>) -> FormatResult<()> {
|
fn fmt(&self, f: &mut Formatter<ASTFormatContext<'_>>) -> FormatResult<()> {
|
||||||
for comment in self.comments {
|
for comment in self.comments {
|
||||||
if matches!(comment.relationship, Relationship::Leading) {
|
if comment.relationship.is_leading() {
|
||||||
if let TriviaKind::OwnLineComment(range) = comment.kind {
|
if let TriviaKind::OwnLineComment(range) = comment.kind {
|
||||||
write!(f, [hard_line_break()])?;
|
write!(f, [hard_line_break()])?;
|
||||||
write!(f, [literal(range)])?;
|
write!(f, [literal(range)])?;
|
||||||
|
@ -60,7 +60,7 @@ pub struct TrailingComments<'a> {
|
||||||
impl Format<ASTFormatContext<'_>> for TrailingComments<'_> {
|
impl Format<ASTFormatContext<'_>> for TrailingComments<'_> {
|
||||||
fn fmt(&self, f: &mut Formatter<ASTFormatContext<'_>>) -> FormatResult<()> {
|
fn fmt(&self, f: &mut Formatter<ASTFormatContext<'_>>) -> FormatResult<()> {
|
||||||
for comment in self.comments {
|
for comment in self.comments {
|
||||||
if matches!(comment.relationship, Relationship::Trailing) {
|
if comment.relationship.is_trailing() {
|
||||||
if let TriviaKind::OwnLineComment(range) = comment.kind {
|
if let TriviaKind::OwnLineComment(range) = comment.kind {
|
||||||
write!(f, [hard_line_break()])?;
|
write!(f, [hard_line_break()])?;
|
||||||
write!(f, [literal(range)])?;
|
write!(f, [literal(range)])?;
|
||||||
|
|
|
@ -6,7 +6,6 @@ use crate::builders::literal;
|
||||||
use crate::context::ASTFormatContext;
|
use crate::context::ASTFormatContext;
|
||||||
use crate::cst::Alias;
|
use crate::cst::Alias;
|
||||||
use crate::shared_traits::AsFormat;
|
use crate::shared_traits::AsFormat;
|
||||||
use crate::trivia::{Relationship, TriviaKind};
|
|
||||||
|
|
||||||
pub struct FormatAlias<'a> {
|
pub struct FormatAlias<'a> {
|
||||||
item: &'a Alias,
|
item: &'a Alias,
|
||||||
|
@ -33,12 +32,8 @@ impl Format<ASTFormatContext<'_>> for FormatAlias<'_> {
|
||||||
// Format any end-of-line comments.
|
// Format any end-of-line comments.
|
||||||
let mut first = true;
|
let mut first = true;
|
||||||
for range in alias.trivia.iter().filter_map(|trivia| {
|
for range in alias.trivia.iter().filter_map(|trivia| {
|
||||||
if matches!(trivia.relationship, Relationship::Trailing) {
|
if trivia.relationship.is_trailing() {
|
||||||
if let TriviaKind::EndOfLineComment(range) = trivia.kind {
|
trivia.kind.end_of_line_comment()
|
||||||
Some(range)
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,7 +7,6 @@ use crate::context::ASTFormatContext;
|
||||||
use crate::cst::{Excepthandler, ExcepthandlerKind};
|
use crate::cst::{Excepthandler, ExcepthandlerKind};
|
||||||
use crate::format::builders::block;
|
use crate::format::builders::block;
|
||||||
use crate::shared_traits::AsFormat;
|
use crate::shared_traits::AsFormat;
|
||||||
use crate::trivia::{Relationship, TriviaKind};
|
|
||||||
|
|
||||||
pub struct FormatExcepthandler<'a> {
|
pub struct FormatExcepthandler<'a> {
|
||||||
item: &'a Excepthandler,
|
item: &'a Excepthandler,
|
||||||
|
@ -46,12 +45,8 @@ impl Format<ASTFormatContext<'_>> for FormatExcepthandler<'_> {
|
||||||
// Format any end-of-line comments.
|
// Format any end-of-line comments.
|
||||||
let mut first = true;
|
let mut first = true;
|
||||||
for range in excepthandler.trivia.iter().filter_map(|trivia| {
|
for range in excepthandler.trivia.iter().filter_map(|trivia| {
|
||||||
if matches!(trivia.relationship, Relationship::Trailing) {
|
if trivia.relationship.is_trailing() {
|
||||||
if let TriviaKind::EndOfLineComment(range) = trivia.kind {
|
trivia.kind.end_of_line_comment()
|
||||||
Some(range)
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
|
|
|
@ -17,7 +17,7 @@ use crate::format::helpers::{is_self_closing, is_simple_power, is_simple_slice};
|
||||||
use crate::format::numbers::{complex_literal, float_literal, int_literal};
|
use crate::format::numbers::{complex_literal, float_literal, int_literal};
|
||||||
use crate::format::strings::string_literal;
|
use crate::format::strings::string_literal;
|
||||||
use crate::shared_traits::AsFormat;
|
use crate::shared_traits::AsFormat;
|
||||||
use crate::trivia::{Parenthesize, Relationship, TriviaKind};
|
use crate::trivia::{Parenthesize, TriviaKind};
|
||||||
|
|
||||||
pub struct FormatExpr<'a> {
|
pub struct FormatExpr<'a> {
|
||||||
item: &'a Expr,
|
item: &'a Expr,
|
||||||
|
@ -33,12 +33,8 @@ fn format_starred(
|
||||||
// Format any end-of-line comments.
|
// Format any end-of-line comments.
|
||||||
let mut first = true;
|
let mut first = true;
|
||||||
for range in expr.trivia.iter().filter_map(|trivia| {
|
for range in expr.trivia.iter().filter_map(|trivia| {
|
||||||
if matches!(trivia.relationship, Relationship::Trailing) {
|
if trivia.relationship.is_trailing() {
|
||||||
if let TriviaKind::EndOfLineComment(range) = trivia.kind {
|
trivia.kind.end_of_line_comment()
|
||||||
Some(range)
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
|
@ -62,12 +58,8 @@ fn format_name(
|
||||||
// Format any end-of-line comments.
|
// Format any end-of-line comments.
|
||||||
let mut first = true;
|
let mut first = true;
|
||||||
for range in expr.trivia.iter().filter_map(|trivia| {
|
for range in expr.trivia.iter().filter_map(|trivia| {
|
||||||
if matches!(trivia.relationship, Relationship::Trailing) {
|
if trivia.relationship.is_trailing() {
|
||||||
if let TriviaKind::EndOfLineComment(range) = trivia.kind {
|
trivia.kind.end_of_line_comment()
|
||||||
Some(range)
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
|
@ -96,7 +88,7 @@ fn format_subscript(
|
||||||
|
|
||||||
// Apply any dangling comments.
|
// Apply any dangling comments.
|
||||||
for trivia in &expr.trivia {
|
for trivia in &expr.trivia {
|
||||||
if matches!(trivia.relationship, Relationship::Dangling) {
|
if trivia.relationship.is_dangling() {
|
||||||
if let TriviaKind::OwnLineComment(range) = trivia.kind {
|
if let TriviaKind::OwnLineComment(range) = trivia.kind {
|
||||||
write!(f, [expand_parent()])?;
|
write!(f, [expand_parent()])?;
|
||||||
write!(f, [hard_line_break()])?;
|
write!(f, [hard_line_break()])?;
|
||||||
|
@ -136,7 +128,7 @@ fn format_tuple(
|
||||||
write!(
|
write!(
|
||||||
f,
|
f,
|
||||||
[group(&format_with(|f| {
|
[group(&format_with(|f| {
|
||||||
if matches!(expr.parentheses, Parenthesize::IfExpanded) {
|
if expr.parentheses.is_if_expanded() {
|
||||||
write!(f, [if_group_breaks(&text("("))])?;
|
write!(f, [if_group_breaks(&text("("))])?;
|
||||||
}
|
}
|
||||||
if matches!(
|
if matches!(
|
||||||
|
@ -146,10 +138,8 @@ fn format_tuple(
|
||||||
write!(
|
write!(
|
||||||
f,
|
f,
|
||||||
[soft_block_indent(&format_with(|f| {
|
[soft_block_indent(&format_with(|f| {
|
||||||
let magic_trailing_comma = expr
|
let magic_trailing_comma =
|
||||||
.trivia
|
expr.trivia.iter().any(|c| c.kind.is_magic_trailing_comma());
|
||||||
.iter()
|
|
||||||
.any(|c| matches!(c.kind, TriviaKind::MagicTrailingComma));
|
|
||||||
let is_unbroken =
|
let is_unbroken =
|
||||||
expr.location.row() == expr.end_location.unwrap().row();
|
expr.location.row() == expr.end_location.unwrap().row();
|
||||||
if magic_trailing_comma {
|
if magic_trailing_comma {
|
||||||
|
@ -170,10 +160,8 @@ fn format_tuple(
|
||||||
}))]
|
}))]
|
||||||
)?;
|
)?;
|
||||||
} else {
|
} else {
|
||||||
let magic_trailing_comma = expr
|
let magic_trailing_comma =
|
||||||
.trivia
|
expr.trivia.iter().any(|c| c.kind.is_magic_trailing_comma());
|
||||||
.iter()
|
|
||||||
.any(|c| matches!(c.kind, TriviaKind::MagicTrailingComma));
|
|
||||||
let is_unbroken = expr.location.row() == expr.end_location.unwrap().row();
|
let is_unbroken = expr.location.row() == expr.end_location.unwrap().row();
|
||||||
if magic_trailing_comma {
|
if magic_trailing_comma {
|
||||||
write!(f, [expand_parent()])?;
|
write!(f, [expand_parent()])?;
|
||||||
|
@ -190,7 +178,7 @@ fn format_tuple(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if matches!(expr.parentheses, Parenthesize::IfExpanded) {
|
if expr.parentheses.is_if_expanded() {
|
||||||
write!(f, [if_group_breaks(&text(")"))])?;
|
write!(f, [if_group_breaks(&text(")"))])?;
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
|
@ -236,7 +224,7 @@ fn format_slice(
|
||||||
|
|
||||||
// Apply any dangling comments.
|
// Apply any dangling comments.
|
||||||
for trivia in &lower.trivia {
|
for trivia in &lower.trivia {
|
||||||
if matches!(trivia.relationship, Relationship::Dangling) {
|
if trivia.relationship.is_dangling() {
|
||||||
if let TriviaKind::OwnLineComment(range) = trivia.kind {
|
if let TriviaKind::OwnLineComment(range) = trivia.kind {
|
||||||
write!(f, [expand_parent()])?;
|
write!(f, [expand_parent()])?;
|
||||||
write!(f, [hard_line_break()])?;
|
write!(f, [hard_line_break()])?;
|
||||||
|
@ -256,12 +244,8 @@ fn format_slice(
|
||||||
// Format any end-of-line comments.
|
// Format any end-of-line comments.
|
||||||
let mut first = true;
|
let mut first = true;
|
||||||
for range in lower.trivia.iter().filter_map(|trivia| {
|
for range in lower.trivia.iter().filter_map(|trivia| {
|
||||||
if matches!(trivia.relationship, Relationship::Trailing) {
|
if trivia.relationship.is_trailing() {
|
||||||
if let TriviaKind::EndOfLineComment(range) = trivia.kind {
|
trivia.kind.end_of_line_comment()
|
||||||
Some(range)
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
|
@ -282,7 +266,7 @@ fn format_slice(
|
||||||
|
|
||||||
// Apply any dangling comments.
|
// Apply any dangling comments.
|
||||||
for trivia in &upper.trivia {
|
for trivia in &upper.trivia {
|
||||||
if matches!(trivia.relationship, Relationship::Dangling) {
|
if trivia.relationship.is_dangling() {
|
||||||
if let TriviaKind::OwnLineComment(range) = trivia.kind {
|
if let TriviaKind::OwnLineComment(range) = trivia.kind {
|
||||||
write!(f, [expand_parent()])?;
|
write!(f, [expand_parent()])?;
|
||||||
write!(f, [hard_line_break()])?;
|
write!(f, [hard_line_break()])?;
|
||||||
|
@ -295,12 +279,8 @@ fn format_slice(
|
||||||
// Format any end-of-line comments.
|
// Format any end-of-line comments.
|
||||||
let mut first = true;
|
let mut first = true;
|
||||||
for range in upper.trivia.iter().filter_map(|trivia| {
|
for range in upper.trivia.iter().filter_map(|trivia| {
|
||||||
if matches!(trivia.relationship, Relationship::Trailing) {
|
if trivia.relationship.is_trailing() {
|
||||||
if let TriviaKind::EndOfLineComment(range) = trivia.kind {
|
trivia.kind.end_of_line_comment()
|
||||||
Some(range)
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
|
@ -329,7 +309,7 @@ fn format_slice(
|
||||||
|
|
||||||
// Apply any dangling comments.
|
// Apply any dangling comments.
|
||||||
for trivia in &step.trivia {
|
for trivia in &step.trivia {
|
||||||
if matches!(trivia.relationship, Relationship::Dangling) {
|
if trivia.relationship.is_dangling() {
|
||||||
if let TriviaKind::OwnLineComment(range) = trivia.kind {
|
if let TriviaKind::OwnLineComment(range) = trivia.kind {
|
||||||
write!(f, [expand_parent()])?;
|
write!(f, [expand_parent()])?;
|
||||||
write!(f, [hard_line_break()])?;
|
write!(f, [hard_line_break()])?;
|
||||||
|
@ -342,12 +322,8 @@ fn format_slice(
|
||||||
// Format any end-of-line comments.
|
// Format any end-of-line comments.
|
||||||
let mut first = true;
|
let mut first = true;
|
||||||
for range in step.trivia.iter().filter_map(|trivia| {
|
for range in step.trivia.iter().filter_map(|trivia| {
|
||||||
if matches!(trivia.relationship, Relationship::Trailing) {
|
if trivia.relationship.is_trailing() {
|
||||||
if let TriviaKind::EndOfLineComment(range) = trivia.kind {
|
trivia.kind.end_of_line_comment()
|
||||||
Some(range)
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
|
@ -365,12 +341,8 @@ fn format_slice(
|
||||||
// Format any end-of-line comments.
|
// Format any end-of-line comments.
|
||||||
let mut first = true;
|
let mut first = true;
|
||||||
for range in expr.trivia.iter().filter_map(|trivia| {
|
for range in expr.trivia.iter().filter_map(|trivia| {
|
||||||
if matches!(trivia.relationship, Relationship::Trailing) {
|
if trivia.relationship.is_trailing() {
|
||||||
if let TriviaKind::EndOfLineComment(range) = trivia.kind {
|
trivia.kind.end_of_line_comment()
|
||||||
Some(range)
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
|
@ -391,10 +363,7 @@ fn format_list(
|
||||||
) -> FormatResult<()> {
|
) -> FormatResult<()> {
|
||||||
write!(f, [text("[")])?;
|
write!(f, [text("[")])?;
|
||||||
if !elts.is_empty() {
|
if !elts.is_empty() {
|
||||||
let magic_trailing_comma = expr
|
let magic_trailing_comma = expr.trivia.iter().any(|c| c.kind.is_magic_trailing_comma());
|
||||||
.trivia
|
|
||||||
.iter()
|
|
||||||
.any(|c| matches!(c.kind, TriviaKind::MagicTrailingComma));
|
|
||||||
write!(
|
write!(
|
||||||
f,
|
f,
|
||||||
[group(&format_args![soft_block_indent(&format_with(|f| {
|
[group(&format_args![soft_block_indent(&format_with(|f| {
|
||||||
|
@ -429,10 +398,7 @@ fn format_set(
|
||||||
} else {
|
} else {
|
||||||
write!(f, [text("{")])?;
|
write!(f, [text("{")])?;
|
||||||
if !elts.is_empty() {
|
if !elts.is_empty() {
|
||||||
let magic_trailing_comma = expr
|
let magic_trailing_comma = expr.trivia.iter().any(|c| c.kind.is_magic_trailing_comma());
|
||||||
.trivia
|
|
||||||
.iter()
|
|
||||||
.any(|c| matches!(c.kind, TriviaKind::MagicTrailingComma));
|
|
||||||
write!(
|
write!(
|
||||||
f,
|
f,
|
||||||
[group(&format_args![soft_block_indent(&format_with(|f| {
|
[group(&format_args![soft_block_indent(&format_with(|f| {
|
||||||
|
@ -474,12 +440,8 @@ fn format_call(
|
||||||
// Format any end-of-line comments.
|
// Format any end-of-line comments.
|
||||||
let mut first = true;
|
let mut first = true;
|
||||||
for range in expr.trivia.iter().filter_map(|trivia| {
|
for range in expr.trivia.iter().filter_map(|trivia| {
|
||||||
if matches!(trivia.relationship, Relationship::Trailing) {
|
if trivia.relationship.is_trailing() {
|
||||||
if let TriviaKind::EndOfLineComment(range) = trivia.kind {
|
trivia.kind.end_of_line_comment()
|
||||||
Some(range)
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
|
@ -495,12 +457,8 @@ fn format_call(
|
||||||
// Format any end-of-line comments.
|
// Format any end-of-line comments.
|
||||||
let mut first = true;
|
let mut first = true;
|
||||||
for range in expr.trivia.iter().filter_map(|trivia| {
|
for range in expr.trivia.iter().filter_map(|trivia| {
|
||||||
if matches!(trivia.relationship, Relationship::Trailing) {
|
if trivia.relationship.is_trailing() {
|
||||||
if let TriviaKind::EndOfLineComment(range) = trivia.kind {
|
trivia.kind.end_of_line_comment()
|
||||||
Some(range)
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
|
@ -511,10 +469,7 @@ fn format_call(
|
||||||
write!(f, [line_suffix(&literal(range))])?;
|
write!(f, [line_suffix(&literal(range))])?;
|
||||||
}
|
}
|
||||||
|
|
||||||
let magic_trailing_comma = expr
|
let magic_trailing_comma = expr.trivia.iter().any(|c| c.kind.is_magic_trailing_comma());
|
||||||
.trivia
|
|
||||||
.iter()
|
|
||||||
.any(|c| matches!(c.kind, TriviaKind::MagicTrailingComma));
|
|
||||||
write!(
|
write!(
|
||||||
f,
|
f,
|
||||||
[group(&format_args![soft_block_indent(&format_with(|f| {
|
[group(&format_args![soft_block_indent(&format_with(|f| {
|
||||||
|
@ -560,7 +515,7 @@ fn format_call(
|
||||||
|
|
||||||
// Apply any dangling trailing comments.
|
// Apply any dangling trailing comments.
|
||||||
for trivia in &expr.trivia {
|
for trivia in &expr.trivia {
|
||||||
if matches!(trivia.relationship, Relationship::Dangling) {
|
if trivia.relationship.is_dangling() {
|
||||||
if let TriviaKind::OwnLineComment(range) = trivia.kind {
|
if let TriviaKind::OwnLineComment(range) = trivia.kind {
|
||||||
write!(f, [expand_parent()])?;
|
write!(f, [expand_parent()])?;
|
||||||
write!(f, [hard_line_break()])?;
|
write!(f, [hard_line_break()])?;
|
||||||
|
@ -757,12 +712,8 @@ fn format_compare(
|
||||||
// Format any end-of-line comments.
|
// Format any end-of-line comments.
|
||||||
let mut first = true;
|
let mut first = true;
|
||||||
for range in expr.trivia.iter().filter_map(|trivia| {
|
for range in expr.trivia.iter().filter_map(|trivia| {
|
||||||
if matches!(trivia.relationship, Relationship::Trailing) {
|
if trivia.relationship.is_trailing() {
|
||||||
if let TriviaKind::EndOfLineComment(range) = trivia.kind {
|
trivia.kind.end_of_line_comment()
|
||||||
Some(range)
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
|
@ -819,10 +770,7 @@ fn format_dict(
|
||||||
) -> FormatResult<()> {
|
) -> FormatResult<()> {
|
||||||
write!(f, [text("{")])?;
|
write!(f, [text("{")])?;
|
||||||
if !keys.is_empty() {
|
if !keys.is_empty() {
|
||||||
let magic_trailing_comma = expr
|
let magic_trailing_comma = expr.trivia.iter().any(|c| c.kind.is_magic_trailing_comma());
|
||||||
.trivia
|
|
||||||
.iter()
|
|
||||||
.any(|c| matches!(c.kind, TriviaKind::MagicTrailingComma));
|
|
||||||
write!(
|
write!(
|
||||||
f,
|
f,
|
||||||
[soft_block_indent(&format_with(|f| {
|
[soft_block_indent(&format_with(|f| {
|
||||||
|
@ -889,12 +837,8 @@ fn format_attribute(
|
||||||
// Format any end-of-line comments.
|
// Format any end-of-line comments.
|
||||||
let mut first = true;
|
let mut first = true;
|
||||||
for range in expr.trivia.iter().filter_map(|trivia| {
|
for range in expr.trivia.iter().filter_map(|trivia| {
|
||||||
if matches!(trivia.relationship, Relationship::Trailing) {
|
if trivia.relationship.is_trailing() {
|
||||||
if let TriviaKind::EndOfLineComment(range) = trivia.kind {
|
trivia.kind.end_of_line_comment()
|
||||||
Some(range)
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
|
@ -929,12 +873,8 @@ fn format_bool_op(
|
||||||
// Format any end-of-line comments.
|
// Format any end-of-line comments.
|
||||||
let mut first = true;
|
let mut first = true;
|
||||||
for range in expr.trivia.iter().filter_map(|trivia| {
|
for range in expr.trivia.iter().filter_map(|trivia| {
|
||||||
if matches!(trivia.relationship, Relationship::Trailing) {
|
if trivia.relationship.is_trailing() {
|
||||||
if let TriviaKind::EndOfLineComment(range) = trivia.kind {
|
trivia.kind.end_of_line_comment()
|
||||||
Some(range)
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
|
@ -971,12 +911,8 @@ fn format_bin_op(
|
||||||
// Format any end-of-line comments.
|
// Format any end-of-line comments.
|
||||||
let mut first = true;
|
let mut first = true;
|
||||||
for range in expr.trivia.iter().filter_map(|trivia| {
|
for range in expr.trivia.iter().filter_map(|trivia| {
|
||||||
if matches!(trivia.relationship, Relationship::Trailing) {
|
if trivia.relationship.is_trailing() {
|
||||||
if let TriviaKind::EndOfLineComment(range) = trivia.kind {
|
trivia.kind.end_of_line_comment()
|
||||||
Some(range)
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
|
@ -1004,7 +940,7 @@ fn format_unary_op(
|
||||||
ExprKind::BoolOp { .. } | ExprKind::Compare { .. } | ExprKind::BinOp { .. }
|
ExprKind::BoolOp { .. } | ExprKind::Compare { .. } | ExprKind::BinOp { .. }
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
let parenthesized = matches!(operand.parentheses, Parenthesize::Always);
|
let parenthesized = operand.parentheses.is_always();
|
||||||
if !parenthesized {
|
if !parenthesized {
|
||||||
write!(f, [text("(")])?;
|
write!(f, [text("(")])?;
|
||||||
}
|
}
|
||||||
|
@ -1056,13 +992,13 @@ fn format_if_exp(
|
||||||
|
|
||||||
impl Format<ASTFormatContext<'_>> for FormatExpr<'_> {
|
impl Format<ASTFormatContext<'_>> for FormatExpr<'_> {
|
||||||
fn fmt(&self, f: &mut Formatter<ASTFormatContext<'_>>) -> FormatResult<()> {
|
fn fmt(&self, f: &mut Formatter<ASTFormatContext<'_>>) -> FormatResult<()> {
|
||||||
if matches!(self.item.parentheses, Parenthesize::Always) {
|
if self.item.parentheses.is_always() {
|
||||||
write!(f, [text("(")])?;
|
write!(f, [text("(")])?;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Any leading comments come on the line before.
|
// Any leading comments come on the line before.
|
||||||
for trivia in &self.item.trivia {
|
for trivia in &self.item.trivia {
|
||||||
if matches!(trivia.relationship, Relationship::Leading) {
|
if trivia.relationship.is_leading() {
|
||||||
if let TriviaKind::OwnLineComment(range) = trivia.kind {
|
if let TriviaKind::OwnLineComment(range) = trivia.kind {
|
||||||
write!(f, [expand_parent()])?;
|
write!(f, [expand_parent()])?;
|
||||||
write!(f, [literal(range)])?;
|
write!(f, [literal(range)])?;
|
||||||
|
@ -1130,7 +1066,7 @@ impl Format<ASTFormatContext<'_>> for FormatExpr<'_> {
|
||||||
|
|
||||||
// Any trailing comments come on the lines after.
|
// Any trailing comments come on the lines after.
|
||||||
for trivia in &self.item.trivia {
|
for trivia in &self.item.trivia {
|
||||||
if matches!(trivia.relationship, Relationship::Trailing) {
|
if trivia.relationship.is_trailing() {
|
||||||
if let TriviaKind::OwnLineComment(range) = trivia.kind {
|
if let TriviaKind::OwnLineComment(range) = trivia.kind {
|
||||||
write!(f, [expand_parent()])?;
|
write!(f, [expand_parent()])?;
|
||||||
write!(f, [literal(range)])?;
|
write!(f, [literal(range)])?;
|
||||||
|
@ -1139,7 +1075,7 @@ impl Format<ASTFormatContext<'_>> for FormatExpr<'_> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if matches!(self.item.parentheses, Parenthesize::Always) {
|
if self.item.parentheses.is_always() {
|
||||||
write!(f, [text(")")])?;
|
write!(f, [text(")")])?;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -12,7 +12,7 @@ use crate::cst::{
|
||||||
use crate::format::builders::{block, join_names};
|
use crate::format::builders::{block, join_names};
|
||||||
use crate::format::helpers::is_self_closing;
|
use crate::format::helpers::is_self_closing;
|
||||||
use crate::shared_traits::AsFormat;
|
use crate::shared_traits::AsFormat;
|
||||||
use crate::trivia::{Parenthesize, Relationship, TriviaKind};
|
use crate::trivia::TriviaKind;
|
||||||
|
|
||||||
fn format_break(f: &mut Formatter<ASTFormatContext<'_>>) -> FormatResult<()> {
|
fn format_break(f: &mut Formatter<ASTFormatContext<'_>>) -> FormatResult<()> {
|
||||||
write!(f, [text("break")])
|
write!(f, [text("break")])
|
||||||
|
@ -25,12 +25,8 @@ fn format_pass(f: &mut Formatter<ASTFormatContext<'_>>, stmt: &Stmt) -> FormatRe
|
||||||
// Format any end-of-line comments.
|
// Format any end-of-line comments.
|
||||||
let mut first = true;
|
let mut first = true;
|
||||||
for range in stmt.trivia.iter().filter_map(|trivia| {
|
for range in stmt.trivia.iter().filter_map(|trivia| {
|
||||||
if matches!(trivia.relationship, Relationship::Trailing) {
|
if trivia.relationship.is_trailing() {
|
||||||
if let TriviaKind::EndOfLineComment(range) = trivia.kind {
|
trivia.kind.end_of_line_comment()
|
||||||
Some(range)
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
|
@ -189,11 +185,7 @@ fn format_func_def(
|
||||||
dynamic_text(name, TextSize::default()),
|
dynamic_text(name, TextSize::default()),
|
||||||
text("("),
|
text("("),
|
||||||
group(&soft_block_indent(&format_with(|f| {
|
group(&soft_block_indent(&format_with(|f| {
|
||||||
if stmt
|
if stmt.trivia.iter().any(|c| c.kind.is_magic_trailing_comma()) {
|
||||||
.trivia
|
|
||||||
.iter()
|
|
||||||
.any(|c| matches!(c.kind, TriviaKind::MagicTrailingComma))
|
|
||||||
{
|
|
||||||
write!(f, [expand_parent()])?;
|
write!(f, [expand_parent()])?;
|
||||||
}
|
}
|
||||||
write!(f, [args.format()])
|
write!(f, [args.format()])
|
||||||
|
@ -211,12 +203,8 @@ fn format_func_def(
|
||||||
// Format any end-of-line comments.
|
// Format any end-of-line comments.
|
||||||
let mut first = true;
|
let mut first = true;
|
||||||
for range in stmt.trivia.iter().filter_map(|trivia| {
|
for range in stmt.trivia.iter().filter_map(|trivia| {
|
||||||
if matches!(trivia.relationship, Relationship::Trailing) {
|
if trivia.relationship.is_trailing() {
|
||||||
if let TriviaKind::EndOfLineComment(range) = trivia.kind {
|
trivia.kind.end_of_line_comment()
|
||||||
Some(range)
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
|
@ -260,12 +248,8 @@ fn format_assign(
|
||||||
// Format any end-of-line comments.
|
// Format any end-of-line comments.
|
||||||
let mut first = true;
|
let mut first = true;
|
||||||
for range in stmt.trivia.iter().filter_map(|trivia| {
|
for range in stmt.trivia.iter().filter_map(|trivia| {
|
||||||
if matches!(trivia.relationship, Relationship::Trailing) {
|
if trivia.relationship.is_trailing() {
|
||||||
if let TriviaKind::EndOfLineComment(range) = trivia.kind {
|
trivia.kind.end_of_line_comment()
|
||||||
Some(range)
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
|
@ -436,12 +420,8 @@ fn format_return(
|
||||||
// Format any end-of-line comments.
|
// Format any end-of-line comments.
|
||||||
let mut first = true;
|
let mut first = true;
|
||||||
for range in stmt.trivia.iter().filter_map(|trivia| {
|
for range in stmt.trivia.iter().filter_map(|trivia| {
|
||||||
if matches!(trivia.relationship, Relationship::Trailing) {
|
if trivia.relationship.is_trailing() {
|
||||||
if let TriviaKind::EndOfLineComment(range) = trivia.kind {
|
trivia.kind.end_of_line_comment()
|
||||||
Some(range)
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
|
@ -585,10 +565,7 @@ fn format_import_from(
|
||||||
if names.iter().any(|name| name.node.name == "*") {
|
if names.iter().any(|name| name.node.name == "*") {
|
||||||
write!(f, [text("*")])?;
|
write!(f, [text("*")])?;
|
||||||
} else {
|
} else {
|
||||||
let magic_trailing_comma = stmt
|
let magic_trailing_comma = stmt.trivia.iter().any(|c| c.kind.is_magic_trailing_comma());
|
||||||
.trivia
|
|
||||||
.iter()
|
|
||||||
.any(|c| matches!(c.kind, TriviaKind::MagicTrailingComma));
|
|
||||||
write!(
|
write!(
|
||||||
f,
|
f,
|
||||||
[group(&format_args![
|
[group(&format_args![
|
||||||
|
@ -616,12 +593,8 @@ fn format_import_from(
|
||||||
// Format any end-of-line comments.
|
// Format any end-of-line comments.
|
||||||
let mut first = true;
|
let mut first = true;
|
||||||
for range in stmt.trivia.iter().filter_map(|trivia| {
|
for range in stmt.trivia.iter().filter_map(|trivia| {
|
||||||
if matches!(trivia.relationship, Relationship::Trailing) {
|
if trivia.relationship.is_trailing() {
|
||||||
if let TriviaKind::EndOfLineComment(range) = trivia.kind {
|
trivia.kind.end_of_line_comment()
|
||||||
Some(range)
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
|
@ -640,7 +613,7 @@ fn format_expr(
|
||||||
stmt: &Stmt,
|
stmt: &Stmt,
|
||||||
expr: &Expr,
|
expr: &Expr,
|
||||||
) -> FormatResult<()> {
|
) -> FormatResult<()> {
|
||||||
if matches!(stmt.parentheses, Parenthesize::Always) {
|
if stmt.parentheses.is_always() {
|
||||||
write!(
|
write!(
|
||||||
f,
|
f,
|
||||||
[group(&format_args![
|
[group(&format_args![
|
||||||
|
@ -665,12 +638,8 @@ fn format_expr(
|
||||||
// Format any end-of-line comments.
|
// Format any end-of-line comments.
|
||||||
let mut first = true;
|
let mut first = true;
|
||||||
for range in stmt.trivia.iter().filter_map(|trivia| {
|
for range in stmt.trivia.iter().filter_map(|trivia| {
|
||||||
if matches!(trivia.relationship, Relationship::Trailing) {
|
if trivia.relationship.is_trailing() {
|
||||||
if let TriviaKind::EndOfLineComment(range) = trivia.kind {
|
trivia.kind.end_of_line_comment()
|
||||||
Some(range)
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
|
@ -730,7 +699,7 @@ impl Format<ASTFormatContext<'_>> for FormatStmt<'_> {
|
||||||
fn fmt(&self, f: &mut Formatter<ASTFormatContext<'_>>) -> FormatResult<()> {
|
fn fmt(&self, f: &mut Formatter<ASTFormatContext<'_>>) -> FormatResult<()> {
|
||||||
// Any leading comments come on the line before.
|
// Any leading comments come on the line before.
|
||||||
for trivia in &self.item.trivia {
|
for trivia in &self.item.trivia {
|
||||||
if matches!(trivia.relationship, Relationship::Leading) {
|
if trivia.relationship.is_leading() {
|
||||||
match trivia.kind {
|
match trivia.kind {
|
||||||
TriviaKind::EmptyLine => {
|
TriviaKind::EmptyLine => {
|
||||||
write!(f, [empty_line()])?;
|
write!(f, [empty_line()])?;
|
||||||
|
@ -886,7 +855,7 @@ impl Format<ASTFormatContext<'_>> for FormatStmt<'_> {
|
||||||
|
|
||||||
// Any trailing comments come on the lines after.
|
// Any trailing comments come on the lines after.
|
||||||
for trivia in &self.item.trivia {
|
for trivia in &self.item.trivia {
|
||||||
if matches!(trivia.relationship, Relationship::Trailing) {
|
if trivia.relationship.is_trailing() {
|
||||||
match trivia.kind {
|
match trivia.kind {
|
||||||
TriviaKind::EmptyLine => {
|
TriviaKind::EmptyLine => {
|
||||||
write!(f, [empty_line()])?;
|
write!(f, [empty_line()])?;
|
||||||
|
|
|
@ -8,7 +8,6 @@ use crate::context::ASTFormatContext;
|
||||||
use crate::core::helpers::{leading_quote, trailing_quote};
|
use crate::core::helpers::{leading_quote, trailing_quote};
|
||||||
use crate::core::types::Range;
|
use crate::core::types::Range;
|
||||||
use crate::cst::Expr;
|
use crate::cst::Expr;
|
||||||
use crate::trivia::Parenthesize;
|
|
||||||
|
|
||||||
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
|
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
|
||||||
pub struct StringLiteralPart {
|
pub struct StringLiteralPart {
|
||||||
|
@ -142,7 +141,7 @@ impl Format<ASTFormatContext<'_>> for StringLiteral<'_> {
|
||||||
write!(
|
write!(
|
||||||
f,
|
f,
|
||||||
[group(&format_with(|f| {
|
[group(&format_with(|f| {
|
||||||
if matches!(expr.parentheses, Parenthesize::IfExpanded) {
|
if expr.parentheses.is_if_expanded() {
|
||||||
write!(f, [if_group_breaks(&text("("))])?;
|
write!(f, [if_group_breaks(&text("("))])?;
|
||||||
}
|
}
|
||||||
for (i, elt) in elts.iter().enumerate() {
|
for (i, elt) in elts.iter().enumerate() {
|
||||||
|
@ -151,7 +150,7 @@ impl Format<ASTFormatContext<'_>> for StringLiteral<'_> {
|
||||||
write!(f, [soft_line_break_or_space()])?;
|
write!(f, [soft_line_break_or_space()])?;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if matches!(expr.parentheses, Parenthesize::IfExpanded) {
|
if expr.parentheses.is_if_expanded() {
|
||||||
write!(f, [if_group_breaks(&text(")"))])?;
|
write!(f, [if_group_breaks(&text(")"))])?;
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|
|
@ -49,10 +49,7 @@ impl<'a> Visitor<'a> for NewlineNormalizer {
|
||||||
// Remove any runs of empty lines greater than two in a row.
|
// Remove any runs of empty lines greater than two in a row.
|
||||||
let mut count = 0;
|
let mut count = 0;
|
||||||
stmt.trivia.retain(|c| {
|
stmt.trivia.retain(|c| {
|
||||||
if matches!(
|
if c.kind.is_empty_line() && c.relationship.is_leading() {
|
||||||
(c.kind, c.relationship),
|
|
||||||
(TriviaKind::EmptyLine, Relationship::Leading)
|
|
||||||
) {
|
|
||||||
count += 1;
|
count += 1;
|
||||||
count <= self.depth.max_newlines()
|
count <= self.depth.max_newlines()
|
||||||
} else {
|
} else {
|
||||||
|
@ -78,10 +75,7 @@ impl<'a> Visitor<'a> for NewlineNormalizer {
|
||||||
if seen_non_empty {
|
if seen_non_empty {
|
||||||
true
|
true
|
||||||
} else {
|
} else {
|
||||||
if matches!(
|
if c.kind.is_empty_line() && c.relationship.is_leading() {
|
||||||
(c.kind, c.relationship),
|
|
||||||
(TriviaKind::EmptyLine, Relationship::Leading)
|
|
||||||
) {
|
|
||||||
false
|
false
|
||||||
} else {
|
} else {
|
||||||
seen_non_empty = true;
|
seen_non_empty = true;
|
||||||
|
@ -104,12 +98,7 @@ impl<'a> Visitor<'a> for NewlineNormalizer {
|
||||||
let present_newlines = stmt
|
let present_newlines = stmt
|
||||||
.trivia
|
.trivia
|
||||||
.iter()
|
.iter()
|
||||||
.take_while(|c| {
|
.take_while(|c| c.kind.is_empty_line() && c.relationship.is_leading())
|
||||||
matches!(
|
|
||||||
(c.kind, c.relationship),
|
|
||||||
(TriviaKind::EmptyLine, Relationship::Leading)
|
|
||||||
)
|
|
||||||
})
|
|
||||||
.count();
|
.count();
|
||||||
if present_newlines < required_newlines {
|
if present_newlines < required_newlines {
|
||||||
for _ in 0..(required_newlines - present_newlines) {
|
for _ in 0..(required_newlines - present_newlines) {
|
||||||
|
@ -135,12 +124,7 @@ impl<'a> Visitor<'a> for NewlineNormalizer {
|
||||||
- stmt
|
- stmt
|
||||||
.trivia
|
.trivia
|
||||||
.iter()
|
.iter()
|
||||||
.take_while(|c| {
|
.take_while(|c| c.kind.is_empty_line() && c.relationship.is_leading())
|
||||||
matches!(
|
|
||||||
(c.kind, c.relationship),
|
|
||||||
(TriviaKind::EmptyLine, Relationship::Leading)
|
|
||||||
)
|
|
||||||
})
|
|
||||||
.count();
|
.count();
|
||||||
for _ in 0..num_to_insert {
|
for _ in 0..num_to_insert {
|
||||||
stmt.trivia.insert(
|
stmt.trivia.insert(
|
||||||
|
|
|
@ -4,15 +4,14 @@ use crate::core::types::Range;
|
||||||
use crate::core::visitor;
|
use crate::core::visitor;
|
||||||
use crate::core::visitor::Visitor;
|
use crate::core::visitor::Visitor;
|
||||||
use crate::cst::{Expr, ExprKind, Stmt, StmtKind};
|
use crate::cst::{Expr, ExprKind, Stmt, StmtKind};
|
||||||
use crate::trivia::{Parenthesize, TriviaKind};
|
use crate::trivia::Parenthesize;
|
||||||
use rustpython_parser::ast::Constant;
|
use rustpython_parser::ast::Constant;
|
||||||
|
|
||||||
/// Modify an [`Expr`] to infer parentheses, rather than respecting any user-provided trivia.
|
/// Modify an [`Expr`] to infer parentheses, rather than respecting any user-provided trivia.
|
||||||
fn use_inferred_parens(expr: &mut Expr) {
|
fn use_inferred_parens(expr: &mut Expr) {
|
||||||
// Remove parentheses, unless it's a generator expression, in which case, keep them.
|
// Remove parentheses, unless it's a generator expression, in which case, keep them.
|
||||||
if !matches!(expr.node, ExprKind::GeneratorExp { .. }) {
|
if !matches!(expr.node, ExprKind::GeneratorExp { .. }) {
|
||||||
expr.trivia
|
expr.trivia.retain(|trivia| !trivia.kind.is_parentheses());
|
||||||
.retain(|trivia| !matches!(trivia.kind, TriviaKind::Parentheses));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// If it's a tuple, add parentheses if it's a singleton; otherwise, we only need parentheses
|
// If it's a tuple, add parentheses if it's a singleton; otherwise, we only need parentheses
|
||||||
|
@ -35,8 +34,7 @@ impl<'a> Visitor<'a> for ParenthesesNormalizer<'_> {
|
||||||
// Always remove parentheses around statements, unless it's an expression statement,
|
// Always remove parentheses around statements, unless it's an expression statement,
|
||||||
// in which case, remove parentheses around the expression.
|
// in which case, remove parentheses around the expression.
|
||||||
let before = stmt.trivia.len();
|
let before = stmt.trivia.len();
|
||||||
stmt.trivia
|
stmt.trivia.retain(|trivia| !trivia.kind.is_parentheses());
|
||||||
.retain(|trivia| !matches!(trivia.kind, TriviaKind::Parentheses));
|
|
||||||
let after = stmt.trivia.len();
|
let after = stmt.trivia.len();
|
||||||
if let StmtKind::Expr { value } = &mut stmt.node {
|
if let StmtKind::Expr { value } = &mut stmt.node {
|
||||||
if before != after {
|
if before != after {
|
||||||
|
@ -112,8 +110,7 @@ impl<'a> Visitor<'a> for ParenthesesNormalizer<'_> {
|
||||||
fn visit_expr(&mut self, expr: &'a mut Expr) {
|
fn visit_expr(&mut self, expr: &'a mut Expr) {
|
||||||
// Always retain parentheses around expressions.
|
// Always retain parentheses around expressions.
|
||||||
let before = expr.trivia.len();
|
let before = expr.trivia.len();
|
||||||
expr.trivia
|
expr.trivia.retain(|trivia| !trivia.kind.is_parentheses());
|
||||||
.retain(|trivia| !matches!(trivia.kind, TriviaKind::Parentheses));
|
|
||||||
let after = expr.trivia.len();
|
let after = expr.trivia.len();
|
||||||
if before != after {
|
if before != after {
|
||||||
expr.parentheses = Parenthesize::Always;
|
expr.parentheses = Parenthesize::Always;
|
||||||
|
@ -169,7 +166,7 @@ impl<'a> Visitor<'a> for ParenthesesNormalizer<'_> {
|
||||||
if !slice
|
if !slice
|
||||||
.trivia
|
.trivia
|
||||||
.iter()
|
.iter()
|
||||||
.any(|trivia| matches!(trivia.kind, TriviaKind::Parentheses))
|
.any(|trivia| trivia.kind.is_parentheses())
|
||||||
{
|
{
|
||||||
value.parentheses = Parenthesize::Never;
|
value.parentheses = Parenthesize::Never;
|
||||||
}
|
}
|
||||||
|
|
|
@ -48,7 +48,7 @@ pub struct TriviaToken {
|
||||||
pub kind: TriviaTokenKind,
|
pub kind: TriviaTokenKind,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
#[derive(Clone, Copy, Debug, PartialEq, Eq, is_macro::Is)]
|
||||||
pub enum TriviaKind {
|
pub enum TriviaKind {
|
||||||
/// A Comment that is separated by at least one line break from the
|
/// A Comment that is separated by at least one line break from the
|
||||||
/// preceding token.
|
/// preceding token.
|
||||||
|
@ -77,14 +77,14 @@ pub enum TriviaKind {
|
||||||
Parentheses,
|
Parentheses,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
#[derive(Clone, Copy, Debug, PartialEq, Eq, is_macro::Is)]
|
||||||
pub enum Relationship {
|
pub enum Relationship {
|
||||||
Leading,
|
Leading,
|
||||||
Trailing,
|
Trailing,
|
||||||
Dangling,
|
Dangling,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
#[derive(Clone, Copy, Debug, PartialEq, Eq, is_macro::Is)]
|
||||||
pub enum Parenthesize {
|
pub enum Parenthesize {
|
||||||
/// Always parenthesize the statement or expression.
|
/// Always parenthesize the statement or expression.
|
||||||
Always,
|
Always,
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue