mirror of
https://github.com/astral-sh/ruff.git
synced 2025-07-25 14:03:51 +00:00
Format import statements (#5493)
## 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.
This commit is contained in:
parent
6acc316d19
commit
787e2fd49d
26 changed files with 304 additions and 991 deletions
|
@ -1,4 +1,5 @@
|
|||
use crate::{not_yet_implemented, FormatNodeRule, PyFormatter};
|
||||
use crate::{FormatNodeRule, FormattedIterExt, PyFormatter};
|
||||
use ruff_formatter::prelude::{format_args, format_with, space, text};
|
||||
use ruff_formatter::{write, Buffer, FormatResult};
|
||||
use rustpython_parser::ast::StmtImport;
|
||||
|
||||
|
@ -7,6 +8,12 @@ pub struct FormatStmtImport;
|
|||
|
||||
impl FormatNodeRule<StmtImport> for FormatStmtImport {
|
||||
fn fmt_fields(&self, item: &StmtImport, f: &mut PyFormatter) -> FormatResult<()> {
|
||||
write!(f, [not_yet_implemented(item)])
|
||||
let StmtImport { names, range: _ } = item;
|
||||
let names = format_with(|f| {
|
||||
f.join_with(&format_args![text(","), space()])
|
||||
.entries(names.iter().formatted())
|
||||
.finish()
|
||||
});
|
||||
write!(f, [text("import"), space(), names])
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
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)]
|
||||
|
@ -7,6 +9,40 @@ 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)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue