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

View File

@ -11,7 +11,6 @@ use ruff_source_file::LineRanges;
use ruff_text_size::{Ranged, TextLen, TextRange, TextSize}; use ruff_text_size::{Ranged, TextLen, TextRange, TextSize};
use std::cmp::Ordering; use std::cmp::Ordering;
use crate::PreviewMode;
use crate::comments::visitor::{CommentPlacement, DecoratedComment}; use crate::comments::visitor::{CommentPlacement, DecoratedComment};
use crate::expression::expr_slice::{ExprSliceCommentSection, assign_comment_in_slice}; use crate::expression::expr_slice::{ExprSliceCommentSection, assign_comment_in_slice};
use crate::expression::parentheses::is_expression_parenthesized; use crate::expression::parentheses::is_expression_parenthesized;
@ -19,19 +18,17 @@ use crate::other::parameters::{
assign_argument_separator_comment_placement, find_parameter_separators, assign_argument_separator_comment_placement, find_parameter_separators,
}; };
use crate::pattern::pattern_match_sequence::SequenceType; 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. /// Manually attach comments to nodes that the default placement gets wrong.
pub(super) fn place_comment<'a>( pub(super) fn place_comment<'a>(
comment: DecoratedComment<'a>, comment: DecoratedComment<'a>,
comment_ranges: &CommentRanges, comment_ranges: &CommentRanges,
source: &str, source: &str,
preview: PreviewMode,
) -> CommentPlacement<'a> { ) -> CommentPlacement<'a> {
handle_parenthesized_comment(comment, source) handle_parenthesized_comment(comment, source)
.or_else(|comment| handle_end_of_line_comment_around_body(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_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 /// 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: DecoratedComment<'a>,
comment_ranges: &CommentRanges, comment_ranges: &CommentRanges,
source: &str, source: &str,
preview: PreviewMode,
) -> CommentPlacement<'a> { ) -> CommentPlacement<'a> {
match comment.enclosing_node() { match comment.enclosing_node() {
AnyNodeRef::Parameters(parameters) => { 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::ExprUnaryOp(unary_op) => handle_unary_op_comment(comment, unary_op, source),
AnyNodeRef::ExprNamed(_) => handle_named_expr_comment(comment, 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) AnyNodeRef::ExprDict(_) => handle_dict_unpacking_comment(comment, source)
.or_else(|comment| handle_bracketed_end_of_line_comment(comment, source)) .or_else(|comment| handle_bracketed_end_of_line_comment(comment, source))
.or_else(|comment| handle_key_value_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. /// 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 /// ```python
/// ( /// (
/// lambda # dangling lambda /// lambda # 1
/// # leading parameters /// # 2
/// x /// x
/// : # dangling lambda /// : # 3
/// # dangling lambda /// # 4
/// y /// 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 /// For non-parameterized lambdas, all comments before the body are considered dangling, as is the
/// case for all of the following: /// case for all of the following:
/// ///
@ -1841,25 +1823,10 @@ fn handle_named_expr_comment<'a>(
/// y /// 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>( fn handle_lambda_comment<'a>(
comment: DecoratedComment<'a>, comment: DecoratedComment<'a>,
lambda: &'a ast::ExprLambda, lambda: &'a ast::ExprLambda,
source: &str, source: &str,
preview: PreviewMode,
) -> CommentPlacement<'a> { ) -> CommentPlacement<'a> {
if let Some(parameters) = lambda.parameters.as_deref() { if let Some(parameters) = lambda.parameters.as_deref() {
// End-of-line comments between the `lambda` and the parameters are dangling on the lambda: // 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 // 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() { if parameters.end() < comment.start() && comment.start() < lambda.body.start() {
return if is_parenthesize_lambda_bodies_enabled_preview(preview) { // If the value is parenthesized, and the comment is within the parentheses, it should
CommentPlacement::leading(&*lambda.body, comment) // be a leading comment on the value, not a dangling comment in the lambda, as in:
} else { // ```python
// 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: // lambda x: ( # comment
// ```python // y
// ( // )
// lambda x: ( # comment // )
// y // ```
// ) let tokenizer =
// ) SimpleTokenizer::new(source, TextRange::new(parameters.end(), comment.start()));
// ``` if tokenizer
let tokenizer = .skip_trivia()
SimpleTokenizer::new(source, TextRange::new(parameters.end(), comment.start())); .any(|token| token.kind == SimpleTokenKind::LParen)
if tokenizer {
.skip_trivia() return CommentPlacement::Default(comment);
.any(|token| token.kind == SimpleTokenKind::LParen) }
{
return CommentPlacement::Default(comment);
}
CommentPlacement::dangling(comment.enclosing_node(), comment) return CommentPlacement::dangling(comment.enclosing_node(), comment);
};
} }
} else { } else {
// Comments between the lambda and the body are dangling on the lambda: // Comments between the lambda and the body are dangling on the lambda:
@ -1930,32 +1891,26 @@ fn handle_lambda_comment<'a>(
// y // y
// ) // )
// ``` // ```
// Except in preview, where they become leading on the body instead, regardless of
// parenthesization.
if comment.start() < lambda.body.start() { if comment.start() < lambda.body.start() {
return if is_parenthesize_lambda_bodies_enabled_preview(preview) { // If the value is parenthesized, and the comment is within the parentheses, it should
CommentPlacement::leading(&*lambda.body, comment) // be a leading comment on the value, not a dangling comment in the lambda, as in:
} else { // ```python
// 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: // lambda: ( # comment
// ```python // y
// ( // )
// lambda: ( # comment // )
// y // ```
// ) let tokenizer =
// ) SimpleTokenizer::new(source, TextRange::new(lambda.start(), comment.start()));
// ``` if tokenizer
let tokenizer = .skip_trivia()
SimpleTokenizer::new(source, TextRange::new(lambda.start(), comment.start())); .any(|token| token.kind == SimpleTokenKind::LParen)
if tokenizer {
.skip_trivia() return CommentPlacement::Default(comment);
.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_python_trivia::{CommentLinePosition, CommentRanges};
use ruff_text_size::{Ranged, TextRange, TextSize}; use ruff_text_size::{Ranged, TextRange, TextSize};
use crate::PreviewMode;
use crate::comments::node_key::NodeRefEqualityKey; use crate::comments::node_key::NodeRefEqualityKey;
use crate::comments::placement::place_comment; use crate::comments::placement::place_comment;
use crate::comments::{CommentsMap, SourceComment}; use crate::comments::{CommentsMap, SourceComment};
@ -536,12 +535,11 @@ pub(super) struct CommentsMapBuilder<'a> {
/// We need those for backwards lexing /// We need those for backwards lexing
comment_ranges: &'a CommentRanges, comment_ranges: &'a CommentRanges,
source: &'a str, source: &'a str,
preview: PreviewMode,
} }
impl<'a> PushComment<'a> for CommentsMapBuilder<'a> { impl<'a> PushComment<'a> for CommentsMapBuilder<'a> {
fn push_comment(&mut self, placement: DecoratedComment<'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 { match placement {
CommentPlacement::Leading { node, comment } => { CommentPlacement::Leading { node, comment } => {
self.push_leading_comment(node, comment); self.push_leading_comment(node, comment);
@ -603,16 +601,11 @@ impl<'a> PushComment<'a> for CommentsMapBuilder<'a> {
} }
impl<'a> CommentsMapBuilder<'a> { impl<'a> CommentsMapBuilder<'a> {
pub(crate) fn new( pub(crate) fn new(source: &'a str, comment_ranges: &'a CommentRanges) -> Self {
source: &'a str,
comment_ranges: &'a CommentRanges,
preview: PreviewMode,
) -> Self {
Self { Self {
comments: CommentsMap::default(), comments: CommentsMap::default(),
comment_ranges, comment_ranges,
source, source,
preview,
} }
} }

View File

@ -127,6 +127,51 @@ impl FormatNodeRule<ExprLambda> for FormatExprLambda {
if dangling_after_parameters.is_empty() { if dangling_after_parameters.is_empty() {
write!(f, [space()])?; 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 { } else {
write!(f, [dangling_comments(dangling_after_parameters)])?; 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. // In this context, a dangling comment is a comment between the `lambda` and the body.
if dangling.is_empty() { if dangling.is_empty() {
write!(f, [space()])?; 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 { } else {
write!(f, [dangling_comments(dangling)])?; 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 // ensures that we correctly handle parenthesized comments, and don't need to worry
// about them in the implementation below. // about them in the implementation below.
if body_comments.has_leading() || body_comments.has_trailing_own_line() { 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.format().with_options(Parentheses::Always).fmt(f)
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(")")
]
)
} }
// Calls and subscripts require special formatting because they have their own // Calls and subscripts require special formatting because they have their own
// parentheses, but they can also have an arbitrary amount of text before the // 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>>, &'a N: Into<AnyNodeRef<'a>>,
{ {
let source_code = SourceCode::new(source); let source_code = SourceCode::new(source);
let comments = Comments::from_ast( let comments = Comments::from_ast(parsed.syntax(), source_code, comment_ranges);
parsed.syntax(),
source_code,
comment_ranges,
options.preview(),
);
let formatted = format!( let formatted = format!(
PyFormatContext::new(options, source, comments, parsed.tokens()), 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. /// Public function for generating a printable string of the debug comments.
pub fn pretty_comments( pub fn pretty_comments(module: &Mod, comment_ranges: &CommentRanges, source: &str) -> String {
module: &Mod,
comment_ranges: &CommentRanges,
source: &str,
preview: PreviewMode,
) -> String {
let source_code = SourceCode::new(source); 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)) 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: //! 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. //! 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. /// 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( 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 { pub(crate) const fn is_parenthesize_lambda_bodies_enabled(context: &PyFormatContext) -> bool {
context.is_preview() 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 parsed = parse(source, ParseOptions::from(options.source_type()))?;
let source_code = SourceCode::new(source); let source_code = SourceCode::new(source);
let comment_ranges = CommentRanges::from(parsed.tokens()); let comment_ranges = CommentRanges::from(parsed.tokens());
let comments = Comments::from_ast( let comments = Comments::from_ast(parsed.syntax(), source_code, &comment_ranges);
parsed.syntax(),
source_code,
&comment_ranges,
options.preview(),
);
let mut context = PyFormatContext::new( let mut context = PyFormatContext::new(
options.with_source_map_generation(SourceMapGeneration::Enabled), 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 parsed = ruff_python_parser::parse(source, ParseOptions::from(source_type))?;
let comment_ranges = CommentRanges::from(parsed.tokens()); let comment_ranges = CommentRanges::from(parsed.tokens());
let source_code = ruff_formatter::SourceCode::new(source); let source_code = ruff_formatter::SourceCode::new(source);
let comments = crate::Comments::from_ast( let comments = crate::Comments::from_ast(parsed.syntax(), source_code, &comment_ranges);
parsed.syntax(),
source_code,
&comment_ranges,
options.preview(),
);
let ctx = PyFormatContext::new(options, source, comments, parsed.tokens()) let ctx = PyFormatContext::new(options, source, comments, parsed.tokens())
.in_docstring(docstring_quote_style); .in_docstring(docstring_quote_style);

View File

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

View File

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