-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
mod.rs
438 lines (402 loc) · 16.7 KB
/
mod.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
use rustpython_parser::ast;
use rustpython_parser::ast::{Expr, Operator};
use std::cmp::Ordering;
use crate::builders::parenthesize_if_expands;
use ruff_formatter::{FormatOwnedWithRule, FormatRefWithRule, FormatRule, FormatRuleWithOptions};
use ruff_python_ast::node::AnyNodeRef;
use ruff_python_ast::visitor::preorder::{walk_expr, PreorderVisitor};
use crate::context::NodeLevel;
use crate::expression::expr_tuple::TupleParentheses;
use crate::expression::parentheses::{
is_expression_parenthesized, optional_parentheses, parenthesized, NeedsParentheses,
Parentheses, Parenthesize,
};
use crate::expression::string::StringLayout;
use crate::prelude::*;
pub(crate) mod expr_attribute;
pub(crate) mod expr_await;
pub(crate) mod expr_bin_op;
pub(crate) mod expr_bool_op;
pub(crate) mod expr_call;
pub(crate) mod expr_compare;
pub(crate) mod expr_constant;
pub(crate) mod expr_dict;
pub(crate) mod expr_dict_comp;
pub(crate) mod expr_formatted_value;
pub(crate) mod expr_generator_exp;
pub(crate) mod expr_if_exp;
pub(crate) mod expr_joined_str;
pub(crate) mod expr_lambda;
pub(crate) mod expr_list;
pub(crate) mod expr_list_comp;
pub(crate) mod expr_name;
pub(crate) mod expr_named_expr;
pub(crate) mod expr_set;
pub(crate) mod expr_set_comp;
pub(crate) mod expr_slice;
pub(crate) mod expr_starred;
pub(crate) mod expr_subscript;
pub(crate) mod expr_tuple;
pub(crate) mod expr_unary_op;
pub(crate) mod expr_yield;
pub(crate) mod expr_yield_from;
pub(crate) mod parentheses;
pub(crate) mod string;
#[derive(Default)]
pub struct FormatExpr {
parenthesize: Parenthesize,
}
impl FormatRuleWithOptions<Expr, PyFormatContext<'_>> for FormatExpr {
type Options = Parenthesize;
fn with_options(mut self, options: Self::Options) -> Self {
self.parenthesize = options;
self
}
}
impl FormatRule<Expr, PyFormatContext<'_>> for FormatExpr {
fn fmt(&self, item: &Expr, f: &mut PyFormatter) -> FormatResult<()> {
let parentheses = item.needs_parentheses(self.parenthesize, f.context());
let format_expr = format_with(|f| match item {
Expr::BoolOp(expr) => expr.format().with_options(Some(parentheses)).fmt(f),
Expr::NamedExpr(expr) => expr.format().fmt(f),
Expr::BinOp(expr) => expr.format().with_options(Some(parentheses)).fmt(f),
Expr::UnaryOp(expr) => expr.format().fmt(f),
Expr::Lambda(expr) => expr.format().fmt(f),
Expr::IfExp(expr) => expr.format().fmt(f),
Expr::Dict(expr) => expr.format().fmt(f),
Expr::Set(expr) => expr.format().fmt(f),
Expr::ListComp(expr) => expr.format().fmt(f),
Expr::SetComp(expr) => expr.format().fmt(f),
Expr::DictComp(expr) => expr.format().fmt(f),
Expr::GeneratorExp(expr) => expr.format().fmt(f),
Expr::Await(expr) => expr.format().fmt(f),
Expr::Yield(expr) => expr.format().fmt(f),
Expr::YieldFrom(expr) => expr.format().fmt(f),
Expr::Compare(expr) => expr.format().with_options(Some(parentheses)).fmt(f),
Expr::Call(expr) => expr.format().fmt(f),
Expr::FormattedValue(expr) => expr.format().fmt(f),
Expr::JoinedStr(expr) => expr.format().fmt(f),
Expr::Constant(expr) => expr
.format()
.with_options(StringLayout::Default(Some(parentheses)))
.fmt(f),
Expr::Attribute(expr) => expr.format().fmt(f),
Expr::Subscript(expr) => expr.format().fmt(f),
Expr::Starred(expr) => expr.format().fmt(f),
Expr::Name(expr) => expr.format().fmt(f),
Expr::List(expr) => expr.format().fmt(f),
Expr::Tuple(expr) => expr
.format()
.with_options(TupleParentheses::Expr(parentheses))
.fmt(f),
Expr::Slice(expr) => expr.format().fmt(f),
});
let result = match parentheses {
Parentheses::Always => parenthesized("(", &format_expr, ")").fmt(f),
// Add optional parentheses. Ignore if the item renders parentheses itself.
Parentheses::Optional => {
if can_omit_optional_parentheses(item, f.context()) {
optional_parentheses(&format_expr).fmt(f)
} else {
parenthesize_if_expands(&format_expr).fmt(f)
}
}
Parentheses::Custom | Parentheses::Never => {
let saved_level = f.context().node_level();
let new_level = match saved_level {
NodeLevel::TopLevel | NodeLevel::CompoundStatement => {
NodeLevel::Expression(None)
}
level @ (NodeLevel::Expression(_) | NodeLevel::ParenthesizedExpression) => {
level
}
};
f.context_mut().set_node_level(new_level);
let result = Format::fmt(&format_expr, f);
f.context_mut().set_node_level(saved_level);
result
}
};
result
}
}
impl NeedsParentheses for Expr {
fn needs_parentheses(
&self,
parenthesize: Parenthesize,
context: &PyFormatContext,
) -> Parentheses {
match self {
Expr::BoolOp(expr) => expr.needs_parentheses(parenthesize, context),
Expr::NamedExpr(expr) => expr.needs_parentheses(parenthesize, context),
Expr::BinOp(expr) => expr.needs_parentheses(parenthesize, context),
Expr::UnaryOp(expr) => expr.needs_parentheses(parenthesize, context),
Expr::Lambda(expr) => expr.needs_parentheses(parenthesize, context),
Expr::IfExp(expr) => expr.needs_parentheses(parenthesize, context),
Expr::Dict(expr) => expr.needs_parentheses(parenthesize, context),
Expr::Set(expr) => expr.needs_parentheses(parenthesize, context),
Expr::ListComp(expr) => expr.needs_parentheses(parenthesize, context),
Expr::SetComp(expr) => expr.needs_parentheses(parenthesize, context),
Expr::DictComp(expr) => expr.needs_parentheses(parenthesize, context),
Expr::GeneratorExp(expr) => expr.needs_parentheses(parenthesize, context),
Expr::Await(expr) => expr.needs_parentheses(parenthesize, context),
Expr::Yield(expr) => expr.needs_parentheses(parenthesize, context),
Expr::YieldFrom(expr) => expr.needs_parentheses(parenthesize, context),
Expr::Compare(expr) => expr.needs_parentheses(parenthesize, context),
Expr::Call(expr) => expr.needs_parentheses(parenthesize, context),
Expr::FormattedValue(expr) => expr.needs_parentheses(parenthesize, context),
Expr::JoinedStr(expr) => expr.needs_parentheses(parenthesize, context),
Expr::Constant(expr) => expr.needs_parentheses(parenthesize, context),
Expr::Attribute(expr) => expr.needs_parentheses(parenthesize, context),
Expr::Subscript(expr) => expr.needs_parentheses(parenthesize, context),
Expr::Starred(expr) => expr.needs_parentheses(parenthesize, context),
Expr::Name(expr) => expr.needs_parentheses(parenthesize, context),
Expr::List(expr) => expr.needs_parentheses(parenthesize, context),
Expr::Tuple(expr) => expr.needs_parentheses(parenthesize, context),
Expr::Slice(expr) => expr.needs_parentheses(parenthesize, context),
}
}
}
impl<'ast> AsFormat<PyFormatContext<'ast>> for Expr {
type Format<'a> = FormatRefWithRule<'a, Expr, FormatExpr, PyFormatContext<'ast>>;
fn format(&self) -> Self::Format<'_> {
FormatRefWithRule::new(self, FormatExpr::default())
}
}
impl<'ast> IntoFormat<PyFormatContext<'ast>> for Expr {
type Format = FormatOwnedWithRule<Expr, FormatExpr, PyFormatContext<'ast>>;
fn into_format(self) -> Self::Format {
FormatOwnedWithRule::new(self, FormatExpr::default())
}
}
/// Tests if it is safe to omit the optional parentheses.
///
/// We prefer parentheses at least in the following cases:
/// * The expression contains more than one unparenthesized expression with the same priority. For example,
/// the expression `a * b * c` contains two multiply operations. We prefer parentheses in that case.
/// `(a * b) * c` or `a * b + c` are okay, because the subexpression is parenthesized, or the expression uses operands with a lower priority
/// * The expression contains at least one parenthesized sub expression (optimization to avoid unnecessary work)
///
/// This mimics Black's [`_maybe_split_omitting_optional_parens`](https://github.com/psf/black/blob/d1248ca9beaf0ba526d265f4108836d89cf551b7/src/black/linegen.py#L746-L820)
fn can_omit_optional_parentheses(expr: &Expr, context: &PyFormatContext) -> bool {
let mut visitor = CanOmitOptionalParenthesesVisitor::new(context.source());
visitor.visit_subexpression(expr);
visitor.can_omit()
}
#[derive(Clone, Debug)]
struct CanOmitOptionalParenthesesVisitor<'input> {
max_priority: OperatorPriority,
max_priority_count: u32,
any_parenthesized_expressions: bool,
last: Option<&'input Expr>,
first: Option<&'input Expr>,
source: &'input str,
}
impl<'input> CanOmitOptionalParenthesesVisitor<'input> {
fn new(source: &'input str) -> Self {
Self {
source,
max_priority: OperatorPriority::None,
max_priority_count: 0,
any_parenthesized_expressions: false,
last: None,
first: None,
}
}
fn update_max_priority(&mut self, current_priority: OperatorPriority) {
self.update_max_priority_with_count(current_priority, 1);
}
fn update_max_priority_with_count(&mut self, current_priority: OperatorPriority, count: u32) {
match self.max_priority.cmp(¤t_priority) {
Ordering::Less => {
self.max_priority_count = count;
self.max_priority = current_priority;
}
Ordering::Equal => {
self.max_priority_count += count;
}
Ordering::Greater => {}
}
}
// Visits a subexpression, ignoring whether it is parenthesized or not
fn visit_subexpression(&mut self, expr: &'input Expr) {
match expr {
Expr::Dict(_) | Expr::List(_) | Expr::Tuple(_) | Expr::Set(_) => {
self.any_parenthesized_expressions = true;
// The values are always parenthesized, don't visit.
return;
}
Expr::ListComp(_) | Expr::SetComp(_) | Expr::DictComp(_) => {
self.any_parenthesized_expressions = true;
self.update_max_priority(OperatorPriority::Comprehension);
return;
}
// It's impossible for a file smaller or equal to 4GB to contain more than 2^32 comparisons
// because each comparison requires a left operand, and `n` `operands` and right sides.
#[allow(clippy::cast_possible_truncation)]
Expr::BoolOp(ast::ExprBoolOp {
range: _,
op: _,
values,
}) => self.update_max_priority_with_count(
OperatorPriority::BooleanOperation,
values.len().saturating_sub(1) as u32,
),
Expr::BinOp(ast::ExprBinOp {
op,
left: _,
right: _,
range: _,
}) => self.update_max_priority(OperatorPriority::from(*op)),
Expr::IfExp(_) => {
// + 1 for the if and one for the else
self.update_max_priority_with_count(OperatorPriority::Conditional, 2);
}
// It's impossible for a file smaller or equal to 4GB to contain more than 2^32 comparisons
// because each comparison requires a left operand, and `n` `operands` and right sides.
#[allow(clippy::cast_possible_truncation)]
Expr::Compare(ast::ExprCompare {
range: _,
left: _,
ops,
comparators: _,
}) => {
self.update_max_priority_with_count(OperatorPriority::Comparator, ops.len() as u32);
}
Expr::Call(ast::ExprCall {
range: _,
func,
args: _,
keywords: _,
}) => {
self.any_parenthesized_expressions = true;
// Only walk the function, the arguments are always parenthesized
self.visit_expr(func);
self.last = Some(expr);
return;
}
Expr::Subscript(_) => {
// Don't walk the value. Splitting before the value looks weird.
// Don't walk the slice, because the slice is always parenthesized.
return;
}
Expr::UnaryOp(ast::ExprUnaryOp {
range: _,
op,
operand: _,
}) => {
if op.is_invert() {
self.update_max_priority(OperatorPriority::BitwiseInversion);
}
}
// `[a, b].test[300].dot`
Expr::Attribute(ast::ExprAttribute {
range: _,
value,
attr: _,
ctx: _,
}) => {
if has_parentheses(value, self.source) {
self.update_max_priority(OperatorPriority::Attribute);
}
}
Expr::NamedExpr(_)
| Expr::GeneratorExp(_)
| Expr::Lambda(_)
| Expr::Await(_)
| Expr::Yield(_)
| Expr::YieldFrom(_)
| Expr::FormattedValue(_)
| Expr::JoinedStr(_)
| Expr::Constant(_)
| Expr::Starred(_)
| Expr::Name(_)
| Expr::Slice(_) => {}
};
walk_expr(self, expr);
}
fn can_omit(self) -> bool {
if self.max_priority_count > 1 {
false
} else if self.max_priority == OperatorPriority::Attribute {
true
} else if !self.any_parenthesized_expressions {
// Only use the more complex IR when there is any expression that we can possibly split by
false
} else {
// Only use the layout if the first or last expression has parentheses of some sort.
let first_parenthesized = self
.first
.map_or(false, |first| has_parentheses(first, self.source));
let last_parenthesized = self
.last
.map_or(false, |last| has_parentheses(last, self.source));
first_parenthesized || last_parenthesized
}
}
}
impl<'input> PreorderVisitor<'input> for CanOmitOptionalParenthesesVisitor<'input> {
fn visit_expr(&mut self, expr: &'input Expr) {
self.last = Some(expr);
// Rule only applies for non-parenthesized expressions.
if is_expression_parenthesized(AnyNodeRef::from(expr), self.source) {
self.any_parenthesized_expressions = true;
} else {
self.visit_subexpression(expr);
}
if self.first.is_none() {
self.first = Some(expr);
}
}
}
fn has_parentheses(expr: &Expr, source: &str) -> bool {
matches!(
expr,
Expr::Dict(_)
| Expr::List(_)
| Expr::Tuple(_)
| Expr::Set(_)
| Expr::ListComp(_)
| Expr::SetComp(_)
| Expr::DictComp(_)
| Expr::Call(_)
| Expr::Subscript(_)
) || is_expression_parenthesized(AnyNodeRef::from(expr), source)
}
#[derive(Debug, Copy, Clone, Ord, PartialOrd, Eq, PartialEq)]
enum OperatorPriority {
None,
Attribute,
Comparator,
Exponential,
BitwiseInversion,
Multiplicative,
Additive,
Shift,
BitwiseAnd,
BitwiseOr,
BitwiseXor,
// TODO(micha)
#[allow(unused)]
String,
BooleanOperation,
Conditional,
Comprehension,
}
impl From<ast::Operator> for OperatorPriority {
fn from(value: Operator) -> Self {
match value {
Operator::Add | Operator::Sub => OperatorPriority::Additive,
Operator::Mult
| Operator::MatMult
| Operator::Div
| Operator::Mod
| Operator::FloorDiv => OperatorPriority::Multiplicative,
Operator::Pow => OperatorPriority::Exponential,
Operator::LShift | Operator::RShift => OperatorPriority::Shift,
Operator::BitOr => OperatorPriority::BitwiseOr,
Operator::BitXor => OperatorPriority::BitwiseXor,
Operator::BitAnd => OperatorPriority::BitwiseAnd,
}
}
}