-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
comprehension.rs
170 lines (146 loc) · 6.06 KB
/
comprehension.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
use ruff_formatter::{format_args, write, Buffer, FormatResult};
use ruff_python_ast::{Comprehension, Expr};
use ruff_python_trivia::{find_only_token_in_range, SimpleTokenKind};
use ruff_text_size::{Ranged, TextRange};
use crate::comments::{leading_comments, trailing_comments};
use crate::expression::expr_tuple::TupleParentheses;
use crate::expression::parentheses::is_expression_parenthesized;
use crate::prelude::*;
use crate::preview::is_comprehension_leading_expression_comments_same_line_enabled;
#[derive(Default)]
pub struct FormatComprehension;
impl FormatNodeRule<Comprehension> for FormatComprehension {
fn fmt_fields(&self, item: &Comprehension, f: &mut PyFormatter) -> FormatResult<()> {
struct Spacer<'a> {
expression: &'a Expr,
preserve_parentheses: bool,
}
impl Format<PyFormatContext<'_>> for Spacer<'_> {
fn fmt(&self, f: &mut PyFormatter) -> FormatResult<()> {
let has_leading_comments = f.context().comments().has_leading(self.expression);
// Don't add a soft line break for parenthesized expressions with a leading comment.
// The comments are rendered **inside** the parentheses and adding a softline break
// unnecessarily forces the parentheses to be on their own line.
// ```python
// y = [
// ...
// if
// (
// # See how the `(` gets forced on its own line? We don't want that.
// ...
// )
// ]
// ```
let will_be_parenthesized =
is_comprehension_leading_expression_comments_same_line_enabled(f.context())
&& self.preserve_parentheses
&& is_expression_parenthesized(
self.expression.into(),
f.context().comments().ranges(),
f.context().source(),
);
if has_leading_comments && !will_be_parenthesized {
soft_line_break_or_space().fmt(f)
} else {
space().fmt(f)
}
}
}
let Comprehension {
range: _,
target,
iter,
ifs,
is_async,
} = item;
if *is_async {
write!(f, [token("async"), space()])?;
}
let comments = f.context().comments().clone();
let dangling_item_comments = comments.dangling(item);
let (before_target_comments, dangling_comments) = dangling_item_comments.split_at(
dangling_item_comments.partition_point(|comment| comment.end() < target.start()),
);
let in_token = find_only_token_in_range(
TextRange::new(target.end(), iter.start()),
SimpleTokenKind::In,
f.context().source(),
);
let (before_in_comments, dangling_comments) = dangling_comments.split_at(
dangling_comments.partition_point(|comment| comment.end() < in_token.start()),
);
let (trailing_in_comments, dangling_if_comments) = dangling_comments
.split_at(dangling_comments.partition_point(|comment| comment.start() < iter.start()));
let in_spacer = format_with(|f| {
if before_in_comments.is_empty() {
space().fmt(f)
} else {
soft_line_break_or_space().fmt(f)
}
});
write!(
f,
[
token("for"),
trailing_comments(before_target_comments),
Spacer {
expression: target,
preserve_parentheses: !target.is_tuple_expr()
},
ExprTupleWithoutParentheses(target),
in_spacer,
leading_comments(before_in_comments),
token("in"),
trailing_comments(trailing_in_comments),
Spacer {
expression: iter,
preserve_parentheses: true
},
iter.format(),
]
)?;
if !ifs.is_empty() {
let joined = format_with(|f| {
let mut joiner = f.join_with(soft_line_break_or_space());
let mut dangling_if_comments = dangling_if_comments;
for if_case in ifs {
let (if_comments, rest) = dangling_if_comments.split_at(
dangling_if_comments
.partition_point(|comment| comment.start() < if_case.start()),
);
let (own_line_if_comments, end_of_line_if_comments) = if_comments.split_at(
if_comments
.partition_point(|comment| comment.line_position().is_own_line()),
);
joiner.entry(&format_args!(
leading_comments(own_line_if_comments),
token("if"),
trailing_comments(end_of_line_if_comments),
Spacer {
expression: if_case,
preserve_parentheses: true
},
if_case.format(),
));
dangling_if_comments = rest;
}
debug_assert!(dangling_if_comments.is_empty());
joiner.finish()
});
write!(f, [soft_line_break_or_space(), joined])?;
}
Ok(())
}
}
struct ExprTupleWithoutParentheses<'a>(&'a Expr);
impl Format<PyFormatContext<'_>> for ExprTupleWithoutParentheses<'_> {
fn fmt(&self, f: &mut PyFormatter) -> FormatResult<()> {
match self.0 {
Expr::Tuple(expr_tuple) => expr_tuple
.format()
.with_options(TupleParentheses::Never)
.fmt(f),
other => other.format().fmt(f),
}
}
}