Skip to content

Commit

Permalink
Add partial implementation of can_omit_optional_parentheses
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaReiser committed Jul 10, 2023
1 parent 14919f2 commit aa6915d
Show file tree
Hide file tree
Showing 12 changed files with 349 additions and 221 deletions.
17 changes: 14 additions & 3 deletions crates/ruff_python_formatter/src/comments/placement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1215,11 +1215,22 @@ fn handle_expr_if_comment<'a>(
CommentPlacement::Default(comment)
}

/// Looks for a token in the range that contains no other tokens.
/// Looks for a token in the range that contains no other tokens than closing parentheses.
fn find_only_token_in_range(range: TextRange, locator: &Locator, token_kind: TokenKind) -> Token {
let mut tokens = SimpleTokenizer::new(locator.contents(), range).skip_trivia();
let token = tokens.next().expect("Expected a token");
debug_assert_eq!(token.kind(), token_kind);
let token = loop {
if let Some(token) = tokens.next() {
if token.kind() == token_kind {
break token;
} else if token.kind() == TokenKind::RParen {
// skip over right parens
} else {
unreachable!("Expected {token_kind:?} but found {token:?})");
}
} else {
unreachable!("Expected a {token_kind:?} token but found none");
};
};
debug_assert_eq!(tokens.next(), None);
token
}
Expand Down
12 changes: 7 additions & 5 deletions crates/ruff_python_formatter/src/expression/expr_if_exp.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use crate::comments::{leading_comments, Comments};
use crate::expression::parentheses::{
default_expression_needs_parentheses, NeedsParentheses, Parentheses, Parenthesize,
default_expression_needs_parentheses, in_parentheses_only_group, NeedsParentheses, Parentheses,
Parenthesize,
};
use crate::{AsFormat, FormatNodeRule, PyFormatter};
use ruff_formatter::prelude::{group, soft_line_break_or_space, space, text};
use ruff_formatter::{format_args, write, Buffer, FormatResult};
use crate::prelude::*;
use crate::FormatNodeRule;
use ruff_formatter::{format_args, write};
use rustpython_parser::ast::ExprIfExp;

#[derive(Default)]
Expand All @@ -19,12 +20,13 @@ impl FormatNodeRule<ExprIfExp> for FormatExprIfExp {
orelse,
} = item;
let comments = f.context().comments().clone();

// We place `if test` and `else orelse` on a single line, so the `test` and `orelse` leading
// comments go on the line before the `if` or `else` instead of directly ahead `test` or
// `orelse`
write!(
f,
[group(&format_args![
[in_parentheses_only_group(&format_args![
body.format(),
soft_line_break_or_space(),
leading_comments(comments.leading_comments(test.as_ref())),
Expand Down
30 changes: 22 additions & 8 deletions crates/ruff_python_formatter/src/expression/expr_subscript.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
use rustpython_parser::ast::ExprSubscript;

use ruff_formatter::{format_args, write};
use ruff_python_ast::node::AstNode;

use crate::comments::{trailing_comments, Comments};
use crate::context::NodeLevel;
use crate::expression::parentheses::{
default_expression_needs_parentheses, NeedsParentheses, Parentheses, Parenthesize,
default_expression_needs_parentheses, in_parentheses_only_group, NeedsParentheses, Parentheses,
Parenthesize,
};
use crate::{AsFormat, FormatNodeRule, PyFormatter};
use ruff_formatter::prelude::{group, soft_block_indent, text};
use ruff_formatter::{format_args, write, Buffer, FormatResult};
use ruff_python_ast::node::AstNode;
use rustpython_parser::ast::ExprSubscript;
use crate::prelude::*;
use crate::FormatNodeRule;

#[derive(Default)]
pub struct FormatExprSubscript;
Expand All @@ -27,10 +31,20 @@ impl FormatNodeRule<ExprSubscript> for FormatExprSubscript {
"The subscript expression must have at most a single comment, the one after the bracket"
);

if let NodeLevel::Expression(Some(group_id)) = f.context().node_level() {
// Enforce the optional parentheses for parenthesized values.
f.context_mut().set_node_level(NodeLevel::Expression(None));
let result = value.format().fmt(f);
f.context_mut()
.set_node_level(NodeLevel::Expression(Some(group_id)));
result?;
} else {
value.format().fmt(f)?;
}

write!(
f,
[group(&format_args![
value.format(),
[in_parentheses_only_group(&format_args![
text("["),
trailing_comments(dangling_comments),
soft_block_indent(&slice.format()),
Expand Down
Loading

0 comments on commit aa6915d

Please sign in to comment.