mirror of
https://github.com/astral-sh/ruff.git
synced 2025-10-01 06:11:43 +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
|
@ -12,7 +12,7 @@ pub(crate) struct Block<'a> {
|
|||
body: &'a Body,
|
||||
}
|
||||
|
||||
impl Format<ASTFormatContext> for Block<'_> {
|
||||
impl Format<ASTFormatContext<'_>> for Block<'_> {
|
||||
fn fmt(&self, f: &mut Formatter<ASTFormatContext>) -> FormatResult<()> {
|
||||
for (i, stmt) in self.body.iter().enumerate() {
|
||||
if i > 0 {
|
||||
|
@ -28,7 +28,7 @@ impl Format<ASTFormatContext> for Block<'_> {
|
|||
write!(f, [empty_line()])?;
|
||||
}
|
||||
TriviaKind::OwnLineComment(range) => {
|
||||
write!(f, [literal(range), hard_line_break()])?;
|
||||
write!(f, [literal(range, ContainsNewlines::No), hard_line_break()])?;
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
|
@ -49,7 +49,7 @@ pub(crate) struct Statements<'a> {
|
|||
suite: &'a [Stmt],
|
||||
}
|
||||
|
||||
impl Format<ASTFormatContext> for Statements<'_> {
|
||||
impl Format<ASTFormatContext<'_>> for Statements<'_> {
|
||||
fn fmt(&self, f: &mut Formatter<ASTFormatContext>) -> FormatResult<()> {
|
||||
for (i, stmt) in self.suite.iter().enumerate() {
|
||||
if i > 0 {
|
||||
|
@ -70,20 +70,18 @@ pub(crate) struct Literal {
|
|||
range: TextRange,
|
||||
}
|
||||
|
||||
impl Format<ASTFormatContext> for Literal {
|
||||
impl Format<ASTFormatContext<'_>> for Literal {
|
||||
fn fmt(&self, f: &mut Formatter<ASTFormatContext>) -> FormatResult<()> {
|
||||
let text = f.context().contents();
|
||||
|
||||
f.write_element(FormatElement::StaticTextSlice {
|
||||
text,
|
||||
range: self.range,
|
||||
})
|
||||
source_text_slice(self.range, ContainsNewlines::Detect).fmt(f)
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub(crate) const fn literal(range: TextRange) -> Literal {
|
||||
Literal { range }
|
||||
pub(crate) const fn literal(
|
||||
range: TextRange,
|
||||
newlines: ContainsNewlines,
|
||||
) -> SourceTextSliceBuilder {
|
||||
source_text_slice(range, newlines)
|
||||
}
|
||||
|
||||
pub(crate) const fn join_names(names: &[String]) -> JoinNames {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue