Add builders for common comment rendering (#3232)

This commit is contained in:
Charlie Marsh 2023-02-25 23:16:24 -05:00 committed by GitHub
parent a8a312e862
commit 51bca19c1d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
13 changed files with 195 additions and 433 deletions

View file

@ -1,8 +1,9 @@
use ruff_formatter::prelude::*;
use ruff_formatter::{write, Format};
use ruff_text_size::TextSize;
use ruff_text_size::{TextRange, TextSize};
use crate::context::ASTFormatContext;
use crate::core::types::Range;
use crate::cst::Stmt;
use crate::shared_traits::AsFormat;
@ -28,6 +29,26 @@ pub fn block(body: &[Stmt]) -> Block {
Block { body }
}
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub struct Literal {
range: Range,
}
impl Format<ASTFormatContext<'_>> for Literal {
fn fmt(&self, f: &mut Formatter<ASTFormatContext<'_>>) -> FormatResult<()> {
let (text, start, end) = f.context().locator().slice(self.range);
f.write_element(FormatElement::StaticTextSlice {
text,
range: TextRange::new(start.try_into().unwrap(), end.try_into().unwrap()),
})
}
}
#[inline]
pub const fn literal(range: Range) -> Literal {
Literal { range }
}
pub(crate) const fn join_names(names: &[String]) -> JoinNames {
JoinNames { names }
}