-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Format Function definitions #4951
Conversation
Current dependencies on/for this PR:
This comment was auto-generated by Graphite. |
PR Check ResultsEcosystem✅ ecosystem check detected no changes. BenchmarkLinux
Windows
|
01a9a40
to
c0c645e
Compare
7ba434b
to
875cd8b
Compare
@@ -77,6 +77,11 @@ def to_camel_case(node: str) -> str: | |||
# ) | |||
# src.joinpath(groups[group]).joinpath("mod.rs").write_text(rustfmt(mod_section)) | |||
for node in group_nodes: | |||
node_path = src.joinpath(groups[group]).joinpath(f"{to_camel_case(node)}.rs") | |||
# Don't override existing manual implementations | |||
if node_path.exists(): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was a bit unexpected haha...
.unwrap_or_default() | ||
.len(); | ||
let match_stmt_indentation = | ||
whitespace::indentation(locator, match_stmt).map_or(usize::MAX, str::len); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
using default
here yields an incorrect result if the body is on the same line as the case header. Using usize::MAX
should be fairly safe ;)
@@ -39,19 +39,17 @@ where | |||
N: AstNode, | |||
{ | |||
fn fmt(&self, node: &N, f: &mut PyFormatter) -> FormatResult<()> { | |||
write!(f, [source_position(node.start())])?; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The leading and trailing comments normally appear before the node and have lower source positions. That's why we should move the node's source position to just cover the node itself.
@@ -234,65 +234,69 @@ def stable_quote_normalization_with_immediate_inner_single_quote(self): | |||
```diff | |||
--- Black | |||
+++ Ruff | |||
@@ -1,219 +1,105 @@ | |||
@@ -1,219 +1,149 @@ | |||
-class MyClass: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This PR does not implement any special handling for docstrings yet.
875cd8b
to
3c45cf3
Compare
|
||
write!(f, [if_group_breaks(&text(","))])?; | ||
|
||
if let Some(last_node) = last_node { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if let Some(last_node) = last_node { | |
// Break group if there is a trailing comma | |
if let Some(last_node) = last_node { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this also need to read the magic trailing comma setting?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we introduce a setting for this, then yes
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i forgot this isn't merged: https://github.com/astral-sh/ruff/pull/4878/files#diff-c1f1071e384c9f14936cad67780f301345744791e34afd93cada2cf43af8dcb5R18
I haven't looked into how to pass settings, but i think it makes sense to introduce the boolean for that now
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The intended way for settings is that Ruff defines a PyFormatOption
struct that implements FormatOptions
and is passed to the PyFormatContext
(instead of using the generic SimpleFormatOptions
). You can then access it with f.context().options()....
This implements StmtPass as `pass`. The snapshot diff is small because pass mainly occurs in bodies and function (#4951) and if/for bodies.
This implements StmtReturn as `return` or `return {value}`. The snapshot diff is small because return occurs in functions (#4951)
3c45cf3
to
60dd48a
Compare
This implements StmtPass as `pass`. The snapshot diff is small because pass mainly occurs in bodies and function (#4951) and if/for bodies.
* Implement StmtPass This implements StmtPass as `pass`. The snapshot diff is small because pass mainly occurs in bodies and function (#4951) and if/for bodies. * Implement StmtReturn This implements StmtReturn as `return` or `return {value}`. The snapshot diff is small because return occurs in functions (#4951)
60dd48a
to
26a4a83
Compare
26a4a83
to
3be5049
Compare
3be5049
to
dcb2aaa
Compare
Pull request was closed
dcb2aaa
to
3b8b3a9
Compare
This implements StmtPass as `pass`. The snapshot diff is small because pass mainly occurs in bodies and function (#4951) and if/for bodies.
* Implement StmtPass This implements StmtPass as `pass`. The snapshot diff is small because pass mainly occurs in bodies and function (#4951) and if/for bodies. * Implement StmtReturn This implements StmtReturn as `return` or `return {value}`. The snapshot diff is small because return occurs in functions (#4951)
Summary
This PR implements formatting for function definitions and async function definitions. The formatting should be fairly complete.
Test Plan
I added a few of my own test cases and reviewed the black differences.