mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-22 03:15:44 +00:00
Introduce SourceCodeSlice
to reduce the size of FormatElement
(#4622)
Introduce `SourceCodeSlice` to reduce the size of `FormatElement`
This commit is contained in:
parent
6943beee66
commit
86ced3516b
26 changed files with 408 additions and 171 deletions
|
@ -1,36 +1,34 @@
|
|||
use std::rc::Rc;
|
||||
|
||||
use ruff_formatter::{FormatContext, SimpleFormatOptions};
|
||||
use ruff_formatter::{FormatContext, SimpleFormatOptions, SourceCode};
|
||||
use ruff_python_ast::source_code::Locator;
|
||||
|
||||
pub struct ASTFormatContext {
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct ASTFormatContext<'source> {
|
||||
options: SimpleFormatOptions,
|
||||
contents: Rc<str>,
|
||||
contents: &'source str,
|
||||
}
|
||||
|
||||
impl ASTFormatContext {
|
||||
pub fn new(options: SimpleFormatOptions, contents: &str) -> Self {
|
||||
Self {
|
||||
options,
|
||||
contents: Rc::from(contents),
|
||||
}
|
||||
impl<'source> ASTFormatContext<'source> {
|
||||
pub fn new(options: SimpleFormatOptions, contents: &'source str) -> Self {
|
||||
Self { options, contents }
|
||||
}
|
||||
|
||||
pub fn contents(&self) -> &'source str {
|
||||
self.contents
|
||||
}
|
||||
|
||||
pub fn locator(&self) -> Locator<'source> {
|
||||
Locator::new(self.contents)
|
||||
}
|
||||
}
|
||||
|
||||
impl FormatContext for ASTFormatContext {
|
||||
impl FormatContext for ASTFormatContext<'_> {
|
||||
type Options = SimpleFormatOptions;
|
||||
|
||||
fn options(&self) -> &Self::Options {
|
||||
&self.options
|
||||
}
|
||||
}
|
||||
|
||||
impl ASTFormatContext {
|
||||
pub fn contents(&self) -> Rc<str> {
|
||||
self.contents.clone()
|
||||
}
|
||||
|
||||
pub fn locator(&self) -> Locator {
|
||||
Locator::new(&self.contents)
|
||||
fn source_code(&self) -> SourceCode {
|
||||
SourceCode::new(self.contents)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue