Skip to content

Commit

Permalink
Format class definitions (#5289)
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaReiser committed Jun 22, 2023
1 parent 7d4f8e5 commit f7e1cf4
Show file tree
Hide file tree
Showing 27 changed files with 914 additions and 541 deletions.
12 changes: 6 additions & 6 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions Cargo.toml
Expand Up @@ -50,15 +50,15 @@ toml = { version = "0.7.2" }
# v0.0.1
libcst = { git = "https://github.com/charliermarsh/LibCST", rev = "80e4c1399f95e5beb532fdd1e209ad2dbb470438" }
# v0.0.3
ruff_text_size = { git = "https://github.com/astral-sh/RustPython-Parser.git", rev = "08ebbe40d7776cac6e3ba66277d435056f2b8dca" }
ruff_text_size = { git = "https://github.com/astral-sh/RustPython-Parser.git", rev = "f60e204b73b95bdb6ce87ccd0de34081b4a17c11" }
# v0.0.3
rustpython-ast = { git = "https://github.com/astral-sh/RustPython-Parser.git", rev = "08ebbe40d7776cac6e3ba66277d435056f2b8dca" , default-features = false, features = ["all-nodes-with-ranges", "num-bigint"]}
rustpython-ast = { git = "https://github.com/astral-sh/RustPython-Parser.git", rev = "f60e204b73b95bdb6ce87ccd0de34081b4a17c11" , default-features = false, features = ["all-nodes-with-ranges", "num-bigint"]}
# v0.0.3
rustpython-format = { git = "https://github.com/astral-sh/RustPython-Parser.git", rev = "08ebbe40d7776cac6e3ba66277d435056f2b8dca", default-features = false, features = ["num-bigint"] }
rustpython-format = { git = "https://github.com/astral-sh/RustPython-Parser.git", rev = "f60e204b73b95bdb6ce87ccd0de34081b4a17c11", default-features = false, features = ["num-bigint"] }
# v0.0.3
rustpython-literal = { git = "https://github.com/astral-sh/RustPython-Parser.git", rev = "08ebbe40d7776cac6e3ba66277d435056f2b8dca", default-features = false }
rustpython-literal = { git = "https://github.com/astral-sh/RustPython-Parser.git", rev = "f60e204b73b95bdb6ce87ccd0de34081b4a17c11", default-features = false }
# v0.0.3
rustpython-parser = { git = "https://github.com/astral-sh/RustPython-Parser.git", rev = "08ebbe40d7776cac6e3ba66277d435056f2b8dca" , default-features = false, features = ["full-lexer", "all-nodes-with-ranges", "num-bigint"] }
rustpython-parser = { git = "https://github.com/astral-sh/RustPython-Parser.git", rev = "f60e204b73b95bdb6ce87ccd0de34081b4a17c11" , default-features = false, features = ["full-lexer", "all-nodes-with-ranges", "num-bigint"] }

[profile.release]
lto = "fat"
Expand Down
@@ -0,0 +1,36 @@
class Test(
Aaaaaaaaaaaaaaaaa,
Bbbbbbbbbbbbbbbb,
DDDDDDDDDDDDDDDD,
EEEEEEEEEEEEEE,
metaclass=meta,
):
pass


class Test((Aaaaaaaaaaaaaaaaa), Bbbbbbbbbbbbbbbb, metaclass=meta):
pass

class Test( # trailing class comment
Aaaaaaaaaaaaaaaaa, # trailing comment

# in between comment

Bbbbbbbbbbbbbbbb,
# another leading comment
DDDDDDDDDDDDDDDD,
EEEEEEEEEEEEEE,
# meta comment
metaclass=meta, # trailing meta comment
):
pass

class Test((Aaaa)):
...


class Test(aaaaaaaaaaaaaaa + bbbbbbbbbbbbbbbbbbbbbb + cccccccccccccccccccccccc + dddddddddddddddddddddd + eeeeeeeee, ffffffffffffffffff, gggggggggggggggggg):
pass

class Test(Aaaa): # trailing comment
pass
15 changes: 13 additions & 2 deletions crates/ruff_python_formatter/src/comments/placement.rs
Expand Up @@ -582,6 +582,12 @@ fn handle_trailing_end_of_line_condition_comment<'a>(
.as_deref()
.map(AnyNodeRef::from)
.or_else(|| Some(AnyNodeRef::from(args.as_ref()))),
AnyNodeRef::StmtClassDef(StmtClassDef {
bases, keywords, ..
}) => keywords
.last()
.map(AnyNodeRef::from)
.or_else(|| bases.last().map(AnyNodeRef::from)),
_ => None,
};

Expand Down Expand Up @@ -622,8 +628,13 @@ fn handle_trailing_end_of_line_condition_comment<'a>(
TokenKind::RParen => {
// Skip over any closing parentheses
}
_ => {
unreachable!("Only ')' or ':' should follow the condition")
TokenKind::Comma => {
// Skip over any trailing comma
}
kind => {
unreachable!(
"Only ')' or ':' should follow the condition but encountered {kind:?}"
)
}
}
}
Expand Down
10 changes: 10 additions & 0 deletions crates/ruff_python_formatter/src/expression/parentheses.rs
Expand Up @@ -26,6 +26,8 @@ pub(super) fn default_expression_needs_parentheses(
#[allow(clippy::if_same_then_else)]
if parenthesize.is_always() {
Parentheses::Always
} else if parenthesize.is_never() {
Parentheses::Never
}
// `Optional` or `Preserve` and expression has parentheses in source code.
else if !parenthesize.is_if_breaks() && is_expression_parenthesized(node, source) {
Expand Down Expand Up @@ -58,14 +60,22 @@ pub enum Parenthesize {
/// Parenthesizes the expression only if it doesn't fit on a line.
IfBreaks,

/// Always adds parentheses
Always,

/// Never adds parentheses. Parentheses are handled by the caller.
Never,
}

impl Parenthesize {
pub(crate) const fn is_always(self) -> bool {
matches!(self, Parenthesize::Always)
}

pub(crate) const fn is_never(self) -> bool {
matches!(self, Parenthesize::Never)
}

pub(crate) const fn is_if_breaks(self) -> bool {
matches!(self, Parenthesize::IfBreaks)
}
Expand Down
16 changes: 13 additions & 3 deletions crates/ruff_python_formatter/src/other/keyword.rs
@@ -1,12 +1,22 @@
use crate::{not_yet_implemented, FormatNodeRule, PyFormatter};
use ruff_formatter::{write, Buffer, FormatResult};
use crate::prelude::*;
use crate::FormatNodeRule;
use ruff_formatter::write;
use rustpython_parser::ast::Keyword;

#[derive(Default)]
pub struct FormatKeyword;

impl FormatNodeRule<Keyword> for FormatKeyword {
fn fmt_fields(&self, item: &Keyword, f: &mut PyFormatter) -> FormatResult<()> {
write!(f, [not_yet_implemented(item)])
let Keyword {
range: _,
arg,
value,
} = item;
if let Some(argument) = arg {
write!(f, [argument.format(), text("=")])?;
}

value.format().fmt(f)
}
}

This file was deleted.

0 comments on commit f7e1cf4

Please sign in to comment.