mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-24 20:34:53 +00:00
Use new formatter infrastructure in CLI and test (#4767)
* Use dummy verbatim formatter for all nodes * Use new formatter infrastructure in CLI and test * Expose the new formatter in the CLI * Merge import blocks
This commit is contained in:
parent
9bf168c0a4
commit
d4027d8b65
6 changed files with 64 additions and 41 deletions
|
@ -1,38 +1,37 @@
|
|||
use crate::context::PyFormatContext;
|
||||
use crate::{AsFormat, IntoFormat, PyFormatter};
|
||||
use ruff_formatter::{Format, FormatOwnedWithRule, FormatRefWithRule, FormatResult, FormatRule};
|
||||
use rustpython_parser::ast::Mod;
|
||||
|
||||
pub(crate) mod mod_expression;
|
||||
pub(crate) mod mod_function_type;
|
||||
pub(crate) mod mod_interactive;
|
||||
pub(crate) mod mod_module;
|
||||
|
||||
use crate::context::PyFormatContext;
|
||||
use ruff_formatter::format_element::tag::VerbatimKind;
|
||||
use ruff_formatter::prelude::*;
|
||||
use ruff_formatter::write;
|
||||
use rustpython_parser::ast::{Mod, Ranged};
|
||||
#[derive(Default)]
|
||||
pub struct FormatMod;
|
||||
|
||||
pub(crate) struct FormatModule<'a> {
|
||||
module: &'a Mod,
|
||||
}
|
||||
|
||||
impl<'a> FormatModule<'a> {
|
||||
pub(crate) fn new(module: &'a Mod) -> Self {
|
||||
Self { module }
|
||||
impl FormatRule<Mod, PyFormatContext<'_>> for FormatMod {
|
||||
fn fmt(&self, item: &Mod, f: &mut PyFormatter) -> FormatResult<()> {
|
||||
match item {
|
||||
Mod::Module(x) => x.format().fmt(f),
|
||||
Mod::Interactive(x) => x.format().fmt(f),
|
||||
Mod::Expression(x) => x.format().fmt(f),
|
||||
Mod::FunctionType(x) => x.format().fmt(f),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Format<PyFormatContext<'_>> for FormatModule<'_> {
|
||||
fn fmt(&self, f: &mut Formatter<PyFormatContext<'_>>) -> FormatResult<()> {
|
||||
let range = self.module.range();
|
||||
|
||||
write!(f, [source_position(range.start())])?;
|
||||
|
||||
f.write_element(FormatElement::Tag(Tag::StartVerbatim(
|
||||
VerbatimKind::Verbatim {
|
||||
length: range.len(),
|
||||
},
|
||||
)))?;
|
||||
write!(f, [source_text_slice(range, ContainsNewlines::Detect)])?;
|
||||
f.write_element(FormatElement::Tag(Tag::EndVerbatim))?;
|
||||
|
||||
write!(f, [source_position(range.end())])
|
||||
impl<'ast> AsFormat<PyFormatContext<'ast>> for Mod {
|
||||
type Format<'a> = FormatRefWithRule<'a, Mod, FormatMod, PyFormatContext<'ast>>;
|
||||
fn format(&self) -> Self::Format<'_> {
|
||||
FormatRefWithRule::new(self, FormatMod::default())
|
||||
}
|
||||
}
|
||||
|
||||
impl<'ast> IntoFormat<PyFormatContext<'ast>> for Mod {
|
||||
type Format = FormatOwnedWithRule<Mod, FormatMod, PyFormatContext<'ast>>;
|
||||
fn into_format(self) -> Self::Format {
|
||||
FormatOwnedWithRule::new(self, FormatMod::default())
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue