From 33fcca9c53359b924ea1d0c03bb7ec1c2d595eb0 Mon Sep 17 00:00:00 2001 From: Brent Westbrook Date: Wed, 10 Dec 2025 09:11:00 -0500 Subject: [PATCH] re-apply 'pass preview to handle_lambda_comment' --- .../ruff_python_formatter/src/comments/mod.rs | 17 +++++++++++++---- .../src/comments/placement.rs | 8 ++++++-- .../src/comments/visitor.rs | 11 +++++++++-- crates/ruff_python_formatter/src/lib.rs | 16 +++++++++++++--- crates/ruff_python_formatter/src/range.rs | 7 ++++++- .../src/string/docstring.rs | 7 ++++++- crates/ruff_wasm/src/lib.rs | 7 ++++++- 7 files changed, 59 insertions(+), 14 deletions(-) diff --git a/crates/ruff_python_formatter/src/comments/mod.rs b/crates/ruff_python_formatter/src/comments/mod.rs index 07fc789f34..9c137080e5 100644 --- a/crates/ruff_python_formatter/src/comments/mod.rs +++ b/crates/ruff_python_formatter/src/comments/mod.rs @@ -101,6 +101,7 @@ 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; @@ -248,16 +249,19 @@ impl<'a> Comments<'a> { root: impl Into>, 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); + let mut builder = + CommentsMapBuilder::new(source_code.as_str(), comment_ranges, preview); CommentsVisitor::new(source_code, comment_ranges, &mut builder).visit(root); builder.finish() }; @@ -265,7 +269,7 @@ impl<'a> Comments<'a> { Comments::new(map, comment_ranges) } - collect_comments(root.into(), source_code, comment_ranges) + collect_comments(root.into(), source_code, comment_ranges, preview) } /// Returns `true` if the given `node` has any comments. @@ -520,7 +524,7 @@ mod tests { use ruff_python_parser::{ParseOptions, Parsed, parse}; use ruff_python_trivia::CommentRanges; - use crate::comments::Comments; + use crate::{PreviewMode, comments::Comments}; struct CommentsTestCase<'a> { parsed: Parsed, @@ -544,7 +548,12 @@ mod tests { } fn to_comments(&self) -> Comments<'_> { - Comments::from_ast(self.parsed.syntax(), self.source_code, &self.comment_ranges) + Comments::from_ast( + self.parsed.syntax(), + self.source_code, + &self.comment_ranges, + PreviewMode::Disabled, + ) } } diff --git a/crates/ruff_python_formatter/src/comments/placement.rs b/crates/ruff_python_formatter/src/comments/placement.rs index 76449285be..4a33095f43 100644 --- a/crates/ruff_python_formatter/src/comments/placement.rs +++ b/crates/ruff_python_formatter/src/comments/placement.rs @@ -11,6 +11,7 @@ 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; @@ -24,11 +25,12 @@ 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)) + .or_else(|comment| handle_enclosed_comment(comment, comment_ranges, source, preview)) } /// Handle parenthesized comments. A parenthesized comment is a comment that appears within a @@ -193,6 +195,7 @@ fn handle_enclosed_comment<'a>( comment: DecoratedComment<'a>, comment_ranges: &CommentRanges, source: &str, + preview: PreviewMode, ) -> CommentPlacement<'a> { match comment.enclosing_node() { AnyNodeRef::Parameters(parameters) => { @@ -231,7 +234,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), + AnyNodeRef::ExprLambda(lambda) => handle_lambda_comment(comment, lambda, source, preview), 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)), @@ -1827,6 +1830,7 @@ 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: diff --git a/crates/ruff_python_formatter/src/comments/visitor.rs b/crates/ruff_python_formatter/src/comments/visitor.rs index b9670a3ee8..42e10f4bba 100644 --- a/crates/ruff_python_formatter/src/comments/visitor.rs +++ b/crates/ruff_python_formatter/src/comments/visitor.rs @@ -11,6 +11,7 @@ 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}; @@ -535,11 +536,12 @@ 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); + let placement = place_comment(placement, self.comment_ranges, self.source, self.preview); match placement { CommentPlacement::Leading { node, comment } => { self.push_leading_comment(node, comment); @@ -601,11 +603,16 @@ impl<'a> PushComment<'a> for CommentsMapBuilder<'a> { } impl<'a> CommentsMapBuilder<'a> { - pub(crate) fn new(source: &'a str, comment_ranges: &'a CommentRanges) -> Self { + pub(crate) fn new( + source: &'a str, + comment_ranges: &'a CommentRanges, + preview: PreviewMode, + ) -> Self { Self { comments: CommentsMap::default(), comment_ranges, source, + preview, } } diff --git a/crates/ruff_python_formatter/src/lib.rs b/crates/ruff_python_formatter/src/lib.rs index bf68598e13..c797b67018 100644 --- a/crates/ruff_python_formatter/src/lib.rs +++ b/crates/ruff_python_formatter/src/lib.rs @@ -177,7 +177,12 @@ where &'a N: Into>, { let source_code = SourceCode::new(source); - let comments = Comments::from_ast(parsed.syntax(), source_code, comment_ranges); + let comments = Comments::from_ast( + parsed.syntax(), + source_code, + comment_ranges, + options.preview(), + ); let formatted = format!( PyFormatContext::new(options, source, comments, parsed.tokens()), @@ -213,9 +218,14 @@ pub fn formatted_file(db: &dyn Db, file: File) -> Result, FormatM } /// Public function for generating a printable string of the debug comments. -pub fn pretty_comments(module: &Mod, comment_ranges: &CommentRanges, source: &str) -> String { +pub fn pretty_comments( + module: &Mod, + comment_ranges: &CommentRanges, + source: &str, + preview: PreviewMode, +) -> String { let source_code = SourceCode::new(source); - let comments = Comments::from_ast(module, source_code, comment_ranges); + let comments = Comments::from_ast(module, source_code, comment_ranges, preview); std::format!("{comments:#?}", comments = comments.debug(source_code)) } diff --git a/crates/ruff_python_formatter/src/range.rs b/crates/ruff_python_formatter/src/range.rs index 436c1a12c2..fdbfb64e81 100644 --- a/crates/ruff_python_formatter/src/range.rs +++ b/crates/ruff_python_formatter/src/range.rs @@ -76,7 +76,12 @@ 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); + let comments = Comments::from_ast( + parsed.syntax(), + source_code, + &comment_ranges, + options.preview(), + ); let mut context = PyFormatContext::new( options.with_source_map_generation(SourceMapGeneration::Enabled), diff --git a/crates/ruff_python_formatter/src/string/docstring.rs b/crates/ruff_python_formatter/src/string/docstring.rs index ad357fc65e..e0cb398c48 100644 --- a/crates/ruff_python_formatter/src/string/docstring.rs +++ b/crates/ruff_python_formatter/src/string/docstring.rs @@ -1584,7 +1584,12 @@ 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); + let comments = crate::Comments::from_ast( + parsed.syntax(), + source_code, + &comment_ranges, + options.preview(), + ); let ctx = PyFormatContext::new(options, source, comments, parsed.tokens()) .in_docstring(docstring_quote_style); diff --git a/crates/ruff_wasm/src/lib.rs b/crates/ruff_wasm/src/lib.rs index 6dd49a15bf..c23a510ab3 100644 --- a/crates/ruff_wasm/src/lib.rs +++ b/crates/ruff_wasm/src/lib.rs @@ -289,7 +289,12 @@ impl Workspace { pub fn comments(&self, contents: &str) -> Result { let parsed = ParsedModule::from_source(contents)?; let comment_ranges = CommentRanges::from(parsed.parsed.tokens()); - let comments = pretty_comments(parsed.parsed.syntax(), &comment_ranges, contents); + let comments = pretty_comments( + parsed.parsed.syntax(), + &comment_ranges, + contents, + self.settings.formatter.preview, + ); Ok(comments) }