mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-09 21:28:04 +00:00
Preserve tuple parentheses in case patterns (#18147)
This commit is contained in:
parent
01eeb2f0d6
commit
bdf488462a
6 changed files with 86 additions and 18 deletions
|
@ -79,9 +79,26 @@ pub(crate) enum SequenceType {
|
|||
|
||||
impl SequenceType {
|
||||
pub(crate) fn from_pattern(pattern: &PatternMatchSequence, source: &str) -> SequenceType {
|
||||
if source[pattern.range()].starts_with('[') {
|
||||
let before_first_pattern = &source[TextRange::new(
|
||||
pattern.start(),
|
||||
pattern
|
||||
.patterns
|
||||
.first()
|
||||
.map(Ranged::start)
|
||||
.unwrap_or(pattern.end()),
|
||||
)];
|
||||
let after_last_patttern = &source[TextRange::new(
|
||||
pattern.start(),
|
||||
pattern
|
||||
.patterns
|
||||
.first()
|
||||
.map(Ranged::end)
|
||||
.unwrap_or(pattern.end()),
|
||||
)];
|
||||
|
||||
if before_first_pattern.starts_with('[') && !after_last_patttern.ends_with(',') {
|
||||
SequenceType::List
|
||||
} else if source[pattern.range()].starts_with('(') {
|
||||
} else if before_first_pattern.starts_with('(') {
|
||||
// If the pattern is empty, it must be a parenthesized tuple with no members. (This
|
||||
// branch exists to differentiate between a tuple with and without its own parentheses,
|
||||
// but a tuple without its own parentheses must have at least one member.)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue