Implement DerefMut for WithNodeLevel (#6443)

**Summary** Implement `DerefMut` for `WithNodeLevel` so it can be used
in the same way as `PyFormatter`. I want this for my WIP upstack branch
to enable `.fmt(f)` on `WithNodeLevel` context. We could extend this to
remove the other two method from `WithNodeLevel`.
This commit is contained in:
konsti 2023-08-11 12:41:48 +02:00 committed by GitHub
parent f091b46497
commit 0ef6af807b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,9 +1,9 @@
use crate::comments::Comments;
use crate::PyFormatOptions;
use ruff_formatter::prelude::*;
use ruff_formatter::{Arguments, Buffer, FormatContext, GroupId, SourceCode};
use ruff_formatter::{Buffer, FormatContext, GroupId, SourceCode};
use ruff_source_file::Locator;
use std::fmt::{Debug, Formatter};
use std::ops::{Deref, DerefMut};
#[derive(Clone)]
pub struct PyFormatContext<'a> {
@ -96,6 +96,7 @@ impl NodeLevel {
}
}
/// Change the [`NodeLevel`] of the formatter for the lifetime of this struct
pub(crate) struct WithNodeLevel<'ast, 'buf, B>
where
B: Buffer<Context = PyFormatContext<'ast>>,
@ -119,16 +120,25 @@ where
saved_level,
}
}
}
#[inline]
pub(crate) fn write_fmt(&mut self, arguments: Arguments<B::Context>) -> FormatResult<()> {
self.buffer.write_fmt(arguments)
impl<'ast, 'buf, B> Deref for WithNodeLevel<'ast, 'buf, B>
where
B: Buffer<Context = PyFormatContext<'ast>>,
{
type Target = B;
fn deref(&self) -> &Self::Target {
self.buffer
}
}
#[allow(unused)]
#[inline]
pub(crate) fn write_element(&mut self, element: FormatElement) -> FormatResult<()> {
self.buffer.write_element(element)
impl<'ast, 'buf, B> DerefMut for WithNodeLevel<'ast, 'buf, B>
where
B: Buffer<Context = PyFormatContext<'ast>>,
{
fn deref_mut(&mut self) -> &mut Self::Target {
self.buffer
}
}