mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-27 13:54:25 +00:00
parent
a95deec00f
commit
424b720c19
62 changed files with 1799 additions and 3890 deletions
|
@ -14,7 +14,6 @@ use crate::expression::parentheses::{
|
|||
optional_parentheses, parenthesized, NeedsParentheses, OptionalParentheses, Parentheses,
|
||||
};
|
||||
use crate::prelude::*;
|
||||
use crate::preview::is_join_implicit_concatenated_string_enabled;
|
||||
|
||||
pub(crate) mod pattern_arguments;
|
||||
pub(crate) mod pattern_keyword;
|
||||
|
@ -227,7 +226,7 @@ pub(crate) fn can_pattern_omit_optional_parentheses(
|
|||
pattern: &Pattern,
|
||||
context: &PyFormatContext,
|
||||
) -> bool {
|
||||
let mut visitor = CanOmitOptionalParenthesesVisitor::new(context);
|
||||
let mut visitor = CanOmitOptionalParenthesesVisitor::default();
|
||||
visitor.visit_pattern(pattern, context);
|
||||
|
||||
if !visitor.any_parenthesized_expressions {
|
||||
|
@ -272,32 +271,16 @@ pub(crate) fn can_pattern_omit_optional_parentheses(
|
|||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
#[derive(Debug, Default)]
|
||||
struct CanOmitOptionalParenthesesVisitor<'input> {
|
||||
max_precedence: OperatorPrecedence,
|
||||
max_precedence_count: usize,
|
||||
any_parenthesized_expressions: bool,
|
||||
join_implicit_concatenated_strings: bool,
|
||||
last: Option<&'input Pattern>,
|
||||
first: First<'input>,
|
||||
}
|
||||
|
||||
impl<'a> CanOmitOptionalParenthesesVisitor<'a> {
|
||||
fn new(context: &PyFormatContext) -> Self {
|
||||
Self {
|
||||
max_precedence: OperatorPrecedence::default(),
|
||||
max_precedence_count: 0,
|
||||
any_parenthesized_expressions: false,
|
||||
// TODO: Derive default for `CanOmitOptionalParenthesesVisitor` when removing the `join_implicit_concatenated_strings`
|
||||
// preview style.
|
||||
join_implicit_concatenated_strings: is_join_implicit_concatenated_string_enabled(
|
||||
context,
|
||||
),
|
||||
last: None,
|
||||
first: First::default(),
|
||||
}
|
||||
}
|
||||
|
||||
fn visit_pattern(&mut self, pattern: &'a Pattern, context: &PyFormatContext) {
|
||||
match pattern {
|
||||
Pattern::MatchSequence(_) | Pattern::MatchMapping(_) => {
|
||||
|
@ -305,27 +288,11 @@ impl<'a> CanOmitOptionalParenthesesVisitor<'a> {
|
|||
}
|
||||
|
||||
Pattern::MatchValue(value) => match &*value.value {
|
||||
Expr::StringLiteral(string) => {
|
||||
if !self.join_implicit_concatenated_strings {
|
||||
self.update_max_precedence(OperatorPrecedence::String, string.value.len());
|
||||
}
|
||||
}
|
||||
Expr::BytesLiteral(bytes) => {
|
||||
if !self.join_implicit_concatenated_strings {
|
||||
self.update_max_precedence(OperatorPrecedence::String, bytes.value.len());
|
||||
}
|
||||
}
|
||||
Expr::StringLiteral(_) |
|
||||
Expr::BytesLiteral(_) |
|
||||
// F-strings are allowed according to python's grammar but fail with a syntax error at runtime.
|
||||
// That's why we need to support them for formatting.
|
||||
Expr::FString(string) => {
|
||||
if !self.join_implicit_concatenated_strings {
|
||||
self.update_max_precedence(
|
||||
OperatorPrecedence::String,
|
||||
string.value.as_slice().len(),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Expr::FString(_) |
|
||||
Expr::NumberLiteral(_) | Expr::Attribute(_) | Expr::UnaryOp(_) => {
|
||||
// require no state update other than visit_pattern does.
|
||||
}
|
||||
|
@ -397,8 +364,6 @@ enum OperatorPrecedence {
|
|||
None,
|
||||
Additive,
|
||||
Or,
|
||||
// Implicit string concatenation
|
||||
String,
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, Debug, Default)]
|
||||
|
|
|
@ -5,7 +5,6 @@ use ruff_python_ast::PatternMatchAs;
|
|||
use crate::comments::dangling_comments;
|
||||
use crate::expression::parentheses::{NeedsParentheses, OptionalParentheses};
|
||||
use crate::prelude::*;
|
||||
use crate::preview::is_match_case_parentheses_enabled;
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct FormatPatternMatchAs;
|
||||
|
@ -55,16 +54,12 @@ impl NeedsParentheses for PatternMatchAs {
|
|||
fn needs_parentheses(
|
||||
&self,
|
||||
_parent: AnyNodeRef,
|
||||
context: &PyFormatContext,
|
||||
_context: &PyFormatContext,
|
||||
) -> OptionalParentheses {
|
||||
if is_match_case_parentheses_enabled(context) {
|
||||
if self.name.is_some() {
|
||||
OptionalParentheses::Multiline
|
||||
} else {
|
||||
OptionalParentheses::BestFit
|
||||
}
|
||||
} else {
|
||||
if self.name.is_some() {
|
||||
OptionalParentheses::Multiline
|
||||
} else {
|
||||
OptionalParentheses::BestFit
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -8,7 +8,6 @@ use crate::expression::parentheses::{
|
|||
OptionalParentheses,
|
||||
};
|
||||
use crate::prelude::*;
|
||||
use crate::preview::is_match_case_parentheses_enabled;
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct FormatPatternMatchOr;
|
||||
|
@ -43,11 +42,7 @@ impl FormatNodeRule<PatternMatchOr> for FormatPatternMatchOr {
|
|||
Ok(())
|
||||
});
|
||||
|
||||
if is_match_case_parentheses_enabled(f.context()) {
|
||||
in_parentheses_only_group(&inner).fmt(f)
|
||||
} else {
|
||||
inner.fmt(f)
|
||||
}
|
||||
in_parentheses_only_group(&inner).fmt(f)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -3,7 +3,6 @@ use ruff_python_ast::{PatternMatchSingleton, Singleton};
|
|||
|
||||
use crate::expression::parentheses::{NeedsParentheses, OptionalParentheses};
|
||||
use crate::prelude::*;
|
||||
use crate::preview::is_match_case_parentheses_enabled;
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct FormatPatternMatchSingleton;
|
||||
|
@ -22,12 +21,8 @@ impl NeedsParentheses for PatternMatchSingleton {
|
|||
fn needs_parentheses(
|
||||
&self,
|
||||
_parent: AnyNodeRef,
|
||||
context: &PyFormatContext,
|
||||
_context: &PyFormatContext,
|
||||
) -> OptionalParentheses {
|
||||
if is_match_case_parentheses_enabled(context) {
|
||||
OptionalParentheses::BestFit
|
||||
} else {
|
||||
OptionalParentheses::Never
|
||||
}
|
||||
OptionalParentheses::BestFit
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3,7 +3,6 @@ use ruff_python_ast::PatternMatchValue;
|
|||
|
||||
use crate::expression::parentheses::{NeedsParentheses, OptionalParentheses, Parentheses};
|
||||
use crate::prelude::*;
|
||||
use crate::preview::is_match_case_parentheses_enabled;
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct FormatPatternMatchValue;
|
||||
|
@ -21,10 +20,6 @@ impl NeedsParentheses for PatternMatchValue {
|
|||
parent: AnyNodeRef,
|
||||
context: &PyFormatContext,
|
||||
) -> OptionalParentheses {
|
||||
if is_match_case_parentheses_enabled(context) {
|
||||
self.value.needs_parentheses(parent, context)
|
||||
} else {
|
||||
OptionalParentheses::Never
|
||||
}
|
||||
self.value.needs_parentheses(parent, context)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue