mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-27 12:29:48 +00:00
Add support for trailing colons in slice expressions (#3077)
This commit is contained in:
parent
7d4e513a82
commit
ce8953442d
5 changed files with 29 additions and 2 deletions
|
@ -22,6 +22,10 @@ impl Format<ASTFormatContext<'_>> for FormatComprehension<'_> {
|
||||||
let comprehension = self.item;
|
let comprehension = self.item;
|
||||||
|
|
||||||
write!(f, [soft_line_break_or_space()])?;
|
write!(f, [soft_line_break_or_space()])?;
|
||||||
|
if comprehension.is_async > 0 {
|
||||||
|
write!(f, [text("async")])?;
|
||||||
|
write!(f, [space()])?;
|
||||||
|
}
|
||||||
write!(f, [text("for")])?;
|
write!(f, [text("for")])?;
|
||||||
write!(f, [space()])?;
|
write!(f, [space()])?;
|
||||||
// TODO(charlie): If this is an unparenthesized tuple, we need to avoid expanding it.
|
// TODO(charlie): If this is an unparenthesized tuple, we need to avoid expanding it.
|
||||||
|
|
|
@ -218,6 +218,17 @@ fn format_slice(
|
||||||
write!(f, [space()])?;
|
write!(f, [space()])?;
|
||||||
}
|
}
|
||||||
write!(f, [step.format()])?;
|
write!(f, [step.format()])?;
|
||||||
|
} else {
|
||||||
|
let magic_trailing_colon = expr
|
||||||
|
.trivia
|
||||||
|
.iter()
|
||||||
|
.any(|c| matches!(c.kind, TriviaKind::MagicTrailingColon));
|
||||||
|
if magic_trailing_colon {
|
||||||
|
if !is_simple && upper.is_some() {
|
||||||
|
write!(f, [space()])?;
|
||||||
|
}
|
||||||
|
write!(f, [text(":")])?;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|
|
@ -70,6 +70,7 @@ mod tests {
|
||||||
#[test_case(Path::new("simple_cases/beginning_backslash.py"); "beginning_backslash")]
|
#[test_case(Path::new("simple_cases/beginning_backslash.py"); "beginning_backslash")]
|
||||||
#[test_case(Path::new("simple_cases/import_spacing.py"); "import_spacing")]
|
#[test_case(Path::new("simple_cases/import_spacing.py"); "import_spacing")]
|
||||||
#[test_case(Path::new("simple_cases/power_op_spacing.py"); "power_op_spacing")]
|
#[test_case(Path::new("simple_cases/power_op_spacing.py"); "power_op_spacing")]
|
||||||
|
#[test_case(Path::new("simple_cases/slices.py"); "slices")]
|
||||||
fn passing(path: &Path) -> Result<()> {
|
fn passing(path: &Path) -> Result<()> {
|
||||||
let snapshot = format!("{}", path.display());
|
let snapshot = format!("{}", path.display());
|
||||||
let content = std::fs::read_to_string(test_resource_path(
|
let content = std::fs::read_to_string(test_resource_path(
|
||||||
|
|
|
@ -34,4 +34,3 @@ ham[lower:upper], ham[lower:upper:], ham[lower::step]
|
||||||
# ham[lower+offset : upper+offset]
|
# ham[lower+offset : upper+offset]
|
||||||
ham[: upper_fn(x) : step_fn(x)], ham[:: step_fn(x)]
|
ham[: upper_fn(x) : step_fn(x)], ham[:: step_fn(x)]
|
||||||
ham[lower + offset : upper + offset]
|
ham[lower + offset : upper + offset]
|
||||||
|
|
|
@ -31,6 +31,7 @@ pub enum TriviaTokenKind {
|
||||||
OwnLineComment,
|
OwnLineComment,
|
||||||
EndOfLineComment,
|
EndOfLineComment,
|
||||||
MagicTrailingComma,
|
MagicTrailingComma,
|
||||||
|
MagicTrailingColon,
|
||||||
EmptyLine,
|
EmptyLine,
|
||||||
Parentheses,
|
Parentheses,
|
||||||
}
|
}
|
||||||
|
@ -66,6 +67,7 @@ pub enum TriviaKind {
|
||||||
/// ```
|
/// ```
|
||||||
EndOfLineComment(Range),
|
EndOfLineComment(Range),
|
||||||
MagicTrailingComma,
|
MagicTrailingComma,
|
||||||
|
MagicTrailingColon,
|
||||||
EmptyLine,
|
EmptyLine,
|
||||||
Parentheses,
|
Parentheses,
|
||||||
}
|
}
|
||||||
|
@ -100,6 +102,10 @@ impl Trivia {
|
||||||
kind: TriviaKind::MagicTrailingComma,
|
kind: TriviaKind::MagicTrailingComma,
|
||||||
relationship,
|
relationship,
|
||||||
},
|
},
|
||||||
|
TriviaTokenKind::MagicTrailingColon => Self {
|
||||||
|
kind: TriviaKind::MagicTrailingColon,
|
||||||
|
relationship,
|
||||||
|
},
|
||||||
TriviaTokenKind::EmptyLine => Self {
|
TriviaTokenKind::EmptyLine => Self {
|
||||||
kind: TriviaKind::EmptyLine,
|
kind: TriviaKind::EmptyLine,
|
||||||
relationship,
|
relationship,
|
||||||
|
@ -163,6 +169,12 @@ pub fn extract_trivia_tokens(lxr: &[LexResult]) -> Vec<TriviaToken> {
|
||||||
end: *prev_end,
|
end: *prev_end,
|
||||||
kind: TriviaTokenKind::MagicTrailingComma,
|
kind: TriviaTokenKind::MagicTrailingComma,
|
||||||
});
|
});
|
||||||
|
} else if prev_tok == &Tok::Colon {
|
||||||
|
tokens.push(TriviaToken {
|
||||||
|
start: *prev_start,
|
||||||
|
end: *prev_end,
|
||||||
|
kind: TriviaTokenKind::MagicTrailingColon,
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -844,7 +856,7 @@ pub fn decorate_trivia(tokens: Vec<TriviaToken>, python_ast: &[Stmt]) -> TriviaI
|
||||||
unreachable!("Attach token to the ast: {:?}", token);
|
unreachable!("Attach token to the ast: {:?}", token);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
TriviaTokenKind::MagicTrailingComma => {
|
TriviaTokenKind::MagicTrailingComma | TriviaTokenKind::MagicTrailingColon => {
|
||||||
if let Some(enclosing_node) = enclosing_node {
|
if let Some(enclosing_node) = enclosing_node {
|
||||||
add_comment(
|
add_comment(
|
||||||
Trivia::from_token(&token, Relationship::Trailing),
|
Trivia::from_token(&token, Relationship::Trailing),
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue