Skip to content

Commit

Permalink
Discard changes to crates/ruff_python_formatter/src/lib.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaReiser committed Sep 11, 2023
1 parent 3725802 commit dc2c8dc
Show file tree
Hide file tree
Showing 9 changed files with 93 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -334,3 +334,27 @@
+ header.isstdcnt # Standard/wall indicators
+ header.isutcnt # UT/local indicators
)


if (
(1 + 2) # test
or (3 + 4) # other
or (4 + 5) # more
):
pass


if (
(1 and 2) # test
+ (3 and 4) # other
+ (4 and 5) # more
):
pass


if (
(1 + 2) # test
< (3 + 4) # other
> (4 + 5) # more
):
pass
7 changes: 4 additions & 3 deletions crates/ruff_python_formatter/src/comments/placement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,9 @@ fn handle_enclosed_comment<'a>(
locator,
)
}
AnyNodeRef::ExprBoolOp(_) => handle_trailing_bool_expression_comment(comment, locator),
AnyNodeRef::ExprCompare(_) => handle_trailing_bool_expression_comment(comment, locator),
AnyNodeRef::ExprBoolOp(_) | AnyNodeRef::ExprCompare(_) => {
handle_trailing_binary_like_comment(comment, locator)
}
AnyNodeRef::Keyword(keyword) => handle_keyword_comment(comment, keyword, locator),
AnyNodeRef::PatternKeyword(pattern_keyword) => {
handle_pattern_keyword_comment(comment, pattern_keyword, locator)
Expand Down Expand Up @@ -848,7 +849,7 @@ fn handle_trailing_binary_expression_left_or_operator_comment<'a>(
/// and 3 == 3
/// )
/// ```
fn handle_trailing_bool_expression_comment<'a>(
fn handle_trailing_binary_like_comment<'a>(
comment: DecoratedComment<'a>,
locator: &Locator,
) -> CommentPlacement<'a> {
Expand Down
16 changes: 8 additions & 8 deletions crates/ruff_python_formatter/src/expression/binary_like.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ use crate::prelude::*;

#[derive(Copy, Clone, Debug)]
pub(super) enum BinaryLike<'a> {
BinaryExpression(&'a ExprBinOp),
CompareExpression(&'a ExprCompare),
BoolExpression(&'a ExprBoolOp),
Binary(&'a ExprBinOp),
Compare(&'a ExprCompare),
Bool(&'a ExprBoolOp),
}

impl<'a> BinaryLike<'a> {
Expand Down Expand Up @@ -102,7 +102,7 @@ impl<'a> BinaryLike<'a> {
if let Some((left, rest)) = bool_expression.values.split_first() {
rec(
Operand::Left {
expression: &left,
expression: left,
leading_comments,
},
comments,
Expand Down Expand Up @@ -245,15 +245,15 @@ impl<'a> BinaryLike<'a> {

let mut parts = SmallVec::new();
match self {
BinaryLike::BinaryExpression(binary) => {
BinaryLike::Binary(binary) => {
// Leading and trailing comments are handled by the binary's ``FormatNodeRule` implementation.
recurse_binary(binary, &[], &[], comments, source, &mut parts);
}
BinaryLike::CompareExpression(compare) => {
BinaryLike::Compare(compare) => {
// Leading and trailing comments are handled by the compare's ``FormatNodeRule` implementation.
recurse_compare(compare, &[], &[], comments, source, &mut parts);
}
BinaryLike::BoolExpression(bool) => {
BinaryLike::Bool(bool) => {
recurse_bool(bool, &[], &[], comments, source, &mut parts);
}
}
Expand All @@ -262,7 +262,7 @@ impl<'a> BinaryLike<'a> {
}

const fn is_bool_op(self) -> bool {
matches!(self, BinaryLike::BoolExpression(_))
matches!(self, BinaryLike::Bool(_))
}
}

Expand Down
2 changes: 1 addition & 1 deletion crates/ruff_python_formatter/src/expression/expr_bin_op.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub struct FormatExprBinOp;
impl FormatNodeRule<ExprBinOp> for FormatExprBinOp {
#[inline]
fn fmt_fields(&self, item: &ExprBinOp, f: &mut PyFormatter) -> FormatResult<()> {
BinaryLike::BinaryExpression(item).fmt(f)
BinaryLike::Binary(item).fmt(f)
}

fn fmt_dangling_comments(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub struct FormatExprBoolOp;
impl FormatNodeRule<ExprBoolOp> for FormatExprBoolOp {
#[inline]
fn fmt_fields(&self, item: &ExprBoolOp, f: &mut PyFormatter) -> FormatResult<()> {
BinaryLike::BoolExpression(item).fmt(f)
BinaryLike::Bool(item).fmt(f)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub struct FormatExprCompare;
impl FormatNodeRule<ExprCompare> for FormatExprCompare {
#[inline]
fn fmt_fields(&self, item: &ExprCompare, f: &mut PyFormatter) -> FormatResult<()> {
BinaryLike::CompareExpression(item).fmt(f)
BinaryLike::Compare(item).fmt(f)
}

fn fmt_dangling_comments(
Expand Down
10 changes: 0 additions & 10 deletions crates/ruff_python_formatter/src/expression/expr_subscript.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,16 +69,6 @@ impl FormatNodeRule<ExprSubscript> for FormatExprSubscript {
parenthesized("[", &format_slice, "]")
.with_dangling_comments(dangling_comments)
.fmt(f)

// write!(
// f,
// [group(&format_args![
// token("["),
// trailing_comments(dangling_comments),
// soft_block_indent(&format_slice),
// token("]")
// ])]
// )
}

fn fmt_dangling_comments(
Expand Down
18 changes: 6 additions & 12 deletions crates/ruff_python_formatter/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,18 +216,12 @@ if True:
#[test]
fn quick_test() {
let src = r#"
return (
# pypiserver (https://pypi.org/project/pypiserver)
status == 409
# PyPI / TestPyPI / GCP Artifact Registry
or (status == 400 and any("already exist" in x for x in [reason, text]))
# Nexus Repository OSS (https://www.sonatype.com/nexus-repository-oss)
or (status == 400 and any("updating asset" in x for x in [reason, text]))
# Artifactory (https://jfrog.com/artifactory/)
or (status == 403 and "overwrite artifact" in text)
# Gitlab Enterprise Edition (https://about.gitlab.com)
or (status == 400 and "already been taken" in text)
)
(header.timecnt * 5 # Transition times and types
+ header.typecnt * 6 # Local time type records
+ header.charcnt # Time zone designations
+ header.leapcnt * 8 # Leap second records
+ header.isstdcnt # Standard/wall indicators
+ header.isutcnt) # UT/local indicators
"#;
// Tokenize once
let mut tokens = Vec::new();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,30 @@ skip_bytes = (
+ header.isstdcnt # Standard/wall indicators
+ header.isutcnt # UT/local indicators
)
if (
(1 + 2) # test
or (3 + 4) # other
or (4 + 5) # more
):
pass
if (
(1 and 2) # test
+ (3 and 4) # other
+ (4 and 5) # more
):
pass
if (
(1 + 2) # test
< (3 + 4) # other
> (4 + 5) # more
):
pass
```

## Output
Expand Down Expand Up @@ -737,6 +761,30 @@ skip_bytes = (
+ header.isstdcnt # Standard/wall indicators
+ header.isutcnt # UT/local indicators
)
if (
(1 + 2) # test
or (3 + 4) # other
or (4 + 5) # more
):
pass
if (
(1 and 2) # test
+ (3 and 4) # other
+ (4 and 5) # more
):
pass
if (
(1 + 2) # test
< (3 + 4) # other
> (4 + 5) # more
):
pass
```


Expand Down

0 comments on commit dc2c8dc

Please sign in to comment.