-
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 import statements #5493
Merged
Merged
Format import statements #5493
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
3 changes: 3 additions & 0 deletions
3
crates/ruff_python_formatter/resources/test/fixtures/ruff/statement/import.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from a import aksjdhflsakhdflkjsadlfajkslhfdkjsaldajlahflashdfljahlfksajlhfajfjfsaahflakjslhdfkjalhdskjfa | ||
from a import aksjdhflsakhdflkjsadlfajkslhfdkjsaldajlahflashdfljahlfksajlhfajfjfsaahflakjslhdfkjalhdskjfa, aksjdhflsakhdflkjsadlfajkslhfdkjsaldajlahflashdfljahlfksajlhfajfjfsaahflakjslhdfkjalhdskjfa | ||
from a import aksjdhflsakhdflkjsadlfajkslhfdkjsaldajlahflashdfljahlfksajlhfajfjfsaahflakjslhdfkjalhdskjfa as dfgsdfgsd, aksjdhflsakhdflkjsadlfajkslhfdkjsaldajlahflashdfljahlfksajlhfajfjfsaahflakjslhdfkjalhdskjfa as sdkjflsdjlahlfd |
16 changes: 16 additions & 0 deletions
16
crates/ruff_python_formatter/resources/test/fixtures/ruff/statement/import_from.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
from a import aksjdhflsakhdflkjsadlfajkslhf | ||
from a import ( | ||
aksjdhflsakhdflkjsadlfajkslhf, | ||
) | ||
from a import ( | ||
aksjdhflsakhdflkjsadlfajkslhfdkjsaldajlahflashdfljahlfksajlhfajfjfsaahflakjslhdfkjalhdskjfa, | ||
) | ||
from a import ( | ||
aksjdhflsakhdflkjsadlfajkslhfdkjsaldajlahflashdfljahlfksajlhfajfjfsaahflakjslhdfkjalhdskjfa, | ||
aksjdhflsakhdflkjsadlfajkslhfdkjsaldajlahflashdfljahlfksajlhfajfjfsaahflakjslhdfkjalhdskjfa, | ||
) | ||
from a import ( | ||
aksjdhflsakhdflkjsadlfajkslhfdkjsaldajlahflashdfljahlfksajlhfajfjfsaahflakjslhdfkjalhdskjfa as dfgsdfgsd, | ||
aksjdhflsakhdflkjsadlfajkslhfdkjsaldajlahflashdfljahlfksajlhfajfjfsaahflakjslhdfkjalhdskjfa as sdkjflsdjlahlfd, | ||
) | ||
from aksjdhflsakhdflkjsadlfajkslhfdkjsaldajlahflashdfljahlfksajlhfajfjfsaahflakjslhdfkjalhdskjfa import * |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,22 @@ | ||
use crate::{not_yet_implemented, FormatNodeRule, PyFormatter}; | ||
use ruff_formatter::{write, Buffer, FormatResult}; | ||
use crate::{AsFormat, FormatNodeRule, PyFormatter}; | ||
use ruff_formatter::prelude::{space, text}; | ||
use ruff_formatter::{write, Buffer, Format, FormatResult}; | ||
use rustpython_parser::ast::Alias; | ||
|
||
#[derive(Default)] | ||
pub struct FormatAlias; | ||
|
||
impl FormatNodeRule<Alias> for FormatAlias { | ||
fn fmt_fields(&self, item: &Alias, f: &mut PyFormatter) -> FormatResult<()> { | ||
write!(f, [not_yet_implemented(item)]) | ||
let Alias { | ||
range: _, | ||
name, | ||
asname, | ||
} = item; | ||
name.format().fmt(f)?; | ||
if let Some(asname) = asname { | ||
write!(f, [space(), text("as"), space(), asname.format()])?; | ||
} | ||
Ok(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 40 additions & 3 deletions
43
crates/ruff_python_formatter/src/statement/stmt_import_from.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,49 @@ | ||
use crate::{not_yet_implemented, FormatNodeRule, PyFormatter}; | ||
use ruff_formatter::{write, Buffer, FormatResult}; | ||
use crate::builders::{optional_parentheses, PyFormatterExtensions}; | ||
use crate::{AsFormat, FormatNodeRule, PyFormatter}; | ||
use ruff_formatter::prelude::{dynamic_text, format_with, space, text}; | ||
use ruff_formatter::{write, Buffer, Format, FormatResult}; | ||
use rustpython_parser::ast::StmtImportFrom; | ||
use std::iter; | ||
|
||
#[derive(Default)] | ||
pub struct FormatStmtImportFrom; | ||
|
||
impl FormatNodeRule<StmtImportFrom> for FormatStmtImportFrom { | ||
fn fmt_fields(&self, item: &StmtImportFrom, f: &mut PyFormatter) -> FormatResult<()> { | ||
write!(f, [not_yet_implemented(item)]) | ||
let StmtImportFrom { | ||
module, | ||
names, | ||
range: _, | ||
level, | ||
} = item; | ||
|
||
let level_str = level | ||
.map(|level| ".".repeat(level.to_usize())) | ||
.unwrap_or(String::default()); | ||
|
||
write!( | ||
f, | ||
[ | ||
text("from"), | ||
space(), | ||
dynamic_text(&level_str, None), | ||
module.as_ref().map(AsFormat::format), | ||
space(), | ||
text("import"), | ||
space(), | ||
] | ||
)?; | ||
if let [name] = names.as_slice() { | ||
// star can't be surrounded by parentheses | ||
if name.name.as_str() == "*" { | ||
return text("*").fmt(f); | ||
} | ||
} | ||
let names = format_with(|f| { | ||
f.join_comma_separated() | ||
.entries(names.iter().map(|name| (name, name.format()))) | ||
.finish() | ||
}); | ||
optional_parentheses(&names).fmt(f) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -43,21 +43,20 @@ def eggs() -> Union[str, int]: ... | |
--- Black | ||
+++ Ruff | ||
@@ -1,32 +1,58 @@ | ||
-from typing import Union | ||
+NOT_YET_IMPLEMENTED_StmtImportFrom | ||
+ | ||
from typing import Union | ||
|
||
+ | ||
@bird | ||
-def zoo(): ... | ||
+def zoo(): | ||
+ ... | ||
+ | ||
+ | ||
+class A: | ||
+ ... | ||
|
||
-class A: ... | ||
|
||
+class A: | ||
+ ... | ||
+ | ||
+ | ||
@bar | ||
class B: | ||
- def BMethod(self) -> None: ... | ||
|
@@ -94,14 +93,14 @@ def eggs() -> Union[str, int]: ... | |
+ | ||
+class F(A, C): | ||
+ ... | ||
+ | ||
+ | ||
+def spam() -> None: | ||
+ ... | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unrelated to this PR: interesting, black sometimes keeps the single statement formatting |
||
|
||
-class F(A, C): ... | ||
|
||
-def spam() -> None: ... | ||
+def spam() -> None: | ||
+ ... | ||
+ | ||
+ | ||
@overload | ||
-def spam(arg: str) -> str: ... | ||
+def spam(arg: str) -> str: | ||
|
@@ -120,7 +119,7 @@ def eggs() -> Union[str, int]: ... | |
## Ruff Output | ||
|
||
```py | ||
NOT_YET_IMPLEMENTED_StmtImportFrom | ||
from typing import Union | ||
|
||
|
||
@bird | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 was surprised there wasn't a blanket
Option<T>: T: AsFormat
impl for.format()
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 think I ran into issues when I tried adding it last time because Rust doesn't know that T doesn't implement Format and AsFormat at the same time