Format ExpressionStarred nodes (#5654)

This commit is contained in:
Micha Reiser 2023-07-11 08:08:08 +02:00 committed by GitHub
parent 9f486fa841
commit 987111f5fb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
16 changed files with 192 additions and 581 deletions

View file

@ -87,11 +87,12 @@
//!
//! It is possible to add an additional optional label to [`SourceComment`] If ever the need arises to distinguish two *dangling comments* in the formatting logic,
use ruff_text_size::TextRange;
use std::cell::Cell;
use std::fmt::Debug;
use std::rc::Rc;
use rustpython_parser::ast::Mod;
use rustpython_parser::ast::{Mod, Ranged};
pub(crate) use format::{
dangling_comments, dangling_node_comments, leading_alternate_branch_comments, leading_comments,
@ -114,7 +115,7 @@ mod placement;
mod visitor;
/// A comment in the source document.
#[derive(Debug, Clone)]
#[derive(Debug, Clone, Eq, PartialEq)]
pub(crate) struct SourceComment {
/// The location of the comment in the source document.
slice: SourceCodeSlice,
@ -155,15 +156,20 @@ impl SourceComment {
pub(crate) fn is_unformatted(&self) -> bool {
!self.is_formatted()
}
}
impl SourceComment {
/// Returns a nice debug representation that prints the source code for every comment (and not just the range).
pub(crate) fn debug<'a>(&'a self, source_code: SourceCode<'a>) -> DebugComment<'a> {
DebugComment::new(self, source_code)
}
}
impl Ranged for SourceComment {
#[inline]
fn range(&self) -> TextRange {
self.slice.range()
}
}
/// The position of a comment in the source text.
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub(crate) enum CommentLinePosition {

View file

@ -38,6 +38,7 @@ pub(super) fn place_comment<'a>(
handle_slice_comments,
handle_attribute_comment,
handle_expr_if_comment,
handle_trailing_expression_starred_star_end_of_line_comment,
];
for handler in HANDLERS {
comment = match handler(comment, locator) {
@ -1215,6 +1216,21 @@ fn handle_expr_if_comment<'a>(
CommentPlacement::Default(comment)
}
fn handle_trailing_expression_starred_star_end_of_line_comment<'a>(
comment: DecoratedComment<'a>,
_locator: &Locator,
) -> CommentPlacement<'a> {
if comment.line_position().is_own_line() || comment.following_node().is_none() {
return CommentPlacement::Default(comment);
}
let AnyNodeRef::ExprStarred(starred) = comment.enclosing_node() else {
return CommentPlacement::Default(comment);
};
CommentPlacement::leading(starred.as_any_node_ref(), comment)
}
/// Looks for a token in the range that contains no other tokens except for parentheses outside
/// the expression ranges
fn find_only_token_in_range(range: TextRange, locator: &Locator, token_kind: TokenKind) -> Token {

View file

@ -454,6 +454,13 @@ impl<'a> DecoratedComment<'a> {
}
}
impl Ranged for DecoratedComment<'_> {
#[inline]
fn range(&self) -> TextRange {
self.slice.range()
}
}
impl From<DecoratedComment<'_>> for SourceComment {
fn from(decorated: DecoratedComment) -> Self {
Self::new(decorated.slice, decorated.line_position)

View file

@ -1,22 +1,32 @@
use rustpython_parser::ast::ExprStarred;
use ruff_formatter::write;
use crate::comments::Comments;
use crate::expression::parentheses::{
default_expression_needs_parentheses, NeedsParentheses, Parentheses, Parenthesize,
};
use crate::{not_yet_implemented_custom_text, FormatNodeRule, PyFormatter};
use ruff_formatter::{write, Buffer, FormatResult};
use rustpython_parser::ast::ExprStarred;
use crate::prelude::*;
use crate::FormatNodeRule;
#[derive(Default)]
pub struct FormatExprStarred;
impl FormatNodeRule<ExprStarred> for FormatExprStarred {
fn fmt_fields(&self, _item: &ExprStarred, f: &mut PyFormatter) -> FormatResult<()> {
write!(
f,
[not_yet_implemented_custom_text(
"*NOT_YET_IMPLEMENTED_ExprStarred"
)]
)
fn fmt_fields(&self, item: &ExprStarred, f: &mut PyFormatter) -> FormatResult<()> {
let ExprStarred {
range: _,
value,
ctx: _,
} = item;
write!(f, [text("*"), value.format()])
}
fn fmt_dangling_comments(&self, node: &ExprStarred, f: &mut PyFormatter) -> FormatResult<()> {
debug_assert_eq!(f.context().comments().dangling_comments(node), []);
Ok(())
}
}