mirror of https://github.com/astral-sh/ruff
re-apply 'pass preview to handle_lambda_comment'
This commit is contained in:
parent
6e9e42d343
commit
33fcca9c53
|
|
@ -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<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);
|
||||
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<Mod>,
|
||||
|
|
@ -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,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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:
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -177,7 +177,12 @@ where
|
|||
&'a N: Into<AnyNodeRef<'a>>,
|
||||
{
|
||||
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<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) -> 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))
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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),
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -289,7 +289,12 @@ 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);
|
||||
let comments = pretty_comments(
|
||||
parsed.parsed.syntax(),
|
||||
&comment_ranges,
|
||||
contents,
|
||||
self.settings.formatter.preview,
|
||||
);
|
||||
Ok(comments)
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue