revert preview and comment placement changes

This commit is contained in:
Brent Westbrook 2025-12-09 16:34:01 -05:00
parent fd34cd6042
commit b823866155
No known key found for this signature in database
8 changed files with 62 additions and 207 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

@ -1,8 +1,7 @@
use ast::helpers::comment_indentation_after; use ast::helpers::comment_indentation_after;
use ruff_python_ast::whitespace::indentation; use ruff_python_ast::whitespace::indentation;
use ruff_python_ast::{ use ruff_python_ast::{
self as ast, AnyNodeRef, Comprehension, Expr, ModModule, Parameter, ParameterWithDefault, self as ast, AnyNodeRef, Comprehension, Expr, ModModule, Parameter, Parameters, StringLike,
Parameters, StringLike,
}; };
use ruff_python_trivia::{ use ruff_python_trivia::{
BackwardsTokenizer, CommentRanges, SimpleToken, SimpleTokenKind, SimpleTokenizer, BackwardsTokenizer, CommentRanges, SimpleToken, SimpleTokenKind, SimpleTokenizer,
@ -12,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;
@ -26,12 +24,11 @@ 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) => {
@ -209,9 +205,6 @@ fn handle_enclosed_comment<'a>(
}) })
} }
AnyNodeRef::Parameter(parameter) => handle_parameter_comment(comment, parameter, source), AnyNodeRef::Parameter(parameter) => handle_parameter_comment(comment, parameter, source),
AnyNodeRef::ParameterWithDefault(parameter) => {
handle_parameter_with_default_comment(comment, parameter, source)
}
AnyNodeRef::Arguments(_) | AnyNodeRef::TypeParams(_) | AnyNodeRef::PatternArguments(_) => { AnyNodeRef::Arguments(_) | AnyNodeRef::TypeParams(_) | AnyNodeRef::PatternArguments(_) => {
handle_bracketed_end_of_line_comment(comment, source) handle_bracketed_end_of_line_comment(comment, source)
} }
@ -238,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)),
@ -871,63 +864,19 @@ fn handle_parameter_comment<'a>(
assert_eq!(colon.kind(), SimpleTokenKind::Colon); assert_eq!(colon.kind(), SimpleTokenKind::Colon);
return if comment.start() < colon.start() { if comment.start() < colon.start() {
// The comment is before the colon, pull it out and make it a leading comment of the parameter. // The comment is before the colon, pull it out and make it a leading comment of the parameter.
CommentPlacement::leading(parameter, comment) CommentPlacement::leading(parameter, comment)
} else { } else {
CommentPlacement::Default(comment) CommentPlacement::Default(comment)
};
}
// If the comment falls before the parameter, and the grandparent node is a lambda (the
// parent node is always `Parameters`), make the comment a dangling lambda parameter, so
// that it can be formatted with the other dangling lambda comments in the lambda header.
//
// This is especially important for niche cases like:
//
// ```py
// (
// lambda # comment 1
// * # comment 2
// x: # comment 3
// x
// )
// ```
//
// where `# comment 2` otherwise becomes a leading comment on `*x` in the first format and a
// dangling comment on the lambda in the second. Instead, make it a dangling comment on the
// enclosing lambda from the start.
match comment.enclosing_grandparent() {
Some(AnyNodeRef::ExprLambda(lambda)) => return CommentPlacement::dangling(lambda, comment),
Some(AnyNodeRef::StmtExpr(ast::StmtExpr { value, .. })) if value.is_lambda_expr() => {
return CommentPlacement::dangling(&**value, comment);
} }
_ => {} } else if comment.start() < parameter.name.start() {
}
if comment.start() < parameter.name.start() {
CommentPlacement::leading(parameter, comment) CommentPlacement::leading(parameter, comment)
} else { } else {
CommentPlacement::Default(comment) CommentPlacement::Default(comment)
} }
} }
/// Handles parameters with defaults inside of lambda expressions by making them dangling comments
/// on the lambda itself.
fn handle_parameter_with_default_comment<'a>(
comment: DecoratedComment<'a>,
_parameter: &'a ParameterWithDefault,
_source: &str,
) -> CommentPlacement<'a> {
match comment.enclosing_grandparent() {
Some(AnyNodeRef::ExprLambda(lambda)) => CommentPlacement::dangling(lambda, comment),
Some(AnyNodeRef::StmtExpr(ast::StmtExpr { value, .. })) if value.is_lambda_expr() => {
CommentPlacement::dangling(&**value, comment)
}
_ => CommentPlacement::Default(comment),
}
}
/// Handles comments between the left side and the operator of a binary expression (trailing comments of the left), /// Handles comments between the left side and the operator of a binary expression (trailing comments of the left),
/// and trailing end-of-line comments that are on the same line as the operator. /// and trailing end-of-line comments that are on the same line as the operator.
/// ///
@ -1865,7 +1814,6 @@ 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:

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};
@ -85,13 +84,11 @@ impl<'ast> SourceOrderVisitor<'ast> for CommentsVisitor<'ast, '_> {
break; break;
} }
let mut parents = self.parents.iter().rev();
let comment = DecoratedComment { let comment = DecoratedComment {
enclosing: enclosing_node, enclosing: enclosing_node,
preceding: self.preceding_node, preceding: self.preceding_node,
following: Some(node), following: Some(node),
parent: parents.nth(1).copied(), parent: self.parents.iter().rev().nth(1).copied(),
grandparent: parents.next().copied(),
line_position: CommentLinePosition::for_range( line_position: CommentLinePosition::for_range(
*comment_range, *comment_range,
self.source_code.as_str(), self.source_code.as_str(),
@ -130,11 +127,9 @@ impl<'ast> SourceOrderVisitor<'ast> for CommentsVisitor<'ast, '_> {
break; break;
} }
let mut parents = self.parents.iter().rev();
let comment = DecoratedComment { let comment = DecoratedComment {
enclosing: node, enclosing: node,
parent: parents.nth(1).copied(), parent: self.parents.last().copied(),
grandparent: parents.next().copied(),
preceding: self.preceding_node, preceding: self.preceding_node,
following: None, following: None,
line_position: CommentLinePosition::for_range( line_position: CommentLinePosition::for_range(
@ -186,7 +181,6 @@ pub(crate) struct DecoratedComment<'a> {
preceding: Option<AnyNodeRef<'a>>, preceding: Option<AnyNodeRef<'a>>,
following: Option<AnyNodeRef<'a>>, following: Option<AnyNodeRef<'a>>,
parent: Option<AnyNodeRef<'a>>, parent: Option<AnyNodeRef<'a>>,
grandparent: Option<AnyNodeRef<'a>>,
line_position: CommentLinePosition, line_position: CommentLinePosition,
slice: SourceCodeSlice, slice: SourceCodeSlice,
} }
@ -217,11 +211,6 @@ impl<'a> DecoratedComment<'a> {
self.parent self.parent
} }
/// Returns the grandparent of the enclosing node, if any
pub(super) fn enclosing_grandparent(&self) -> Option<AnyNodeRef<'a>> {
self.grandparent
}
/// Returns the comment's preceding node. /// Returns the comment's preceding node.
/// ///
/// The direct child node (ignoring lists) of the [`enclosing_node`](DecoratedComment::enclosing_node) that precedes this comment. /// The direct child node (ignoring lists) of the [`enclosing_node`](DecoratedComment::enclosing_node) that precedes this comment.
@ -546,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);
@ -613,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

@ -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

@ -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

@ -894,16 +894,16 @@ lambda a, /, c: a
( (
lambda lambda
# comment 1 # comment 1
*x:
# comment 2 # comment 2
*x:
# comment 3 # comment 3
x x
) )
( (
lambda # comment 1 lambda # comment 1
*x: # comment 2 # comment 2
# comment 3 *x: # comment 3
x x
) )
@ -1500,20 +1500,22 @@ transform = (
) )
( (
lambda *x: # comment 2 lambda
x # comment 2
*x: x
) )
( (
lambda # comment 1 lambda # comment 1
*x: # comment 2 # comment 2
x *x: x
) )
( (
lambda # comment 1 lambda # comment 1
y, *x: # comment 2 y,
x # comment 2
*x: x
) )
( (
@ -1523,13 +1525,15 @@ transform = (
) )
( (
lambda *x, **y: # comment 2 lambda
x # comment 2
*x, **y: x
) )
( (
lambda **x: # comment 1 lambda
x # comment 1
**x: x
) )
``` ```
@ -1626,16 +1630,14 @@ transform = (
) )
( (
@@ -135,17 +119,18 @@ @@ -136,16 +120,18 @@
(
lambda lambda
# comment 1 # comment 1
# comment 2
- *x: - *x:
- # comment 2
- # comment 3 - # comment 3
- x - x
+ *x: ( + *x: (
+ # comment 2
+ # comment 3 + # comment 3
+ x + x
+ ) + )
@ -1643,16 +1645,16 @@ transform = (
( (
lambda # comment 1 lambda # comment 1
- *x: # comment 2 # comment 2
- # comment 3 - *x: # comment 3
- x - x
+ *x: ( # comment 2 # comment 3 + *x: ( # comment 3
+ x + x
+ ) + )
) )
lambda *x: x lambda *x: x
@@ -161,30 +146,34 @@ @@ -161,30 +147,34 @@
) )
( (
@ -1699,7 +1701,7 @@ transform = (
x x
) )
) )
@@ -192,11 +181,12 @@ @@ -192,11 +182,12 @@
( (
lambda # 1 lambda # 1
# 2 # 2
@ -1717,7 +1719,7 @@ transform = (
) )
( (
@@ -204,9 +194,10 @@ @@ -204,9 +195,10 @@
# 2 # 2
x, # 3 x, # 3
# 4 # 4
@ -1731,7 +1733,7 @@ transform = (
) )
( (
@@ -218,71 +209,79 @@ @@ -218,71 +210,79 @@
# Leading # Leading
lambda x: ( lambda x: (
@ -1870,7 +1872,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,
@ -1883,7 +1885,7 @@ transform = (
) )
@@ -302,15 +301,9 @@ @@ -302,15 +302,9 @@
c, c,
d, d,
e, e,
@ -1902,7 +1904,7 @@ transform = (
g=10, g=10,
) )
@@ -320,9 +313,9 @@ @@ -320,9 +314,9 @@
c, c,
d, d,
e, e,
@ -1915,7 +1917,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]] = {
@ -1928,7 +1930,7 @@ transform = (
} }
@@ -352,42 +345,40 @@ @@ -352,42 +346,40 @@
def foo(): def foo():
if True: if True:
if True: if True:
@ -1987,7 +1989,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 (
@@ -402,18 +393,19 @@ @@ -402,18 +394,19 @@
long_assignment_target.with_attribute.and_a_slice[with_an_index] = ( long_assignment_target.with_attribute.and_a_slice[with_an_index] = (
# 1 # 1
# 2 # 2
@ -2014,7 +2016,7 @@ transform = (
) )
very_long_variable_name_x, very_long_variable_name_y = ( very_long_variable_name_x, very_long_variable_name_y = (
@@ -421,8 +413,8 @@ @@ -421,8 +414,8 @@
lambda b: b * another_very_long_expression_here, lambda b: b * another_very_long_expression_here,
) )
@ -2025,7 +2027,7 @@ transform = (
x, more_args, additional_parameters x, more_args, additional_parameters
) )
) )
@@ -458,12 +450,12 @@ @@ -458,12 +451,12 @@
[ [
# Not fluent # Not fluent
param( param(
@ -2040,7 +2042,7 @@ transform = (
), ),
param( param(
lambda left, right: ( lambda left, right: (
@@ -472,9 +464,9 @@ @@ -472,9 +465,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(
@ -2053,7 +2055,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.
@@ -508,16 +500,18 @@ @@ -508,16 +501,18 @@
] ]
# adds parentheses around the body # adds parentheses around the body
@ -2075,7 +2077,7 @@ transform = (
lambda x, y, z: ( lambda x, y, z: (
x + y + z x + y + z
@@ -528,7 +522,7 @@ @@ -528,7 +523,7 @@
x + y + z # trailing eol body x + y + z # trailing eol body
) )
@ -2084,7 +2086,7 @@ transform = (
lambda x, y, z: ( lambda x, y, z: (
# leading body # leading body
@@ -540,21 +534,23 @@ @@ -540,21 +535,23 @@
) )
( (
@ -2118,7 +2120,7 @@ transform = (
# dangling header comment # dangling header comment
source_bucket source_bucket
if name == source_bucket_name if name == source_bucket_name
@@ -562,8 +558,7 @@ @@ -562,8 +559,7 @@
) )
( (
@ -2128,7 +2130,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)
@@ -571,61 +566,70 @@ @@ -571,61 +567,70 @@
) )
( (
@ -2231,7 +2233,7 @@ transform = (
) )
( (
@@ -638,27 +642,31 @@ @@ -638,27 +643,31 @@
( (
lambda lambda
# comment # comment
@ -2271,7 +2273,7 @@ transform = (
) )
( (
@@ -666,19 +674,20 @@ @@ -666,19 +675,20 @@
# 2 # 2
left, # 3 left, # 3
# 4 # 4
@ -2302,7 +2304,7 @@ transform = (
) )
) )
) )
@@ -696,65 +705,72 @@ @@ -696,48 +706,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,
@ -2379,48 +2381,4 @@ transform = (
) )
( (
- lambda *x: # comment 2
- x
+ lambda *x: ( # comment 2
+ x
+ )
)
(
lambda # comment 1
- *x: # comment 2
- x
+ *x: ( # comment 2
+ x
+ )
)
(
lambda # comment 1
- y, *x: # comment 2
- x
+ y, *x: ( # comment 2
+ x
+ )
)
(
@@ -764,11 +780,13 @@
)
(
- lambda *x, **y: # comment 2
- x
+ lambda *x, **y: ( # comment 2
+ x
+ )
)
(
- lambda **x: # comment 1
- x
+ lambda **x: ( # comment 1
+ x
+ )
)
``` ```

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)
} }