Skip to content

Commit be450a6

Browse files
committed
Use single lookup for leading, dangling, and trailing comments
1 parent 232b44a commit be450a6

39 files changed

+317
-385
lines changed

crates/ruff_python_formatter/src/comments/map.rs

+120-274
Large diffs are not rendered by default.

crates/ruff_python_formatter/src/comments/mod.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ use ruff_python_index::CommentRanges;
106106
use ruff_python_trivia::PythonWhitespace;
107107

108108
use crate::comments::debug::{DebugComment, DebugComments};
109-
use crate::comments::map::MultiMap;
109+
use crate::comments::map::{LeadingDanglingTrailing, MultiMap};
110110
use crate::comments::node_key::NodeRefEqualityKey;
111111
use crate::comments::visitor::CommentsVisitor;
112112

@@ -405,13 +405,13 @@ impl<'a> Comments<'a> {
405405
pub(crate) fn leading_dangling_trailing_comments<T>(
406406
&self,
407407
node: T,
408-
) -> impl Iterator<Item = &SourceComment>
408+
) -> LeadingDanglingTrailingComments
409409
where
410410
T: Into<AnyNodeRef<'a>>,
411411
{
412412
self.data
413413
.comments
414-
.parts(&NodeRefEqualityKey::from_ref(node.into()))
414+
.leading_dangling_trailing(&NodeRefEqualityKey::from_ref(node.into()))
415415
}
416416

417417
#[inline(always)]
@@ -464,6 +464,8 @@ impl<'a> Comments<'a> {
464464
}
465465
}
466466

467+
pub(crate) type LeadingDanglingTrailingComments<'a> = LeadingDanglingTrailing<'a, SourceComment>;
468+
467469
#[derive(Debug, Default)]
468470
struct CommentsData<'a> {
469471
comments: CommentsMap<'a>,

crates/ruff_python_formatter/src/expression/expr_attribute.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use ruff_formatter::{write, FormatRuleWithOptions};
22
use ruff_python_ast::node::AnyNodeRef;
33
use ruff_python_ast::{Constant, Expr, ExprAttribute, ExprConstant};
44

5-
use crate::comments::{leading_comments, trailing_comments};
5+
use crate::comments::{leading_comments, trailing_comments, SourceComment};
66
use crate::expression::parentheses::{
77
is_expression_parenthesized, NeedsParentheses, OptionalParentheses, Parentheses,
88
};
@@ -150,7 +150,7 @@ impl FormatNodeRule<ExprAttribute> for FormatExprAttribute {
150150

151151
fn fmt_dangling_comments(
152152
&self,
153-
_node: &ExprAttribute,
153+
_dangling_comments: &[SourceComment],
154154
_f: &mut PyFormatter,
155155
) -> FormatResult<()> {
156156
// handle in `fmt_fields`

crates/ruff_python_formatter/src/expression/expr_bin_op.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use smallvec::SmallVec;
99
use ruff_formatter::{format_args, write, FormatOwnedWithRule, FormatRefWithRule};
1010
use ruff_python_ast::node::{AnyNodeRef, AstNode};
1111

12-
use crate::comments::{trailing_comments, trailing_node_comments};
12+
use crate::comments::{trailing_comments, trailing_node_comments, SourceComment};
1313
use crate::expression::expr_constant::ExprConstantLayout;
1414
use crate::expression::parentheses::{
1515
in_parentheses_only_group, in_parentheses_only_soft_line_break,
@@ -147,7 +147,11 @@ impl FormatNodeRule<ExprBinOp> for FormatExprBinOp {
147147
}
148148
}
149149

150-
fn fmt_dangling_comments(&self, _node: &ExprBinOp, _f: &mut PyFormatter) -> FormatResult<()> {
150+
fn fmt_dangling_comments(
151+
&self,
152+
_dangling_comments: &[SourceComment],
153+
_f: &mut PyFormatter,
154+
) -> FormatResult<()> {
151155
// Handled inside of `fmt_fields`
152156
Ok(())
153157
}

crates/ruff_python_formatter/src/expression/expr_bool_op.rs

+10-16
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::comments::leading_comments;
22
use crate::expression::parentheses::{
33
in_parentheses_only_group, in_parentheses_only_soft_line_break_or_space, NeedsParentheses,
4-
OptionalParentheses, Parentheses,
4+
OptionalParentheses,
55
};
66
use crate::prelude::*;
77
use ruff_formatter::{write, FormatOwnedWithRule, FormatRefWithRule, FormatRuleWithOptions};
@@ -12,20 +12,20 @@ use super::parentheses::is_expression_parenthesized;
1212

1313
#[derive(Default)]
1414
pub struct FormatExprBoolOp {
15-
parentheses: Option<Parentheses>,
16-
chained: bool,
15+
layout: BoolOpLayout,
1716
}
1817

19-
pub struct BoolOpLayout {
20-
pub(crate) parentheses: Option<Parentheses>,
21-
pub(crate) chained: bool,
18+
#[derive(Default, Copy, Clone)]
19+
pub enum BoolOpLayout {
20+
#[default]
21+
Default,
22+
Chained,
2223
}
2324

2425
impl FormatRuleWithOptions<ExprBoolOp, PyFormatContext<'_>> for FormatExprBoolOp {
2526
type Options = BoolOpLayout;
2627
fn with_options(mut self, options: Self::Options) -> Self {
27-
self.parentheses = options.parentheses;
28-
self.chained = options.chained;
28+
self.layout = options;
2929
self
3030
}
3131
}
@@ -68,7 +68,7 @@ impl FormatNodeRule<ExprBoolOp> for FormatExprBoolOp {
6868
Ok(())
6969
});
7070

71-
if self.chained {
71+
if matches!(self.layout, BoolOpLayout::Chained) {
7272
// Chained boolean operations should not be given a new group
7373
inner.fmt(f)
7474
} else {
@@ -101,13 +101,7 @@ impl Format<PyFormatContext<'_>> for FormatValue<'_> {
101101
) =>
102102
{
103103
// Mark chained boolean operations e.g. `x and y or z` and avoid creating a new group
104-
write!(
105-
f,
106-
[bool_op.format().with_options(BoolOpLayout {
107-
parentheses: None,
108-
chained: true,
109-
})]
110-
)
104+
write!(f, [bool_op.format().with_options(BoolOpLayout::Chained)])
111105
}
112106
_ => write!(f, [in_parentheses_only_group(&self.value.format())]),
113107
}

crates/ruff_python_formatter/src/expression/expr_compare.rs

+9-16
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,16 @@
1-
use crate::comments::leading_comments;
1+
use crate::comments::{leading_comments, SourceComment};
22
use crate::expression::parentheses::{
33
in_parentheses_only_group, in_parentheses_only_soft_line_break_or_space, NeedsParentheses,
4-
OptionalParentheses, Parentheses,
4+
OptionalParentheses,
55
};
66
use crate::prelude::*;
77
use crate::FormatNodeRule;
8-
use ruff_formatter::{write, FormatOwnedWithRule, FormatRefWithRule, FormatRuleWithOptions};
8+
use ruff_formatter::{write, FormatOwnedWithRule, FormatRefWithRule};
99
use ruff_python_ast::node::AnyNodeRef;
1010
use ruff_python_ast::{CmpOp, ExprCompare};
1111

1212
#[derive(Default)]
13-
pub struct FormatExprCompare {
14-
parentheses: Option<Parentheses>,
15-
}
16-
17-
impl FormatRuleWithOptions<ExprCompare, PyFormatContext<'_>> for FormatExprCompare {
18-
type Options = Option<Parentheses>;
19-
20-
fn with_options(mut self, options: Self::Options) -> Self {
21-
self.parentheses = options;
22-
self
23-
}
24-
}
13+
pub struct FormatExprCompare;
2514

2615
impl FormatNodeRule<ExprCompare> for FormatExprCompare {
2716
fn fmt_fields(&self, item: &ExprCompare, f: &mut PyFormatter) -> FormatResult<()> {
@@ -70,7 +59,11 @@ impl FormatNodeRule<ExprCompare> for FormatExprCompare {
7059
in_parentheses_only_group(&inner).fmt(f)
7160
}
7261

73-
fn fmt_dangling_comments(&self, _node: &ExprCompare, _f: &mut PyFormatter) -> FormatResult<()> {
62+
fn fmt_dangling_comments(
63+
&self,
64+
_dangling_comments: &[SourceComment],
65+
_f: &mut PyFormatter,
66+
) -> FormatResult<()> {
7467
// Node can not have dangling comments
7568
Ok(())
7669
}

crates/ruff_python_formatter/src/expression/expr_constant.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::comments::SourceComment;
12
use ruff_formatter::FormatRuleWithOptions;
23
use ruff_python_ast::node::AnyNodeRef;
34
use ruff_python_ast::{Constant, ExprConstant, Ranged};
@@ -65,7 +66,7 @@ impl FormatNodeRule<ExprConstant> for FormatExprConstant {
6566

6667
fn fmt_dangling_comments(
6768
&self,
68-
_node: &ExprConstant,
69+
_dangling_comments: &[SourceComment],
6970
_f: &mut PyFormatter,
7071
) -> FormatResult<()> {
7172
Ok(())

crates/ruff_python_formatter/src/expression/expr_dict.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use ruff_python_ast::Ranged;
44
use ruff_python_ast::{Expr, ExprDict};
55
use ruff_text_size::TextRange;
66

7-
use crate::comments::leading_comments;
7+
use crate::comments::{leading_comments, SourceComment};
88
use crate::expression::parentheses::{
99
empty_parenthesized, parenthesized, NeedsParentheses, OptionalParentheses,
1010
};
@@ -89,7 +89,11 @@ impl FormatNodeRule<ExprDict> for FormatExprDict {
8989
.fmt(f)
9090
}
9191

92-
fn fmt_dangling_comments(&self, _node: &ExprDict, _f: &mut PyFormatter) -> FormatResult<()> {
92+
fn fmt_dangling_comments(
93+
&self,
94+
_dangling_comments: &[SourceComment],
95+
_f: &mut PyFormatter,
96+
) -> FormatResult<()> {
9397
// Handled by `fmt_fields`
9498
Ok(())
9599
}

crates/ruff_python_formatter/src/expression/expr_generator_exp.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use ruff_formatter::{format_args, write, Buffer, FormatResult, FormatRuleWithOpt
22
use ruff_python_ast::node::AnyNodeRef;
33
use ruff_python_ast::ExprGeneratorExp;
44

5-
use crate::comments::leading_comments;
5+
use crate::comments::{leading_comments, SourceComment};
66
use crate::context::PyFormatContext;
77
use crate::expression::parentheses::{parenthesized, NeedsParentheses, OptionalParentheses};
88
use crate::prelude::*;
@@ -81,7 +81,7 @@ impl FormatNodeRule<ExprGeneratorExp> for FormatExprGeneratorExp {
8181

8282
fn fmt_dangling_comments(
8383
&self,
84-
_node: &ExprGeneratorExp,
84+
_dangling_comments: &[SourceComment],
8585
_f: &mut PyFormatter,
8686
) -> FormatResult<()> {
8787
// Handled as part of `fmt_fields`

crates/ruff_python_formatter/src/expression/expr_lambda.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::comments::dangling_node_comments;
1+
use crate::comments::{dangling_node_comments, SourceComment};
22
use crate::context::PyFormatContext;
33
use crate::expression::parentheses::{NeedsParentheses, OptionalParentheses};
44
use crate::other::parameters::ParametersParentheses;
@@ -55,7 +55,11 @@ impl FormatNodeRule<ExprLambda> for FormatExprLambda {
5555
)
5656
}
5757

58-
fn fmt_dangling_comments(&self, _node: &ExprLambda, _f: &mut PyFormatter) -> FormatResult<()> {
58+
fn fmt_dangling_comments(
59+
&self,
60+
_dangling_comments: &[SourceComment],
61+
_f: &mut PyFormatter,
62+
) -> FormatResult<()> {
5963
// Override. Dangling comments are handled in `fmt_fields`.
6064
Ok(())
6165
}

crates/ruff_python_formatter/src/expression/expr_list.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::comments::SourceComment;
12
use ruff_formatter::prelude::format_with;
23
use ruff_python_ast::node::AnyNodeRef;
34
use ruff_python_ast::{ExprList, Ranged};
@@ -37,7 +38,11 @@ impl FormatNodeRule<ExprList> for FormatExprList {
3738
.fmt(f)
3839
}
3940

40-
fn fmt_dangling_comments(&self, _node: &ExprList, _f: &mut PyFormatter) -> FormatResult<()> {
41+
fn fmt_dangling_comments(
42+
&self,
43+
_dangling_comments: &[SourceComment],
44+
_f: &mut PyFormatter,
45+
) -> FormatResult<()> {
4146
// Handled as part of `fmt_fields`
4247
Ok(())
4348
}

crates/ruff_python_formatter/src/expression/expr_list_comp.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use ruff_formatter::{format_args, write, FormatResult};
22
use ruff_python_ast::node::AnyNodeRef;
33
use ruff_python_ast::ExprListComp;
44

5+
use crate::comments::SourceComment;
56
use crate::context::PyFormatContext;
67
use crate::expression::parentheses::{parenthesized, NeedsParentheses, OptionalParentheses};
78
use crate::prelude::*;
@@ -45,7 +46,7 @@ impl FormatNodeRule<ExprListComp> for FormatExprListComp {
4546

4647
fn fmt_dangling_comments(
4748
&self,
48-
_node: &ExprListComp,
49+
_dangling_comments: &[SourceComment],
4950
_f: &mut PyFormatter,
5051
) -> FormatResult<()> {
5152
// Handled as part of `fmt_fields`

crates/ruff_python_formatter/src/expression/expr_name.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::comments::SourceComment;
12
use crate::expression::parentheses::{NeedsParentheses, OptionalParentheses};
23
use crate::prelude::*;
34
use crate::FormatNodeRule;
@@ -23,7 +24,11 @@ impl FormatNodeRule<ExprName> for FormatExprName {
2324
write!(f, [source_text_slice(*range, ContainsNewlines::No)])
2425
}
2526

26-
fn fmt_dangling_comments(&self, _node: &ExprName, _f: &mut PyFormatter) -> FormatResult<()> {
27+
fn fmt_dangling_comments(
28+
&self,
29+
_dangling_comments: &[SourceComment],
30+
_f: &mut PyFormatter,
31+
) -> FormatResult<()> {
2732
// Node cannot have dangling comments
2833
Ok(())
2934
}

crates/ruff_python_formatter/src/expression/expr_set.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::comments::SourceComment;
12
use ruff_python_ast::node::AnyNodeRef;
23
use ruff_python_ast::{ExprSet, Ranged};
34

@@ -28,7 +29,11 @@ impl FormatNodeRule<ExprSet> for FormatExprSet {
2829
.fmt(f)
2930
}
3031

31-
fn fmt_dangling_comments(&self, _node: &ExprSet, _f: &mut PyFormatter) -> FormatResult<()> {
32+
fn fmt_dangling_comments(
33+
&self,
34+
_dangling_comments: &[SourceComment],
35+
_f: &mut PyFormatter,
36+
) -> FormatResult<()> {
3237
// Handled as part of `fmt_fields`
3338
Ok(())
3439
}

crates/ruff_python_formatter/src/expression/expr_set_comp.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use ruff_formatter::{format_args, write, Buffer, FormatResult};
22
use ruff_python_ast::node::AnyNodeRef;
33
use ruff_python_ast::ExprSetComp;
44

5+
use crate::comments::SourceComment;
56
use crate::context::PyFormatContext;
67
use crate::expression::parentheses::{parenthesized, NeedsParentheses, OptionalParentheses};
78
use crate::prelude::*;
@@ -43,7 +44,11 @@ impl FormatNodeRule<ExprSetComp> for FormatExprSetComp {
4344
)
4445
}
4546

46-
fn fmt_dangling_comments(&self, _node: &ExprSetComp, _f: &mut PyFormatter) -> FormatResult<()> {
47+
fn fmt_dangling_comments(
48+
&self,
49+
_dangling_comments: &[SourceComment],
50+
_f: &mut PyFormatter,
51+
) -> FormatResult<()> {
4752
// Handled as part of `fmt_fields`
4853
Ok(())
4954
}

crates/ruff_python_formatter/src/expression/expr_starred.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use ruff_python_ast::ExprStarred;
22

3+
use crate::comments::SourceComment;
34
use ruff_formatter::write;
45
use ruff_python_ast::node::AnyNodeRef;
56

@@ -22,9 +23,11 @@ impl FormatNodeRule<ExprStarred> for FormatExprStarred {
2223
write!(f, [text("*"), value.format()])
2324
}
2425

25-
fn fmt_dangling_comments(&self, node: &ExprStarred, f: &mut PyFormatter) -> FormatResult<()> {
26-
debug_assert_eq!(f.context().comments().dangling_comments(node), []);
27-
26+
fn fmt_dangling_comments(
27+
&self,
28+
_dangling_comments: &[SourceComment],
29+
_f: &mut PyFormatter,
30+
) -> FormatResult<()> {
2831
Ok(())
2932
}
3033
}

crates/ruff_python_formatter/src/expression/expr_subscript.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use ruff_formatter::{format_args, write, FormatRuleWithOptions};
22
use ruff_python_ast::node::{AnyNodeRef, AstNode};
33
use ruff_python_ast::{Expr, ExprSubscript};
44

5-
use crate::comments::trailing_comments;
5+
use crate::comments::{trailing_comments, SourceComment};
66
use crate::context::PyFormatContext;
77
use crate::context::{NodeLevel, WithNodeLevel};
88
use crate::expression::expr_tuple::TupleParentheses;
@@ -81,7 +81,7 @@ impl FormatNodeRule<ExprSubscript> for FormatExprSubscript {
8181

8282
fn fmt_dangling_comments(
8383
&self,
84-
_node: &ExprSubscript,
84+
_dangling_comments: &[SourceComment],
8585
_f: &mut PyFormatter,
8686
) -> FormatResult<()> {
8787
// Handled inside of `fmt_fields`

0 commit comments

Comments
 (0)