move dangling comment handling back out of placement.rs

Revert "re-apply 'pass preview to handle_lambda_comment'"

This reverts commit 33fcca9c53.
This commit is contained in:
Brent Westbrook 2025-12-10 15:47:58 -05:00
parent b96cf96e8b
commit 553b45e27f
No known key found for this signature in database
10 changed files with 162 additions and 193 deletions

View File

@ -101,7 +101,6 @@ use ruff_python_trivia::{CommentLinePosition, CommentRanges, SuppressionKind};
use ruff_text_size::{Ranged, TextRange};
pub(crate) use visitor::collect_comments;
use crate::PreviewMode;
use crate::comments::debug::{DebugComment, DebugComments};
use crate::comments::map::{LeadingDanglingTrailing, MultiMap};
use crate::comments::node_key::NodeRefEqualityKey;
@ -249,19 +248,16 @@ impl<'a> Comments<'a> {
root: impl Into<AnyNodeRef<'a>>,
source_code: SourceCode<'a>,
comment_ranges: &'a CommentRanges,
preview: PreviewMode,
) -> Self {
fn collect_comments<'a>(
root: AnyNodeRef<'a>,
source_code: SourceCode<'a>,
comment_ranges: &'a CommentRanges,
preview: PreviewMode,
) -> Comments<'a> {
let map = if comment_ranges.is_empty() {
CommentsMap::new()
} else {
let mut builder =
CommentsMapBuilder::new(source_code.as_str(), comment_ranges, preview);
let mut builder = CommentsMapBuilder::new(source_code.as_str(), comment_ranges);
CommentsVisitor::new(source_code, comment_ranges, &mut builder).visit(root);
builder.finish()
};
@ -269,7 +265,7 @@ impl<'a> Comments<'a> {
Comments::new(map, comment_ranges)
}
collect_comments(root.into(), source_code, comment_ranges, preview)
collect_comments(root.into(), source_code, comment_ranges)
}
/// Returns `true` if the given `node` has any comments.
@ -524,7 +520,7 @@ mod tests {
use ruff_python_parser::{ParseOptions, Parsed, parse};
use ruff_python_trivia::CommentRanges;
use crate::{PreviewMode, comments::Comments};
use crate::comments::Comments;
struct CommentsTestCase<'a> {
parsed: Parsed<Mod>,
@ -548,12 +544,7 @@ mod tests {
}
fn to_comments(&self) -> Comments<'_> {
Comments::from_ast(
self.parsed.syntax(),
self.source_code,
&self.comment_ranges,
PreviewMode::Disabled,
)
Comments::from_ast(self.parsed.syntax(), self.source_code, &self.comment_ranges)
}
}

View File

@ -11,7 +11,6 @@ use ruff_source_file::LineRanges;
use ruff_text_size::{Ranged, TextLen, TextRange, TextSize};
use std::cmp::Ordering;
use crate::PreviewMode;
use crate::comments::visitor::{CommentPlacement, DecoratedComment};
use crate::expression::expr_slice::{ExprSliceCommentSection, assign_comment_in_slice};
use crate::expression::parentheses::is_expression_parenthesized;
@ -19,19 +18,17 @@ use crate::other::parameters::{
assign_argument_separator_comment_placement, find_parameter_separators,
};
use crate::pattern::pattern_match_sequence::SequenceType;
use crate::preview::is_parenthesize_lambda_bodies_enabled_preview;
/// Manually attach comments to nodes that the default placement gets wrong.
pub(super) fn place_comment<'a>(
comment: DecoratedComment<'a>,
comment_ranges: &CommentRanges,
source: &str,
preview: PreviewMode,
) -> CommentPlacement<'a> {
handle_parenthesized_comment(comment, source)
.or_else(|comment| handle_end_of_line_comment_around_body(comment, source))
.or_else(|comment| handle_own_line_comment_around_body(comment, source))
.or_else(|comment| handle_enclosed_comment(comment, comment_ranges, source, preview))
.or_else(|comment| handle_enclosed_comment(comment, comment_ranges, source))
}
/// Handle parenthesized comments. A parenthesized comment is a comment that appears within a
@ -196,7 +193,6 @@ fn handle_enclosed_comment<'a>(
comment: DecoratedComment<'a>,
comment_ranges: &CommentRanges,
source: &str,
preview: PreviewMode,
) -> CommentPlacement<'a> {
match comment.enclosing_node() {
AnyNodeRef::Parameters(parameters) => {
@ -235,7 +231,7 @@ fn handle_enclosed_comment<'a>(
}
AnyNodeRef::ExprUnaryOp(unary_op) => handle_unary_op_comment(comment, unary_op, source),
AnyNodeRef::ExprNamed(_) => handle_named_expr_comment(comment, source),
AnyNodeRef::ExprLambda(lambda) => handle_lambda_comment(comment, lambda, source, preview),
AnyNodeRef::ExprLambda(lambda) => handle_lambda_comment(comment, lambda, source),
AnyNodeRef::ExprDict(_) => handle_dict_unpacking_comment(comment, source)
.or_else(|comment| handle_bracketed_end_of_line_comment(comment, source))
.or_else(|comment| handle_key_value_comment(comment, source)),
@ -1800,35 +1796,21 @@ fn handle_named_expr_comment<'a>(
/// Handles comments around the `:` token in a lambda expression.
///
/// For parameterized lambdas, comments will have the following placements:
/// For parameterized lambdas, both the comments between the `lambda` and the parameters, and the
/// comments between the parameters and the body, are considered dangling, as is the case for all
/// of the following:
///
/// ```python
/// (
/// lambda # dangling lambda
/// # leading parameters
/// lambda # 1
/// # 2
/// x
/// : # dangling lambda
/// # dangling lambda
/// : # 3
/// # 4
/// y
/// )
/// ```
///
/// In [preview](is_parenthesize_lambda_bodies_enabled_preview), the comment placement is instead:
///
/// ```python
/// (
/// lambda # dangling lambda
/// # leading parameters
/// x
/// : # leading body
/// # leading body
/// y
/// )
/// ```
///
/// Note that the final two comments are now leading on the body expression instead of dangling on
/// the lambda, which allows them to be moved into the parenthesized body.
///
/// For non-parameterized lambdas, all comments before the body are considered dangling, as is the
/// case for all of the following:
///
@ -1841,25 +1823,10 @@ fn handle_named_expr_comment<'a>(
/// y
/// )
/// ```
///
/// In [preview](is_parenthesize_lambda_bodies_enabled_preview), these all instead become leading
/// comments on the body, allowing this formatting:
///
/// ```python
/// (
/// lambda: ( # 1
/// # 2
/// # 3
/// # 4
/// y
/// )
/// )
/// ```
fn handle_lambda_comment<'a>(
comment: DecoratedComment<'a>,
lambda: &'a ast::ExprLambda,
source: &str,
preview: PreviewMode,
) -> CommentPlacement<'a> {
if let Some(parameters) = lambda.parameters.as_deref() {
// End-of-line comments between the `lambda` and the parameters are dangling on the lambda:
@ -1895,32 +1862,26 @@ fn handle_lambda_comment<'a>(
// y
// )
// ```
// Except in preview, where they become leading on the body instead, regardless of
// parenthesization.
if parameters.end() < comment.start() && comment.start() < lambda.body.start() {
return if is_parenthesize_lambda_bodies_enabled_preview(preview) {
CommentPlacement::leading(&*lambda.body, comment)
} else {
// If the value is parenthesized, and the comment is within the parentheses, it should
// be a leading comment on the value, not a dangling comment in the lambda, as in:
// ```python
// (
// lambda x: ( # comment
// y
// )
// )
// ```
let tokenizer =
SimpleTokenizer::new(source, TextRange::new(parameters.end(), comment.start()));
if tokenizer
.skip_trivia()
.any(|token| token.kind == SimpleTokenKind::LParen)
{
return CommentPlacement::Default(comment);
}
// If the value is parenthesized, and the comment is within the parentheses, it should
// be a leading comment on the value, not a dangling comment in the lambda, as in:
// ```python
// (
// lambda x: ( # comment
// y
// )
// )
// ```
let tokenizer =
SimpleTokenizer::new(source, TextRange::new(parameters.end(), comment.start()));
if tokenizer
.skip_trivia()
.any(|token| token.kind == SimpleTokenKind::LParen)
{
return CommentPlacement::Default(comment);
}
CommentPlacement::dangling(comment.enclosing_node(), comment)
};
return CommentPlacement::dangling(comment.enclosing_node(), comment);
}
} else {
// Comments between the lambda and the body are dangling on the lambda:
@ -1930,32 +1891,26 @@ fn handle_lambda_comment<'a>(
// y
// )
// ```
// Except in preview, where they become leading on the body instead, regardless of
// parenthesization.
if comment.start() < lambda.body.start() {
return if is_parenthesize_lambda_bodies_enabled_preview(preview) {
CommentPlacement::leading(&*lambda.body, comment)
} else {
// If the value is parenthesized, and the comment is within the parentheses, it should
// be a leading comment on the value, not a dangling comment in the lambda, as in:
// ```python
// (
// lambda: ( # comment
// y
// )
// )
// ```
let tokenizer =
SimpleTokenizer::new(source, TextRange::new(lambda.start(), comment.start()));
if tokenizer
.skip_trivia()
.any(|token| token.kind == SimpleTokenKind::LParen)
{
return CommentPlacement::Default(comment);
}
// If the value is parenthesized, and the comment is within the parentheses, it should
// be a leading comment on the value, not a dangling comment in the lambda, as in:
// ```python
// (
// lambda: ( # comment
// y
// )
// )
// ```
let tokenizer =
SimpleTokenizer::new(source, TextRange::new(lambda.start(), comment.start()));
if tokenizer
.skip_trivia()
.any(|token| token.kind == SimpleTokenKind::LParen)
{
return CommentPlacement::Default(comment);
}
CommentPlacement::dangling(comment.enclosing_node(), comment)
};
return CommentPlacement::dangling(comment.enclosing_node(), comment);
}
}

View File

@ -11,7 +11,6 @@ use ruff_python_ast::visitor::source_order::*;
use ruff_python_trivia::{CommentLinePosition, CommentRanges};
use ruff_text_size::{Ranged, TextRange, TextSize};
use crate::PreviewMode;
use crate::comments::node_key::NodeRefEqualityKey;
use crate::comments::placement::place_comment;
use crate::comments::{CommentsMap, SourceComment};
@ -536,12 +535,11 @@ pub(super) struct CommentsMapBuilder<'a> {
/// We need those for backwards lexing
comment_ranges: &'a CommentRanges,
source: &'a str,
preview: PreviewMode,
}
impl<'a> PushComment<'a> for CommentsMapBuilder<'a> {
fn push_comment(&mut self, placement: DecoratedComment<'a>) {
let placement = place_comment(placement, self.comment_ranges, self.source, self.preview);
let placement = place_comment(placement, self.comment_ranges, self.source);
match placement {
CommentPlacement::Leading { node, comment } => {
self.push_leading_comment(node, comment);
@ -603,16 +601,11 @@ impl<'a> PushComment<'a> for CommentsMapBuilder<'a> {
}
impl<'a> CommentsMapBuilder<'a> {
pub(crate) fn new(
source: &'a str,
comment_ranges: &'a CommentRanges,
preview: PreviewMode,
) -> Self {
pub(crate) fn new(source: &'a str, comment_ranges: &'a CommentRanges) -> Self {
Self {
comments: CommentsMap::default(),
comment_ranges,
source,
preview,
}
}

View File

@ -127,6 +127,51 @@ impl FormatNodeRule<ExprLambda> for FormatExprLambda {
if dangling_after_parameters.is_empty() {
write!(f, [space()])?;
}
// In preview, always parenthesize the body if there are dangling comments.
else if preview {
// Can't use partition_point because there can be additional end of line comments
// after the initial set. All of these comments are dangling, for example:
//
// ```python
// (
// lambda # 1
// # 2
// : # 3
// # 4
// y
// )
// ```
//
// and alternate between own line and end of line.
let (after_parameters_end_of_line, leading_body_comments) =
dangling_after_parameters.split_at(
dangling_after_parameters
.iter()
.position(|comment| comment.line_position().is_own_line())
.unwrap_or(dangling_after_parameters.len()),
);
let fmt_body = format_with(|f: &mut PyFormatter| {
write!(
f,
[
space(),
token("("),
trailing_comments(after_parameters_end_of_line),
block_indent(&format_args!(
leading_comments(leading_body_comments),
body.format().with_options(Parentheses::Never)
)),
token(")")
]
)
});
return match self.layout {
ExprLambdaLayout::Assignment => fits_expanded(&fmt_body).fmt(f),
ExprLambdaLayout::Default => fmt_body.fmt(f),
};
} else {
write!(f, [dangling_comments(dangling_after_parameters)])?;
}
@ -136,6 +181,36 @@ impl FormatNodeRule<ExprLambda> for FormatExprLambda {
// In this context, a dangling comment is a comment between the `lambda` and the body.
if dangling.is_empty() {
write!(f, [space()])?;
}
// In preview, always parenthesize the body if there are dangling comments.
else if preview {
let (dangling_end_of_line, dangling_own_line) = dangling.split_at(
dangling
.iter()
.position(|comment| comment.line_position().is_own_line())
.unwrap_or(dangling.len()),
);
let fmt_body = format_with(|f: &mut PyFormatter| {
write!(
f,
[
space(),
token("("),
trailing_comments(dangling_end_of_line),
block_indent(&format_args!(
leading_comments(dangling_own_line),
body.format().with_options(Parentheses::Never)
)),
token(")")
]
)
});
return match self.layout {
ExprLambdaLayout::Assignment => fits_expanded(&fmt_body).fmt(f),
ExprLambdaLayout::Default => fmt_body.fmt(f),
};
} else {
write!(f, [dangling_comments(dangling)])?;
}
@ -148,25 +223,7 @@ impl FormatNodeRule<ExprLambda> for FormatExprLambda {
// ensures that we correctly handle parenthesized comments, and don't need to worry
// about them in the implementation below.
if body_comments.has_leading() || body_comments.has_trailing_own_line() {
let (leading_end_of_line, leading_own_line) = body_comments.leading.split_at(
body_comments
.leading
.iter()
.position(|comment| comment.line_position().is_own_line())
.unwrap_or(body_comments.leading.len()),
);
write!(
f,
[
token("("),
trailing_comments(leading_end_of_line),
block_indent(&format_args!(
leading_comments(leading_own_line),
body.format().with_options(Parentheses::Never)
)),
token(")")
]
)
body.format().with_options(Parentheses::Always).fmt(f)
}
// Calls and subscripts require special formatting because they have their own
// parentheses, but they can also have an arbitrary amount of text before the

View File

@ -177,12 +177,7 @@ where
&'a N: Into<AnyNodeRef<'a>>,
{
let source_code = SourceCode::new(source);
let comments = Comments::from_ast(
parsed.syntax(),
source_code,
comment_ranges,
options.preview(),
);
let comments = Comments::from_ast(parsed.syntax(), source_code, comment_ranges);
let formatted = format!(
PyFormatContext::new(options, source, comments, parsed.tokens()),
@ -218,14 +213,9 @@ pub fn formatted_file(db: &dyn Db, file: File) -> Result<Option<String>, FormatM
}
/// Public function for generating a printable string of the debug comments.
pub fn pretty_comments(
module: &Mod,
comment_ranges: &CommentRanges,
source: &str,
preview: PreviewMode,
) -> String {
pub fn pretty_comments(module: &Mod, comment_ranges: &CommentRanges, source: &str) -> String {
let source_code = SourceCode::new(source);
let comments = Comments::from_ast(module, source_code, comment_ranges, preview);
let comments = Comments::from_ast(module, source_code, comment_ranges);
std::format!("{comments:#?}", comments = comments.debug(source_code))
}

View File

@ -5,7 +5,7 @@
//! for which specific feature this preview check is for. Having named functions simplifies the promotion:
//! Simply delete the function and let Rust tell you which checks you have to remove.
use crate::{PreviewMode, PyFormatContext};
use crate::PyFormatContext;
/// Returns `true` if the [`hug_parens_with_braces_and_square_brackets`](https://github.com/astral-sh/ruff/issues/8279) preview style is enabled.
pub(crate) const fn is_hug_parens_with_braces_and_square_brackets_enabled(
@ -59,6 +59,3 @@ pub(crate) const fn is_avoid_parens_for_long_as_captures_enabled(
pub(crate) const fn is_parenthesize_lambda_bodies_enabled(context: &PyFormatContext) -> bool {
context.is_preview()
}
pub(crate) const fn is_parenthesize_lambda_bodies_enabled_preview(preview: PreviewMode) -> bool {
preview.is_enabled()
}

View File

@ -76,12 +76,7 @@ pub fn format_range(
let parsed = parse(source, ParseOptions::from(options.source_type()))?;
let source_code = SourceCode::new(source);
let comment_ranges = CommentRanges::from(parsed.tokens());
let comments = Comments::from_ast(
parsed.syntax(),
source_code,
&comment_ranges,
options.preview(),
);
let comments = Comments::from_ast(parsed.syntax(), source_code, &comment_ranges);
let mut context = PyFormatContext::new(
options.with_source_map_generation(SourceMapGeneration::Enabled),

View File

@ -1584,12 +1584,7 @@ fn docstring_format_source(
let parsed = ruff_python_parser::parse(source, ParseOptions::from(source_type))?;
let comment_ranges = CommentRanges::from(parsed.tokens());
let source_code = ruff_formatter::SourceCode::new(source);
let comments = crate::Comments::from_ast(
parsed.syntax(),
source_code,
&comment_ranges,
options.preview(),
);
let comments = crate::Comments::from_ast(parsed.syntax(), source_code, &comment_ranges);
let ctx = PyFormatContext::new(options, source, comments, parsed.tokens())
.in_docstring(docstring_quote_style);

View File

@ -668,7 +668,7 @@ x = (
)
(
lambda * # comemnt 1
lambda * # comment 1
ergs, **
# comment 2
kwargs # comment 3
@ -1496,7 +1496,7 @@ x = (
(
lambda
# comemnt 1
# comment 1
*ergs,
# comment 2
**kwargs: # comment 3
@ -1738,7 +1738,7 @@ transform = (
)
lambda *x: x
@@ -161,30 +147,33 @@
@@ -161,30 +147,34 @@
)
(
@ -1780,11 +1780,12 @@ transform = (
(
- lambda: # comment
- ( # comment
+ lambda: ( # comment # comment
+ lambda: ( # comment
+ # comment
x
)
)
@@ -192,11 +181,12 @@
@@ -192,11 +182,12 @@
(
lambda # 1
# 2
@ -1802,7 +1803,7 @@ transform = (
)
(
@@ -204,9 +194,10 @@
@@ -204,9 +195,10 @@
# 2
x, # 3
# 4
@ -1816,7 +1817,7 @@ transform = (
)
(
@@ -218,71 +209,79 @@
@@ -218,71 +210,79 @@
# Leading
lambda x: (
@ -1955,7 +1956,7 @@ transform = (
# Regression tests for https://github.com/astral-sh/ruff/issues/8179
@@ -291,9 +290,9 @@
@@ -291,9 +291,9 @@
c,
d,
e,
@ -1968,7 +1969,7 @@ transform = (
)
@@ -302,15 +301,9 @@
@@ -302,15 +302,9 @@
c,
d,
e,
@ -1987,7 +1988,7 @@ transform = (
g=10,
)
@@ -320,9 +313,9 @@
@@ -320,9 +314,9 @@
c,
d,
e,
@ -2000,7 +2001,7 @@ transform = (
)
@@ -338,9 +331,9 @@
@@ -338,9 +332,9 @@
class C:
function_dict: Dict[Text, Callable[[CRFToken], Any]] = {
@ -2013,7 +2014,7 @@ transform = (
}
@@ -352,42 +345,40 @@
@@ -352,42 +346,40 @@
def foo():
if True:
if True:
@ -2072,7 +2073,7 @@ transform = (
CREATE TABLE {table} AS
SELECT ROW_NUMBER() OVER () AS id, {var}
FROM (
@@ -401,18 +392,19 @@
@@ -401,18 +393,19 @@
long_assignment_target.with_attribute.and_a_slice[with_an_index] = ( # 1
# 2
@ -2099,7 +2100,7 @@ transform = (
)
very_long_variable_name_x, very_long_variable_name_y = (
@@ -420,8 +412,8 @@
@@ -420,8 +413,8 @@
lambda b: b * another_very_long_expression_here,
)
@ -2110,7 +2111,7 @@ transform = (
x, more_args, additional_parameters
)
)
@@ -457,12 +449,12 @@
@@ -457,12 +450,12 @@
[
# Not fluent
param(
@ -2125,7 +2126,7 @@ transform = (
),
param(
lambda left, right: (
@@ -471,9 +463,9 @@
@@ -471,9 +464,9 @@
),
param(lambda left, right: ibis.timestamp("2017-04-01").cast(dt.date)),
param(
@ -2138,7 +2139,7 @@ transform = (
),
# This is too long on one line in the lambda body and gets wrapped
# inside the body.
@@ -507,16 +499,18 @@
@@ -507,16 +500,18 @@
]
# adds parentheses around the body
@ -2160,7 +2161,7 @@ transform = (
lambda x, y, z: (
x + y + z
@@ -527,7 +521,7 @@
@@ -527,7 +522,7 @@
x + y + z # trailing eol body
)
@ -2169,7 +2170,7 @@ transform = (
lambda x, y, z: (
# leading body
@@ -539,21 +533,23 @@
@@ -539,21 +534,23 @@
)
(
@ -2203,7 +2204,7 @@ transform = (
# dangling header comment
source_bucket
if name == source_bucket_name
@@ -561,8 +557,7 @@
@@ -561,8 +558,7 @@
)
(
@ -2213,7 +2214,7 @@ transform = (
source_bucket
if name == source_bucket_name
else storage.Bucket(mock_service, destination_bucket_name)
@@ -570,61 +565,70 @@
@@ -570,61 +566,70 @@
)
(
@ -2316,7 +2317,7 @@ transform = (
)
(
@@ -637,33 +641,37 @@
@@ -637,33 +642,37 @@
(
lambda
# comment
@ -2365,7 +2366,7 @@ transform = (
# comment 3
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(*args, **kwargs)
+ 1 # comment 4
@@ -672,25 +680,28 @@
@@ -672,25 +681,28 @@
)
(
@ -2406,8 +2407,8 @@ transform = (
)
(
@@ -698,9 +709,9 @@
# comemnt 1
@@ -698,9 +710,9 @@
# comment 1
*ergs,
# comment 2
- **kwargs: # comment 3
@ -2419,7 +2420,7 @@ transform = (
)
(
@@ -708,19 +719,20 @@
@@ -708,19 +720,20 @@
# 2
left, # 3
# 4
@ -2450,7 +2451,7 @@ transform = (
)
)
)
@@ -738,48 +750,52 @@
@@ -738,48 +751,52 @@
foo(
lambda from_ts, # but still wrap the body if it gets too long
to_ts,

View File

@ -289,12 +289,7 @@ impl Workspace {
pub fn comments(&self, contents: &str) -> Result<String, Error> {
let parsed = ParsedModule::from_source(contents)?;
let comment_ranges = CommentRanges::from(parsed.parsed.tokens());
let comments = pretty_comments(
parsed.parsed.syntax(),
&comment_ranges,
contents,
self.settings.formatter.preview,
);
let comments = pretty_comments(parsed.parsed.syntax(), &comment_ranges, contents);
Ok(comments)
}