-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathexpr_await.rs
More file actions
54 lines (49 loc) · 1.5 KB
/
expr_await.rs
File metadata and controls
54 lines (49 loc) · 1.5 KB
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
use ruff_formatter::write;
use ruff_python_ast::AnyNodeRef;
use ruff_python_ast::ExprAwait;
use ruff_text_size::Ranged;
use crate::expression::maybe_parenthesize_expression;
use crate::expression::parentheses::{
NeedsParentheses, OptionalParentheses, Parenthesize, is_expression_parenthesized,
is_type_annotation_of,
};
use crate::prelude::*;
#[derive(Default)]
pub struct FormatExprAwait;
impl FormatNodeRule<ExprAwait> for FormatExprAwait {
fn fmt_fields(&self, item: &ExprAwait, f: &mut PyFormatter) -> FormatResult<()> {
let ExprAwait {
range: _,
node_index: _,
value,
} = item;
write!(
f,
[
token("await"),
space(),
maybe_parenthesize_expression(value, item, Parenthesize::IfBreaks)
]
)
}
}
impl NeedsParentheses for ExprAwait {
fn needs_parentheses(
&self,
parent: AnyNodeRef,
context: &PyFormatContext,
) -> OptionalParentheses {
if parent.is_expr_await() || is_type_annotation_of(self.range(), parent) {
OptionalParentheses::Always
} else if is_expression_parenthesized(
self.value.as_ref().into(),
context.comments().ranges(),
context.source(),
) {
// Prefer splitting the value if it is parenthesized.
OptionalParentheses::Never
} else {
self.value.needs_parentheses(self.into(), context)
}
}
}