mirror of
https://github.com/typst/typst
synced 2025-05-14 04:56:26 +08:00
Add a few manual Debug impls to elements
This commit is contained in:
parent
21585e03cf
commit
cb69648e2f
@ -322,10 +322,12 @@ fn create(element: &Elem) -> Result<TokenStream> {
|
|||||||
let partial_eq_impl =
|
let partial_eq_impl =
|
||||||
element.unless_capability("PartialEq", || create_partial_eq_impl(element));
|
element.unless_capability("PartialEq", || create_partial_eq_impl(element));
|
||||||
let repr_impl = element.unless_capability("Repr", || create_repr_impl(element));
|
let repr_impl = element.unless_capability("Repr", || create_repr_impl(element));
|
||||||
|
let derive_debug = element.unless_capability("Debug", || quote! { #[derive(Debug)] });
|
||||||
|
|
||||||
Ok(quote! {
|
Ok(quote! {
|
||||||
#[doc = #docs]
|
#[doc = #docs]
|
||||||
#[derive(Debug, Clone, Hash)]
|
#derive_debug
|
||||||
|
#[derive(Clone, Hash)]
|
||||||
#[allow(clippy::derived_hash_with_manual_eq)]
|
#[allow(clippy::derived_hash_with_manual_eq)]
|
||||||
#vis struct #ident {
|
#vis struct #ident {
|
||||||
#(#fields,)*
|
#(#fields,)*
|
||||||
@ -974,7 +976,8 @@ fn create_repr_impl(element: &Elem) -> TokenStream {
|
|||||||
/// Creates the element's casting vtable.
|
/// Creates the element's casting vtable.
|
||||||
fn create_capable_impl(element: &Elem) -> TokenStream {
|
fn create_capable_impl(element: &Elem) -> TokenStream {
|
||||||
// Forbidden capabilities (i.e capabilities that are not object safe).
|
// Forbidden capabilities (i.e capabilities that are not object safe).
|
||||||
const FORBIDDEN: &[&str] = &["Construct", "PartialEq", "Hash", "LocalName", "Repr"];
|
const FORBIDDEN: &[&str] =
|
||||||
|
&["Construct", "PartialEq", "Hash", "LocalName", "Repr", "Debug"];
|
||||||
|
|
||||||
let ident = &element.ident;
|
let ident = &element.ident;
|
||||||
let relevant = element
|
let relevant = element
|
||||||
|
@ -879,12 +879,19 @@ impl<T: NativeElement + Debug> Debug for Packed<T> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Defines the element for sequences.
|
/// Defines the element for sequences.
|
||||||
#[elem(Repr, PartialEq)]
|
#[elem(Debug, Repr, PartialEq)]
|
||||||
struct SequenceElem {
|
struct SequenceElem {
|
||||||
#[required]
|
#[required]
|
||||||
children: Vec<Prehashed<Content>>,
|
children: Vec<Prehashed<Content>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Debug for SequenceElem {
|
||||||
|
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
|
||||||
|
write!(f, "Sequence ")?;
|
||||||
|
f.debug_list().entries(&self.children).finish()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Derive is currently incompatible with `elem` macro.
|
// Derive is currently incompatible with `elem` macro.
|
||||||
#[allow(clippy::derivable_impls)]
|
#[allow(clippy::derivable_impls)]
|
||||||
impl Default for SequenceElem {
|
impl Default for SequenceElem {
|
||||||
@ -923,7 +930,7 @@ impl Repr for SequenceElem {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Defines the `ElemFunc` for styled elements.
|
/// Defines the `ElemFunc` for styled elements.
|
||||||
#[elem(Repr, PartialEq)]
|
#[elem(Debug, Repr, PartialEq)]
|
||||||
struct StyledElem {
|
struct StyledElem {
|
||||||
#[required]
|
#[required]
|
||||||
child: Prehashed<Content>,
|
child: Prehashed<Content>,
|
||||||
@ -931,6 +938,15 @@ struct StyledElem {
|
|||||||
styles: Styles,
|
styles: Styles,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Debug for StyledElem {
|
||||||
|
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
|
||||||
|
for style in self.styles.iter() {
|
||||||
|
writeln!(f, "#{style:?}")?;
|
||||||
|
}
|
||||||
|
self.child.fmt(f)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl PartialEq for StyledElem {
|
impl PartialEq for StyledElem {
|
||||||
fn eq(&self, other: &Self) -> bool {
|
fn eq(&self, other: &Self) -> bool {
|
||||||
*self.child == *other.child
|
*self.child == *other.child
|
||||||
|
@ -80,6 +80,11 @@ impl Styles {
|
|||||||
self.0.is_empty()
|
self.0.is_empty()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Iterate over the contained styles.
|
||||||
|
pub fn iter(&self) -> impl Iterator<Item = &Style> {
|
||||||
|
self.0.iter().map(|style| &**style)
|
||||||
|
}
|
||||||
|
|
||||||
/// Set an inner value for a style property.
|
/// Set an inner value for a style property.
|
||||||
///
|
///
|
||||||
/// If the property needs folding and the value is already contained in the
|
/// If the property needs folding and the value is already contained in the
|
||||||
|
@ -1,3 +1,5 @@
|
|||||||
|
use std::fmt::{self, Debug, Formatter};
|
||||||
|
|
||||||
use comemo::Prehashed;
|
use comemo::Prehashed;
|
||||||
|
|
||||||
use crate::diag::{bail, SourceResult};
|
use crate::diag::{bail, SourceResult};
|
||||||
@ -22,7 +24,7 @@ use crate::visualize::{
|
|||||||
///
|
///
|
||||||
/// This element is responsible for layouting both the top-level content flow
|
/// This element is responsible for layouting both the top-level content flow
|
||||||
/// and the contents of boxes.
|
/// and the contents of boxes.
|
||||||
#[elem(Layout)]
|
#[elem(Debug, Layout)]
|
||||||
pub struct FlowElem {
|
pub struct FlowElem {
|
||||||
/// The children that will be arranges into a flow.
|
/// The children that will be arranges into a flow.
|
||||||
#[variadic]
|
#[variadic]
|
||||||
@ -95,6 +97,13 @@ impl Layout for Packed<FlowElem> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Debug for FlowElem {
|
||||||
|
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
|
||||||
|
write!(f, "Flow ")?;
|
||||||
|
f.debug_list().entries(&self.children).finish()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Performs flow layout.
|
/// Performs flow layout.
|
||||||
struct FlowLayouter<'a> {
|
struct FlowLayouter<'a> {
|
||||||
/// Whether this is the root flow.
|
/// Whether this is the root flow.
|
||||||
|
@ -1,3 +1,5 @@
|
|||||||
|
use std::fmt::{self, Debug, Formatter};
|
||||||
|
|
||||||
use comemo::Prehashed;
|
use comemo::Prehashed;
|
||||||
|
|
||||||
use crate::diag::SourceResult;
|
use crate::diag::SourceResult;
|
||||||
@ -33,7 +35,7 @@ use crate::layout::{Em, Fragment, Length, Size};
|
|||||||
/// let $a$ be the smallest of the
|
/// let $a$ be the smallest of the
|
||||||
/// three integers. Then, we ...
|
/// three integers. Then, we ...
|
||||||
/// ```
|
/// ```
|
||||||
#[elem(title = "Paragraph", Construct)]
|
#[elem(title = "Paragraph", Debug, Construct)]
|
||||||
pub struct ParElem {
|
pub struct ParElem {
|
||||||
/// The spacing between lines.
|
/// The spacing between lines.
|
||||||
#[resolve]
|
#[resolve]
|
||||||
@ -144,6 +146,13 @@ impl Packed<ParElem> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Debug for ParElem {
|
||||||
|
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
|
||||||
|
write!(f, "Par ")?;
|
||||||
|
f.debug_list().entries(&self.children).finish()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// How to determine line breaks in a paragraph.
|
/// How to determine line breaks in a paragraph.
|
||||||
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash, Cast)]
|
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash, Cast)]
|
||||||
pub enum Linebreaks {
|
pub enum Linebreaks {
|
||||||
|
@ -86,7 +86,7 @@ pub(super) fn define(global: &mut Scope) {
|
|||||||
/// With a function call.
|
/// With a function call.
|
||||||
/// ])
|
/// ])
|
||||||
/// ```
|
/// ```
|
||||||
#[elem(Construct, PlainText, Repr)]
|
#[elem(Debug, Construct, PlainText, Repr)]
|
||||||
pub struct TextElem {
|
pub struct TextElem {
|
||||||
/// A font family name or priority list of font family names.
|
/// A font family name or priority list of font family names.
|
||||||
///
|
///
|
||||||
@ -657,6 +657,12 @@ impl TextElem {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Debug for TextElem {
|
||||||
|
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
|
||||||
|
write!(f, "Text({})", self.text)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl Repr for TextElem {
|
impl Repr for TextElem {
|
||||||
fn repr(&self) -> EcoString {
|
fn repr(&self) -> EcoString {
|
||||||
eco_format!("[{}]", self.text)
|
eco_format!("[{}]", self.text)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user