/
stmt_class_def.rs
181 lines (164 loc) · 6.52 KB
/
stmt_class_def.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
use ruff_formatter::write;
use ruff_python_ast::{Decorator, StmtClassDef};
use ruff_python_trivia::lines_after_ignoring_trivia;
use ruff_text_size::Ranged;
use crate::comments::format::empty_lines_before_trailing_comments;
use crate::comments::{leading_comments, trailing_comments, SourceComment};
use crate::prelude::*;
use crate::statement::clause::{clause_body, clause_header, ClauseHeader};
use crate::statement::suite::SuiteKind;
#[derive(Default)]
pub struct FormatStmtClassDef;
impl FormatNodeRule<StmtClassDef> for FormatStmtClassDef {
fn fmt_fields(&self, item: &StmtClassDef, f: &mut PyFormatter) -> FormatResult<()> {
let StmtClassDef {
range: _,
name,
arguments,
body,
type_params,
decorator_list,
} = item;
let comments = f.context().comments().clone();
let dangling_comments = comments.dangling(item);
let trailing_definition_comments_start =
dangling_comments.partition_point(|comment| comment.line_position().is_own_line());
let (leading_definition_comments, trailing_definition_comments) =
dangling_comments.split_at(trailing_definition_comments_start);
write!(
f,
[
FormatDecorators {
decorators: decorator_list,
leading_definition_comments,
},
clause_header(
ClauseHeader::Class(item),
trailing_definition_comments,
&format_with(|f| {
write!(f, [token("class"), space(), name.format()])?;
if let Some(type_params) = type_params.as_deref() {
write!(f, [type_params.format()])?;
}
if let Some(arguments) = arguments.as_deref() {
// Drop empty the arguments node entirely (i.e., remove the parentheses) if it is empty,
// e.g., given:
// ```python
// class A():
// ...
// ```
//
// Format as:
// ```python
// class A:
// ...
// ```
//
// However, preserve any dangling end-of-line comments, e.g., given:
// ```python
// class A( # comment
// ):
// ...
//
// Format as:
// ```python
// class A: # comment
// ...
// ```
//
// However, the arguments contain any dangling own-line comments, we retain the
// parentheses, e.g., given:
// ```python
// class A( # comment
// # comment
// ):
// ...
// ```
//
// Format as:
// ```python
// class A( # comment
// # comment
// ):
// ...
// ```
if arguments.is_empty()
&& comments
.dangling(arguments)
.iter()
.all(|comment| comment.line_position().is_end_of_line())
{
let dangling = comments.dangling(arguments);
write!(f, [trailing_comments(dangling)])?;
} else {
write!(f, [arguments.format()])?;
}
}
Ok(())
}),
),
clause_body(body, trailing_definition_comments).with_kind(SuiteKind::Class),
]
)?;
// If the class contains trailing comments, insert newlines before them.
// For example, given:
// ```python
// class Class:
// ...
// # comment
// ```
//
// At the top-level in a non-stub file, reformat as:
// ```python
// class Class:
// ...
//
//
// # comment
// ```
empty_lines_before_trailing_comments(f, comments.trailing(item)).fmt(f)
}
fn fmt_dangling_comments(
&self,
_dangling_comments: &[SourceComment],
_f: &mut PyFormatter,
) -> FormatResult<()> {
// handled in fmt_fields
Ok(())
}
}
pub(super) struct FormatDecorators<'a> {
pub(super) decorators: &'a [Decorator],
pub(super) leading_definition_comments: &'a [SourceComment],
}
impl Format<PyFormatContext<'_>> for FormatDecorators<'_> {
fn fmt(&self, f: &mut Formatter<PyFormatContext<'_>>) -> FormatResult<()> {
if let Some(last_decorator) = self.decorators.last() {
f.join_with(hard_line_break())
.entries(self.decorators.iter().formatted())
.finish()?;
if self.leading_definition_comments.is_empty() {
write!(f, [hard_line_break()])?;
} else {
// Write any leading definition comments (between last decorator and the header)
// while maintaining the right amount of empty lines between the comment
// and the last decorator.
let leading_line =
if lines_after_ignoring_trivia(last_decorator.end(), f.context().source()) <= 1
{
hard_line_break()
} else {
empty_line()
};
write!(
f,
[
leading_line,
leading_comments(self.leading_definition_comments)
]
)?;
}
}
Ok(())
}
}