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:
konsti 2023-07-04 09:07:20 +02:00 committed by GitHub
parent 6acc316d19
commit 787e2fd49d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
26 changed files with 304 additions and 991 deletions

View file

@ -1,5 +1,6 @@
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)]
@ -7,6 +8,15 @@ 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(())
}
}

View file

@ -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])
}
}

View file

@ -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)
}
}