-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
## Summary Format import statements in all their variants. Specifically, this implemented formatting `StmtImport`, `StmtImportFrom` and `Alias`. ## Test Plan I added some custom snapshots, even though this has been covered well by black's tests.
- Loading branch information
Showing
26 changed files
with
308 additions
and
995 deletions.
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
42 changes: 39 additions & 3 deletions
42
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,48 @@ | ||
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; | ||
|
||
#[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
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.