mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-02 18:02:23 +00:00
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:
parent
f091b46497
commit
0ef6af807b
1 changed files with 19 additions and 9 deletions
|
@ -1,9 +1,9 @@
|
||||||
use crate::comments::Comments;
|
use crate::comments::Comments;
|
||||||
use crate::PyFormatOptions;
|
use crate::PyFormatOptions;
|
||||||
use ruff_formatter::prelude::*;
|
use ruff_formatter::{Buffer, FormatContext, GroupId, SourceCode};
|
||||||
use ruff_formatter::{Arguments, Buffer, FormatContext, GroupId, SourceCode};
|
|
||||||
use ruff_source_file::Locator;
|
use ruff_source_file::Locator;
|
||||||
use std::fmt::{Debug, Formatter};
|
use std::fmt::{Debug, Formatter};
|
||||||
|
use std::ops::{Deref, DerefMut};
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct PyFormatContext<'a> {
|
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>
|
pub(crate) struct WithNodeLevel<'ast, 'buf, B>
|
||||||
where
|
where
|
||||||
B: Buffer<Context = PyFormatContext<'ast>>,
|
B: Buffer<Context = PyFormatContext<'ast>>,
|
||||||
|
@ -119,16 +120,25 @@ where
|
||||||
saved_level,
|
saved_level,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[inline]
|
impl<'ast, 'buf, B> Deref for WithNodeLevel<'ast, 'buf, B>
|
||||||
pub(crate) fn write_fmt(&mut self, arguments: Arguments<B::Context>) -> FormatResult<()> {
|
where
|
||||||
self.buffer.write_fmt(arguments)
|
B: Buffer<Context = PyFormatContext<'ast>>,
|
||||||
|
{
|
||||||
|
type Target = B;
|
||||||
|
|
||||||
|
fn deref(&self) -> &Self::Target {
|
||||||
|
self.buffer
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[allow(unused)]
|
impl<'ast, 'buf, B> DerefMut for WithNodeLevel<'ast, 'buf, B>
|
||||||
#[inline]
|
where
|
||||||
pub(crate) fn write_element(&mut self, element: FormatElement) -> FormatResult<()> {
|
B: Buffer<Context = PyFormatContext<'ast>>,
|
||||||
self.buffer.write_element(element)
|
{
|
||||||
|
fn deref_mut(&mut self) -> &mut Self::Target {
|
||||||
|
self.buffer
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue