Format raise statement (#5595)

## Summary

This PR implements the formatting of `raise` statements. I haven't
looked at the black implementation, this is inspired from from the
`return` statements formatting.

## Test Plan

The black differences with insta.

I also compared manually some edge cases with very long string and call
chaining and it seems to do the same formatting as black.

There is one issue:
```python
# input

raise OsError(
    "aksjdhflsakhdflkjsadlfajkslhfdkjsaldajlahflashdfljahlfksajlhfajfjfsaahflakjslhdfkjalhdskjfa"
) from a.aaaaa(aksjdhflsakhdflkjsadlfajkslhfdkjsaldajlahflashdfljahlfksajlhfajfjfsaahflakjslhdfkjalhdskjfa).a(aaaa)


# black

raise OsError(
    "aksjdhflsakhdflkjsadlfajkslhfdkjsaldajlahflashdfljahlfksajlhfajfjfsaahflakjslhdfkjalhdskjfa"
) from a.aaaaa(
    aksjdhflsakhdflkjsadlfajkslhfdkjsaldajlahflashdfljahlfksajlhfajfjfsaahflakjslhdfkjalhdskjfa
).a(
    aaaa
)


# ruff

raise OsError(
    "aksjdhflsakhdflkjsadlfajkslhfdkjsaldajlahflashdfljahlfksajlhfajfjfsaahflakjslhdfkjalhdskjfa"
) from a.aaaaa(
    aksjdhflsakhdflkjsadlfajkslhfdkjsaldajlahflashdfljahlfksajlhfajfjfsaahflakjslhdfkjalhdskjfa
).a(aaaa)
```

But I'm not sure this diff is the raise formatting implementation.

---------

Co-authored-by: Louis Dispa <ldispa@deezer.com>
This commit is contained in:
Louis Dispa 2023-07-10 21:23:49 +02:00 committed by GitHub
parent 93bfa239b7
commit e7e2f44440
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 409 additions and 130 deletions

View file

@ -211,7 +211,7 @@ type CommentsMap<'a> = MultiMap<NodeRefEqualityKey<'a>, SourceComment>;
/// The comments of a syntax tree stored by node.
///
/// Cloning `comments` is cheap as it only involves bumping a reference counter.
#[derive(Clone, Default)]
#[derive(Debug, Clone, Default)]
pub(crate) struct Comments<'a> {
/// The implementation uses an [Rc] so that [Comments] has a lifetime independent from the [crate::Formatter].
/// Independent lifetimes are necessary to support the use case where a (formattable object)[crate::Format]
@ -400,7 +400,7 @@ impl<'a> Comments<'a> {
}
}
#[derive(Default)]
#[derive(Debug, Default)]
struct CommentsData<'a> {
comments: CommentsMap<'a>,
}

View file

@ -1,5 +1,5 @@
use crate::builders::optional_parentheses;
use crate::comments::{dangling_node_comments, Comments};
use crate::comments::{dangling_comments, CommentLinePosition, Comments};
use crate::expression::parentheses::{
default_expression_needs_parentheses, parenthesized, NeedsParentheses, Parentheses,
Parenthesize,
@ -57,16 +57,33 @@ impl FormatNodeRule<ExprTuple> for FormatExprTuple {
} = item;
// Handle the edge cases of an empty tuple and a tuple with one element
//
// there can be dangling comments, and they can be in two
// positions:
// ```python
// a3 = ( # end-of-line
// # own line
// )
// ```
// In all other cases comments get assigned to a list element
match elts.as_slice() {
[] => {
let comments = f.context().comments().clone();
let dangling = comments.dangling_comments(item);
let end_of_line_split = dangling.partition_point(|comment| {
comment.line_position() == CommentLinePosition::EndOfLine
});
debug_assert!(dangling[end_of_line_split..]
.iter()
.all(|comment| comment.line_position() == CommentLinePosition::OwnLine));
write!(
f,
[
// An empty tuple always needs parentheses, but does not have a comma
&text("("),
block_indent(&dangling_node_comments(item)),
&text(")"),
]
[group(&format_args![
text("("),
dangling_comments(&dangling[..end_of_line_split]),
soft_block_indent(&dangling_comments(&dangling[end_of_line_split..])),
text(")")
])]
)
}
[single] => {

View file

@ -1,5 +1,8 @@
use crate::{not_yet_implemented, FormatNodeRule, PyFormatter};
use ruff_formatter::{write, Buffer, FormatResult};
use crate::expression::parentheses::Parenthesize;
use crate::{AsFormat, FormatNodeRule, PyFormatter};
use ruff_formatter::prelude::{space, text};
use ruff_formatter::{write, Buffer, Format, FormatResult};
use rustpython_parser::ast::StmtRaise;
#[derive(Default)]
@ -7,6 +10,32 @@ pub struct FormatStmtRaise;
impl FormatNodeRule<StmtRaise> for FormatStmtRaise {
fn fmt_fields(&self, item: &StmtRaise, f: &mut PyFormatter) -> FormatResult<()> {
write!(f, [not_yet_implemented(item)])
let StmtRaise {
range: _,
exc,
cause,
} = item;
text("raise").fmt(f)?;
if let Some(value) = exc {
write!(
f,
[space(), value.format().with_options(Parenthesize::Optional)]
)?;
}
if let Some(value) = cause {
write!(
f,
[
space(),
text("from"),
space(),
value.format().with_options(Parenthesize::Optional)
]
)?;
}
Ok(())
}
}