-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Unify enums used for internal representation of quoting style #10383
Conversation
d39cd95
to
05f68dd
Compare
|
Another option would be to use |
Ahh, that makes sense... I should have checked what the config option was called, sorry! I'll change the formatter enum's name back, and change the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is great! So many lines red lines :)
let expr = self.semantic.current_expression()?; | ||
let string_range = self.indexer.fstring_ranges().innermost(expr.start())?; | ||
let trailing_quote = trailing_quote(self.locator.slice(string_range))?; | ||
|
||
// Invert the quote character, if it's a single quote. | ||
match trailing_quote { | ||
"'" => Some(Quote::Double), | ||
"\"" => Some(Quote::Single), | ||
_ => None, | ||
} | ||
let ast::ExprFString { value, .. } = self | ||
.semantic | ||
.current_expressions() | ||
.find_map(|expr| expr.as_f_string_expr())?; | ||
Some(value.iter().next()?.quote_style().opposite()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh nice, I like this change as the current_expressions
start with innermost f-string in case of nested f-strings.
Summary
Currently we have four enums across the codebase for representing the fact that Python strings can either use single quotes (
'
) or double quotes ("
):ruff/crates/ruff_python_ast/src/str.rs
Lines 6 to 13 in c269c1a
ruff/crates/ruff_python_formatter/src/string/mod.rs
Lines 234 to 242 in c269c1a
ruff/crates/ruff_python_codegen/src/stylist.rs
Lines 97 to 103 in c269c1a
ruff/crates/ruff_python_literal/src/escape.rs
Lines 1 to 5 in c269c1a
This means we have a lot of
impl From<QuoteEnum> for IdenticalOtherQuoteEnum
methods, and it's generally somewhat confusing to keep track of the subtle differences between them. This PR unfies them into one enum inruff_python_ast::str::QuoteStyle
.Using
ruff_python_ast::str::QuoteStyle
in the formatter would have created an awkward name clash withruff_python_formatter::options::QuoteStyle
, so I renamedruff_python_formatter::options::QuoteStyle
toruff_python_formatter::options::QuotePreference
. This seemed to me to more accurately reflect what that enum was for, but an alternative here would have been to renameruff_python_ast::str::QuoteStyle
. (Yet another alternative would be to always use the fully qualified name ofruff_python_ast::str:QuoteStyle
in the formatter crate, but that felt too cumbersome to me.)Test Plan
cargo test