mirror of
https://github.com/astral-sh/ruff
synced 2026-01-09 07:34:06 -05:00
Implement template strings (#17851)
This PR implements template strings (t-strings) in the parser and formatter for Ruff. Minimal changes necessary to compile were made in other parts of the code (e.g. ty, the linter, etc.). These will be covered properly in follow-up PRs.
This commit is contained in:
@@ -314,15 +314,14 @@ fn handle_enclosed_comment<'a>(
|
||||
AnyNodeRef::StmtImportFrom(import_from) => handle_import_from_comment(comment, import_from),
|
||||
AnyNodeRef::StmtWith(with_) => handle_with_comment(comment, with_),
|
||||
AnyNodeRef::ExprCall(_) => handle_call_comment(comment),
|
||||
AnyNodeRef::ExprStringLiteral(_) => {
|
||||
if let Some(AnyNodeRef::FString(fstring)) = comment.enclosing_parent() {
|
||||
CommentPlacement::dangling(fstring, comment)
|
||||
} else {
|
||||
CommentPlacement::Default(comment)
|
||||
}
|
||||
}
|
||||
AnyNodeRef::ExprStringLiteral(_) => match comment.enclosing_parent() {
|
||||
Some(AnyNodeRef::FString(fstring)) => CommentPlacement::dangling(fstring, comment),
|
||||
Some(AnyNodeRef::TString(tstring)) => CommentPlacement::dangling(tstring, comment),
|
||||
_ => CommentPlacement::Default(comment),
|
||||
},
|
||||
AnyNodeRef::FString(fstring) => CommentPlacement::dangling(fstring, comment),
|
||||
AnyNodeRef::FStringExpressionElement(_) => {
|
||||
AnyNodeRef::TString(tstring) => CommentPlacement::dangling(tstring, comment),
|
||||
AnyNodeRef::InterpolatedElement(_) => {
|
||||
// Handle comments after the format specifier (should be rare):
|
||||
//
|
||||
// ```python
|
||||
@@ -336,7 +335,8 @@ fn handle_enclosed_comment<'a>(
|
||||
if matches!(
|
||||
comment.preceding_node(),
|
||||
Some(
|
||||
AnyNodeRef::FStringExpressionElement(_) | AnyNodeRef::FStringLiteralElement(_)
|
||||
AnyNodeRef::InterpolatedElement(_)
|
||||
| AnyNodeRef::InterpolatedStringLiteralElement(_)
|
||||
)
|
||||
) {
|
||||
CommentPlacement::trailing(comment.enclosing_node(), comment)
|
||||
@@ -344,6 +344,7 @@ fn handle_enclosed_comment<'a>(
|
||||
handle_bracketed_end_of_line_comment(comment, source)
|
||||
}
|
||||
}
|
||||
|
||||
AnyNodeRef::ExprList(_)
|
||||
| AnyNodeRef::ExprSet(_)
|
||||
| AnyNodeRef::ExprListComp(_)
|
||||
|
||||
Reference in New Issue
Block a user