-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
stmt_function_def.rs
134 lines (110 loc) · 4.21 KB
/
stmt_function_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
use ruff_python_ast::{Ranged, StmtFunctionDef};
use ruff_formatter::{write, FormatOwnedWithRule, FormatRefWithRule};
use ruff_python_ast::function::AnyFunctionDefinition;
use ruff_python_trivia::lines_after_ignoring_trivia;
use crate::comments::{leading_comments, trailing_comments};
use crate::expression::parentheses::{optional_parentheses, Parentheses};
use crate::prelude::*;
use crate::statement::suite::SuiteKind;
use crate::FormatNodeRule;
#[derive(Default)]
pub struct FormatStmtFunctionDef;
impl FormatNodeRule<StmtFunctionDef> for FormatStmtFunctionDef {
fn fmt_fields(&self, item: &StmtFunctionDef, f: &mut PyFormatter) -> FormatResult<()> {
AnyFunctionDefinition::from(item).format().fmt(f)
}
fn fmt_dangling_comments(
&self,
_node: &StmtFunctionDef,
_f: &mut PyFormatter,
) -> FormatResult<()> {
// Handled by `AnyFunctionDef`
Ok(())
}
}
#[derive(Default)]
pub struct FormatAnyFunctionDef;
impl FormatRule<AnyFunctionDefinition<'_>, PyFormatContext<'_>> for FormatAnyFunctionDef {
fn fmt(&self, item: &AnyFunctionDefinition<'_>, f: &mut PyFormatter) -> FormatResult<()> {
let comments = f.context().comments().clone();
let dangling_comments = comments.dangling_comments(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);
if let Some(last_decorator) = item.decorators().last() {
f.join_with(hard_line_break())
.entries(item.decorators().iter().formatted())
.finish()?;
if 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(leading_definition_comments)]
)?;
}
}
if item.is_async() {
write!(f, [text("async"), space()])?;
}
let name = item.name();
write!(f, [text("def"), space(), name.format()])?;
if let Some(type_params) = item.type_params() {
write!(f, [type_params.format()])?;
}
write!(f, [item.arguments().format()])?;
if let Some(return_annotation) = item.returns() {
write!(
f,
[
space(),
text("->"),
space(),
optional_parentheses(
&return_annotation.format().with_options(Parentheses::Never)
)
]
)?;
}
write!(
f,
[
text(":"),
trailing_comments(trailing_definition_comments),
block_indent(&item.body().format().with_options(SuiteKind::Function))
]
)
}
}
impl<'def, 'ast> AsFormat<PyFormatContext<'ast>> for AnyFunctionDefinition<'def> {
type Format<'a> = FormatRefWithRule<
'a,
AnyFunctionDefinition<'def>,
FormatAnyFunctionDef,
PyFormatContext<'ast>,
> where Self: 'a;
fn format(&self) -> Self::Format<'_> {
FormatRefWithRule::new(self, FormatAnyFunctionDef)
}
}
impl<'def, 'ast> IntoFormat<PyFormatContext<'ast>> for AnyFunctionDefinition<'def> {
type Format = FormatOwnedWithRule<
AnyFunctionDefinition<'def>,
FormatAnyFunctionDef,
PyFormatContext<'ast>,
>;
fn into_format(self) -> Self::Format {
FormatOwnedWithRule::new(self, FormatAnyFunctionDef)
}
}