Rename Comments methods (#6649)

This commit is contained in:
Micha Reiser 2023-08-18 08:37:01 +02:00 committed by GitHub
parent 3ceb6fbeb0
commit 0cea4975fc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
44 changed files with 128 additions and 172 deletions

View file

@ -63,7 +63,7 @@ impl Format<PyFormatContext<'_>> for FormatTargets<'_> {
if let Some((first, rest)) = self.targets.split_first() {
let comments = f.context().comments();
let parenthesize = if comments.has_leading_comments(first) {
let parenthesize = if comments.has_leading(first) {
ParenthesizeTarget::Always
} else if has_own_parentheses(first, f.context()).is_some() {
ParenthesizeTarget::Never

View file

@ -25,7 +25,7 @@ impl FormatNodeRule<StmtClassDef> for FormatStmtClassDef {
let comments = f.context().comments().clone();
let dangling_comments = comments.dangling_comments(item);
let dangling_comments = comments.dangling(item);
let trailing_definition_comments_start =
dangling_comments.partition_point(|comment| comment.line_position().is_own_line());
@ -93,11 +93,11 @@ impl FormatNodeRule<StmtClassDef> for FormatStmtClassDef {
// ```
if arguments.is_empty()
&& comments
.dangling_comments(arguments)
.dangling(arguments)
.iter()
.all(|comment| comment.line_position().is_end_of_line())
{
let dangling = comments.dangling_comments(arguments);
let dangling = comments.dangling(arguments);
write!(f, [trailing_comments(dangling)])?;
} else {
write!(f, [arguments.format()])?;

View file

@ -39,7 +39,7 @@ impl FormatNodeRule<StmtFor> for FormatStmtFor {
} = item;
let comments = f.context().comments().clone();
let dangling_comments = comments.dangling_comments(item);
let dangling_comments = comments.dangling(item);
let body_start = body.first().map_or(iter.end(), Stmt::start);
let or_else_comments_start =
dangling_comments.partition_point(|comment| comment.slice().end() < body_start);

View file

@ -29,7 +29,7 @@ impl FormatNodeRule<StmtFunctionDef> for FormatStmtFunctionDef {
let comments = f.context().comments().clone();
let dangling_comments = comments.dangling_comments(item);
let dangling_comments = comments.dangling(item);
let trailing_definition_comments_start =
dangling_comments.partition_point(|comment| comment.line_position().is_own_line());
@ -64,19 +64,17 @@ impl FormatNodeRule<StmtFunctionDef> for FormatStmtFunctionDef {
write!(f, [space(), text("->"), space()])?;
if return_annotation.is_tuple_expr() {
let parentheses = if comments
.has_leading_comments(return_annotation.as_ref())
{
Parentheses::Always
} else {
Parentheses::Never
};
let parentheses =
if comments.has_leading(return_annotation.as_ref()) {
Parentheses::Always
} else {
Parentheses::Never
};
write!(
f,
[return_annotation.format().with_options(parentheses)]
)?;
} else if comments.has_trailing_comments(return_annotation.as_ref())
{
} else if comments.has_trailing(return_annotation.as_ref()) {
// Intentionally parenthesize any return annotations with trailing comments.
// This avoids an instability in cases like:
// ```python

View file

@ -14,10 +14,7 @@ impl FormatNodeRule<StmtGlobal> for FormatStmtGlobal {
// Join the `global` names, breaking across continuation lines if necessary, unless the
// `global` statement has a trailing comment, in which case, breaking the names would
// move the comment "off" of the `global` statement.
if f.context()
.comments()
.has_trailing_comments(item.as_any_node_ref())
{
if f.context().comments().has_trailing(item.as_any_node_ref()) {
let joined = format_with(|f| {
f.join_with(format_args![text(","), space()])
.entries(item.names.iter().formatted())

View file

@ -22,7 +22,7 @@ impl FormatNodeRule<StmtIf> for FormatStmtIf {
} = item;
let comments = f.context().comments().clone();
let trailing_colon_comment = comments.dangling_comments(item);
let trailing_colon_comment = comments.dangling(item);
write!(
f,
@ -73,8 +73,8 @@ pub(crate) fn format_elif_else_clause(
} = item;
let comments = f.context().comments().clone();
let trailing_colon_comment = comments.dangling_comments(item);
let leading_comments = comments.leading_comments(item);
let trailing_colon_comment = comments.dangling(item);
let leading_comments = comments.leading(item);
write!(
f,

View file

@ -59,7 +59,7 @@ impl FormatNodeRule<StmtImportFrom> for FormatStmtImportFrom {
// )
// ```
let comments = f.context().comments().clone();
let parenthesized_comments = comments.dangling_comments(item.as_any_node_ref());
let parenthesized_comments = comments.dangling(item.as_any_node_ref());
if parenthesized_comments.is_empty() {
parenthesize_if_expands(&names).fmt(f)

View file

@ -21,7 +21,7 @@ impl FormatNodeRule<StmtMatch> for FormatStmtMatch {
} = item;
let comments = f.context().comments().clone();
let dangling_item_comments = comments.dangling_comments(item);
let dangling_item_comments = comments.dangling(item);
// There can be at most one dangling comment after the colon in a match statement.
debug_assert!(dangling_item_comments.len() <= 1);
@ -53,7 +53,7 @@ impl FormatNodeRule<StmtMatch> for FormatStmtMatch {
f,
[block_indent(&format_args!(
leading_alternate_branch_comments(
comments.leading_comments(case),
comments.leading(case),
last_case.body.last(),
),
case.format()

View file

@ -14,10 +14,7 @@ impl FormatNodeRule<StmtNonlocal> for FormatStmtNonlocal {
// Join the `nonlocal` names, breaking across continuation lines if necessary, unless the
// `nonlocal` statement has a trailing comment, in which case, breaking the names would
// move the comment "off" of the `nonlocal` statement.
if f.context()
.comments()
.has_trailing_comments(item.as_any_node_ref())
{
if f.context().comments().has_trailing(item.as_any_node_ref()) {
let joined = format_with(|f| {
f.join_with(format_args![text(","), space()])
.entries(item.names.iter().formatted())

View file

@ -63,13 +63,13 @@ impl FormatNodeRule<StmtTry> for FormatStmtTry {
} = item;
let comments_info = f.context().comments().clone();
let mut dangling_comments = comments_info.dangling_comments(item);
let mut dangling_comments = comments_info.dangling(item);
(_, dangling_comments) = format_case(item, CaseKind::Try, None, dangling_comments, f)?;
let mut previous_node = body.last();
for handler in handlers {
let handler_comments = comments_info.leading_comments(handler);
let handler_comments = comments_info.leading(handler);
write!(
f,
[

View file

@ -22,7 +22,7 @@ impl FormatNodeRule<StmtWhile> for FormatStmtWhile {
} = item;
let comments = f.context().comments().clone();
let dangling_comments = comments.dangling_comments(item.as_any_node_ref());
let dangling_comments = comments.dangling(item.as_any_node_ref());
let body_start = body.first().map_or(test.end(), Stmt::start);
let or_else_comments_start =

View file

@ -33,7 +33,7 @@ impl FormatNodeRule<StmtWith> for FormatStmtWith {
// ...
// ```
let comments = f.context().comments().clone();
let dangling_comments = comments.dangling_comments(item.as_any_node_ref());
let dangling_comments = comments.dangling(item.as_any_node_ref());
let partition_point = dangling_comments.partition_point(|comment| {
item.items
.first()
@ -86,9 +86,7 @@ impl FormatNodeRule<StmtWith> for FormatStmtWith {
} else if let [item] = item.items.as_slice() {
// This is similar to `maybe_parenthesize_expression`, but we're not dealing with an
// expression here, it's a `WithItem`.
if comments.has_leading_comments(item)
|| comments.has_trailing_own_line_comments(item)
{
if comments.has_leading(item) || comments.has_trailing_own_line(item) {
optional_parentheses(&item.format()).fmt(f)?;
} else {
item.format().fmt(f)?;

View file

@ -70,9 +70,7 @@ impl FormatRule<Suite, PyFormatContext<'_>> for FormatSuite {
// Format the first statement in the body, which often has special formatting rules.
let first = match self.kind {
SuiteKind::Other => {
if is_class_or_function_definition(first)
&& !comments.has_leading_comments(first)
{
if is_class_or_function_definition(first) && !comments.has_leading(first) {
// Add an empty line for any nested functions or classes defined within
// non-function or class compound statements, e.g., this is stable formatting:
// ```python
@ -97,7 +95,7 @@ impl FormatRule<Suite, PyFormatContext<'_>> for FormatSuite {
SuiteKind::Class => {
if let Some(docstring) = DocstringStmt::try_from_statement(first) {
if !comments.has_leading_comments(first)
if !comments.has_leading(first)
&& lines_before(first.start(), source) > 1
{
// Allow up to one empty line before a class docstring, e.g., this is
@ -118,7 +116,7 @@ impl FormatRule<Suite, PyFormatContext<'_>> for FormatSuite {
SuiteKind::TopLevel => SuiteChildStatement::Other(first),
};
let first_comments = comments.leading_dangling_trailing_comments(first);
let first_comments = comments.leading_dangling_trailing(first);
let (mut preceding, mut after_class_docstring) = if first_comments
.leading
@ -158,8 +156,8 @@ impl FormatRule<Suite, PyFormatContext<'_>> for FormatSuite {
match self.kind {
SuiteKind::TopLevel if source_type.is_stub() => {
// Preserve the empty line if the definitions are separated by a comment
if comments.has_trailing_comments(preceding)
|| comments.has_leading_comments(following)
if comments.has_trailing(preceding)
|| comments.has_leading(following)
{
empty_line().fmt(f)?;
} else {
@ -220,8 +218,7 @@ impl FormatRule<Suite, PyFormatContext<'_>> for FormatSuite {
// which is 0 instead of 1, the number of lines between the trailing comment and
// the leading comment. This is why the suite handling counts the lines before the
// start of the next statement or before the first leading comments for compound statements.
let start = if let Some(first_leading) =
comments.leading_comments(following).first()
let start = if let Some(first_leading) = comments.leading(following).first()
{
first_leading.slice().start()
} else {
@ -294,7 +291,7 @@ impl FormatRule<Suite, PyFormatContext<'_>> for FormatSuite {
}
}
let following_comments = comments.leading_dangling_trailing_comments(following);
let following_comments = comments.leading_dangling_trailing(following);
if following_comments
.leading
@ -401,7 +398,7 @@ impl<'a> DocstringStmt<'a> {
impl Format<PyFormatContext<'_>> for DocstringStmt<'_> {
fn fmt(&self, f: &mut Formatter<PyFormatContext<'_>>) -> FormatResult<()> {
let comments = f.context().comments().clone();
let node_comments = comments.leading_dangling_trailing_comments(self.0);
let node_comments = comments.leading_dangling_trailing(self.0);
if FormatStmtExpr.is_suppressed(node_comments.trailing, f.context()) {
suppressed_node(self.0).fmt(f)