Skip to content

Commit

Permalink
Rename Comments methods (#6649)
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaReiser committed Aug 18, 2023
1 parent 3ceb6fb commit 0cea497
Show file tree
Hide file tree
Showing 44 changed files with 128 additions and 172 deletions.
6 changes: 3 additions & 3 deletions crates/ruff_python_formatter/src/comments/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ impl Format<PyFormatContext<'_>> for FormatLeadingComments<'_> {
let comments = f.context().comments().clone();

let leading_comments = match self {
FormatLeadingComments::Node(node) => comments.leading_comments(*node),
FormatLeadingComments::Node(node) => comments.leading(*node),
FormatLeadingComments::Comments(comments) => comments,
};

Expand Down Expand Up @@ -124,7 +124,7 @@ impl Format<PyFormatContext<'_>> for FormatTrailingComments<'_> {
let comments = f.context().comments().clone();

let trailing_comments = match self {
FormatTrailingComments::Node(node) => comments.trailing_comments(*node),
FormatTrailingComments::Node(node) => comments.trailing(*node),
FormatTrailingComments::Comments(comments) => comments,
};

Expand Down Expand Up @@ -198,7 +198,7 @@ impl Format<PyFormatContext<'_>> for FormatDanglingComments<'_> {

let dangling_comments = match self {
Self::Comments(comments) => comments,
Self::Node(node) => comments.dangling_comments(*node),
Self::Node(node) => comments.dangling(*node),
};

let mut first = true;
Expand Down
64 changes: 19 additions & 45 deletions crates/ruff_python_formatter/src/comments/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -332,16 +332,16 @@ impl<'a> Comments<'a> {

/// Returns `true` if the given `node` has any [leading comments](self#leading-comments).
#[inline]
pub(crate) fn has_leading_comments<T>(&self, node: T) -> bool
pub(crate) fn has_leading<T>(&self, node: T) -> bool
where
T: Into<AnyNodeRef<'a>>,
{
!self.leading_comments(node).is_empty()
!self.leading(node).is_empty()
}

/// Returns the `node`'s [leading comments](self#leading-comments).
#[inline]
pub(crate) fn leading_comments<T>(&self, node: T) -> &[SourceComment]
pub(crate) fn leading<T>(&self, node: T) -> &[SourceComment]
where
T: Into<AnyNodeRef<'a>>,
{
Expand All @@ -351,15 +351,15 @@ impl<'a> Comments<'a> {
}

/// Returns `true` if node has any [dangling comments](self#dangling-comments).
pub(crate) fn has_dangling_comments<T>(&self, node: T) -> bool
pub(crate) fn has_dangling<T>(&self, node: T) -> bool
where
T: Into<AnyNodeRef<'a>>,
{
!self.dangling_comments(node).is_empty()
!self.dangling(node).is_empty()
}

/// Returns the [dangling comments](self#dangling-comments) of `node`
pub(crate) fn dangling_comments<T>(&self, node: T) -> &[SourceComment]
pub(crate) fn dangling<T>(&self, node: T) -> &[SourceComment]
where
T: Into<AnyNodeRef<'a>>,
{
Expand All @@ -370,7 +370,7 @@ impl<'a> Comments<'a> {

/// Returns the `node`'s [trailing comments](self#trailing-comments).
#[inline]
pub(crate) fn trailing_comments<T>(&self, node: T) -> &[SourceComment]
pub(crate) fn trailing<T>(&self, node: T) -> &[SourceComment]
where
T: Into<AnyNodeRef<'a>>,
{
Expand All @@ -381,43 +381,35 @@ impl<'a> Comments<'a> {

/// Returns `true` if the given `node` has any [trailing comments](self#trailing-comments).
#[inline]
pub(crate) fn has_trailing_comments<T>(&self, node: T) -> bool
pub(crate) fn has_trailing<T>(&self, node: T) -> bool
where
T: Into<AnyNodeRef<'a>>,
{
!self.trailing_comments(node).is_empty()
!self.trailing(node).is_empty()
}

/// Returns `true` if the given `node` has any [trailing own line comments](self#trailing-comments).
#[inline]
pub(crate) fn has_trailing_own_line_comments<T>(&self, node: T) -> bool
pub(crate) fn has_trailing_own_line<T>(&self, node: T) -> bool
where
T: Into<AnyNodeRef<'a>>,
{
self.trailing_comments(node)
self.trailing(node)
.iter()
.any(|comment| comment.line_position().is_own_line())
}

/// Returns an iterator over the [leading](self#leading-comments) and [trailing comments](self#trailing-comments) of `node`.
pub(crate) fn leading_trailing_comments<T>(
&self,
node: T,
) -> impl Iterator<Item = &SourceComment>
pub(crate) fn leading_trailing<T>(&self, node: T) -> impl Iterator<Item = &SourceComment>
where
T: Into<AnyNodeRef<'a>>,
{
let node = node.into();
self.leading_comments(node)
.iter()
.chain(self.trailing_comments(node).iter())
let comments = self.leading_dangling_trailing(node);
comments.leading.iter().chain(comments.trailing)
}

/// Returns an iterator over the [leading](self#leading-comments), [dangling](self#dangling-comments), and [trailing](self#trailing) comments of `node`.
pub(crate) fn leading_dangling_trailing_comments<T>(
&self,
node: T,
) -> LeadingDanglingTrailingComments
pub(crate) fn leading_dangling_trailing<T>(&self, node: T) -> LeadingDanglingTrailingComments
where
T: Into<AnyNodeRef<'a>>,
{
Expand All @@ -426,30 +418,12 @@ impl<'a> Comments<'a> {
.leading_dangling_trailing(&NodeRefEqualityKey::from_ref(node.into()))
}

/// Returns any comments on the open parenthesis of a `node`.
///
/// For example, `# comment` in:
/// ```python
/// ( # comment
/// foo.bar
/// )
/// ```
#[inline]
pub(crate) fn open_parenthesis_comment<T>(&self, node: T) -> Option<&SourceComment>
where
T: Into<AnyNodeRef<'a>>,
{
self.leading_comments(node)
.first()
.filter(|comment| comment.line_position.is_end_of_line())
}

#[inline(always)]
#[cfg(not(debug_assertions))]
pub(crate) fn assert_formatted_all_comments(&self, _source_code: SourceCode) {}
pub(crate) fn assert_all_formatted(&self, _source_code: SourceCode) {}

#[cfg(debug_assertions)]
pub(crate) fn assert_formatted_all_comments(&self, source_code: SourceCode) {
pub(crate) fn assert_all_formatted(&self, source_code: SourceCode) {
use std::fmt::Write;

let mut output = String::new();
Expand Down Expand Up @@ -481,7 +455,7 @@ impl<'a> Comments<'a> {
/// normally if `node` is the first or last node of a suppression range.
#[cfg(debug_assertions)]
pub(crate) fn mark_verbatim_node_comments_formatted(&self, node: AnyNodeRef) {
for dangling in self.dangling_comments(node) {
for dangling in self.dangling(node) {
dangling.mark_formatted();
}

Expand All @@ -505,7 +479,7 @@ struct MarkVerbatimCommentsAsFormattedVisitor<'a>(&'a Comments<'a>);

impl<'a> PreorderVisitor<'a> for MarkVerbatimCommentsAsFormattedVisitor<'a> {
fn enter_node(&mut self, node: AnyNodeRef<'a>) -> TraversalSignal {
for comment in self.0.leading_dangling_trailing_comments(node) {
for comment in self.0.leading_dangling_trailing(node) {
comment.mark_formatted();
}

Expand Down
8 changes: 4 additions & 4 deletions crates/ruff_python_formatter/src/expression/expr_attribute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ impl FormatNodeRule<ExprAttribute> for FormatExprAttribute {
);

let comments = f.context().comments().clone();
let dangling_comments = comments.dangling_comments(item);
let dangling_comments = comments.dangling(item);
let leading_attribute_comments_start = dangling_comments
.partition_point(|comment| comment.line_position().is_end_of_line());
let (trailing_dot_comments, leading_attribute_comments) =
Expand Down Expand Up @@ -88,7 +88,7 @@ impl FormatNodeRule<ExprAttribute> for FormatExprAttribute {
value.format().fmt(f)?;
}

if comments.has_trailing_own_line_comments(value.as_ref()) {
if comments.has_trailing_own_line(value.as_ref()) {
hard_line_break().fmt(f)?;
}

Expand Down Expand Up @@ -171,10 +171,10 @@ impl NeedsParentheses for ExprAttribute {
OptionalParentheses::Multiline
} else if context
.comments()
.dangling_comments(self)
.dangling(self)
.iter()
.any(|comment| comment.line_position().is_own_line())
|| context.comments().has_trailing_own_line_comments(self)
|| context.comments().has_trailing_own_line(self)
{
OptionalParentheses::Always
} else {
Expand Down
15 changes: 5 additions & 10 deletions crates/ruff_python_formatter/src/expression/expr_bin_op.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,7 @@ impl FormatNodeRule<ExprBinOp> for FormatExprBinOp {

match Self::layout(item, f.context()) {
BinOpLayout::LeftString(expression) => {
let right_has_leading_comment = f
.context()
.comments()
.has_leading_comments(item.right.as_ref());
let right_has_leading_comment = comments.has_leading(item.right.as_ref());

let format_right_and_op = format_with(|f| {
if right_has_leading_comment {
Expand Down Expand Up @@ -98,7 +95,7 @@ impl FormatNodeRule<ExprBinOp> for FormatExprBinOp {
right,
} = current;

let operator_comments = comments.dangling_comments(current);
let operator_comments = comments.dangling(current);
let needs_space = !is_simple_power_expression(current);

let before_operator_space = if needs_space {
Expand All @@ -117,9 +114,7 @@ impl FormatNodeRule<ExprBinOp> for FormatExprBinOp {
)?;

// Format the operator on its own line if the right side has any leading comments.
if comments.has_leading_comments(right.as_ref())
|| !operator_comments.is_empty()
{
if comments.has_leading(right.as_ref()) || !operator_comments.is_empty() {
hard_line_break().fmt(f)?;
} else if needs_space {
space().fmt(f)?;
Expand Down Expand Up @@ -171,8 +166,8 @@ impl FormatExprBinOp {

if bin_op.op == Operator::Mod
&& context.node_level().is_parenthesized()
&& !comments.has_dangling_comments(constant)
&& !comments.has_dangling_comments(bin_op)
&& !comments.has_dangling(constant)
&& !comments.has_dangling(bin_op)
{
BinOpLayout::LeftString(constant)
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ impl FormatNodeRule<ExprBoolOp> for FormatExprBoolOp {
FormatValue { value: first }.fmt(f)?;

for value in values {
let leading_value_comments = comments.leading_comments(value);
let leading_value_comments = comments.leading(value);
// Format the expressions leading comments **before** the operator
if leading_value_comments.is_empty() {
write!(f, [in_parentheses_only_soft_line_break_or_space()])?;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ impl FormatNodeRule<ExprCompare> for FormatExprCompare {
assert_eq!(comparators.len(), ops.len());

for (operator, comparator) in ops.iter().zip(comparators) {
let leading_comparator_comments = comments.leading_comments(comparator);
let leading_comparator_comments = comments.leading(comparator);
if leading_comparator_comments.is_empty() {
write!(f, [in_parentheses_only_soft_line_break_or_space()])?;
} else {
Expand Down
4 changes: 2 additions & 2 deletions crates/ruff_python_formatter/src/expression/expr_dict.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ impl Format<PyFormatContext<'_>> for KeyValuePair<'_> {
)
} else {
let comments = f.context().comments().clone();
let leading_value_comments = comments.leading_comments(self.value);
let leading_value_comments = comments.leading(self.value);
write!(
f,
[
Expand All @@ -67,7 +67,7 @@ impl FormatNodeRule<ExprDict> for FormatExprDict {
debug_assert_eq!(keys.len(), values.len());

let comments = f.context().comments().clone();
let dangling = comments.dangling_comments(item);
let dangling = comments.dangling(item);

if values.is_empty() {
return empty_parenthesized("{", dangling, "}").fmt(f);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ impl FormatNodeRule<ExprDictComp> for FormatExprDictComp {
});

let comments = f.context().comments().clone();
let dangling = comments.dangling_comments(item);
let dangling = comments.dangling(item);

write!(
f,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ impl FormatNodeRule<ExprGeneratorExp> for FormatExprGeneratorExp {
});

let comments = f.context().comments().clone();
let dangling = comments.dangling_comments(item);
let dangling = comments.dangling(item);

if self.parentheses == GeneratorExpParentheses::StripIfOnlyFunctionArg
&& dangling.is_empty()
Expand Down
4 changes: 2 additions & 2 deletions crates/ruff_python_formatter/src/expression/expr_if_exp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ impl FormatNodeRule<ExprIfExp> for FormatExprIfExp {
[in_parentheses_only_group(&format_args![
body.format(),
in_parentheses_only_soft_line_break_or_space(),
leading_comments(comments.leading_comments(test.as_ref())),
leading_comments(comments.leading(test.as_ref())),
text("if"),
space(),
test.format(),
in_parentheses_only_soft_line_break_or_space(),
leading_comments(comments.leading_comments(orelse.as_ref())),
leading_comments(comments.leading(orelse.as_ref())),
text("else"),
space(),
orelse.format()
Expand Down
2 changes: 1 addition & 1 deletion crates/ruff_python_formatter/src/expression/expr_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ impl FormatNodeRule<ExprList> for FormatExprList {
} = item;

let comments = f.context().comments().clone();
let dangling = comments.dangling_comments(item);
let dangling = comments.dangling(item);

if elts.is_empty() {
return empty_parenthesized("[", dangling, "]").fmt(f);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ impl FormatNodeRule<ExprListComp> for FormatExprListComp {
});

let comments = f.context().comments().clone();
let dangling = comments.dangling_comments(item);
let dangling = comments.dangling(item);

write!(
f,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ impl FormatNodeRule<ExprNamedExpr> for FormatExprNamedExpr {

// This context, a dangling comment is an end-of-line comment on the same line as the `:=`.
let comments = f.context().comments().clone();
let dangling = comments.dangling_comments(item);
let dangling = comments.dangling(item);

write!(
f,
Expand Down
2 changes: 1 addition & 1 deletion crates/ruff_python_formatter/src/expression/expr_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ impl FormatNodeRule<ExprSet> for FormatExprSet {
});

let comments = f.context().comments().clone();
let dangling = comments.dangling_comments(item);
let dangling = comments.dangling(item);

parenthesized("{", &joined, "}")
.with_dangling_comments(dangling)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ impl FormatNodeRule<ExprSetComp> for FormatExprSetComp {
});

let comments = f.context().comments().clone();
let dangling = comments.dangling_comments(item);
let dangling = comments.dangling(item);

write!(
f,
Expand Down
6 changes: 3 additions & 3 deletions crates/ruff_python_formatter/src/expression/expr_slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ impl FormatNodeRule<ExprSlice> for FormatExprSlice {
// to handle newlines and spacing, or the node is None and we insert the corresponding
// slice of dangling comments
let comments = f.context().comments().clone();
let slice_dangling_comments = comments.dangling_comments(item.as_any_node_ref());
let slice_dangling_comments = comments.dangling(item.as_any_node_ref());
// Put the dangling comments (where the nodes are missing) into buckets
let first_colon_partition_index =
slice_dangling_comments.partition_point(|x| x.slice().start() < first_colon.start());
Expand Down Expand Up @@ -102,7 +102,7 @@ impl FormatNodeRule<ExprSlice> for FormatExprSlice {

// Upper
if let Some(upper) = upper {
let upper_leading_comments = comments.leading_comments(upper.as_ref());
let upper_leading_comments = comments.leading(upper.as_ref());
leading_comments_spacing(f, upper_leading_comments)?;
write!(f, [upper.format(), line_suffix_boundary()])?;
} else {
Expand Down Expand Up @@ -134,7 +134,7 @@ impl FormatNodeRule<ExprSlice> for FormatExprSlice {
space().fmt(f)?;
}
if let Some(step) = step {
let step_leading_comments = comments.leading_comments(step.as_ref());
let step_leading_comments = comments.leading(step.as_ref());
leading_comments_spacing(f, step_leading_comments)?;
step.format().fmt(f)?;
} else if !dangling_step_comments.is_empty() {
Expand Down

0 comments on commit 0cea497

Please sign in to comment.