mirror of
https://github.com/typst/typst
synced 2025-05-18 11:05:28 +08:00
Add arguments
constructor & fix equality (#2686)
This commit is contained in:
parent
7aef798658
commit
50ea3b4f16
@ -38,7 +38,8 @@ use crate::syntax::{Span, Spanned};
|
|||||||
/// #text(..dict)[Hello]
|
/// #text(..dict)[Hello]
|
||||||
/// ```
|
/// ```
|
||||||
#[ty(scope, name = "arguments")]
|
#[ty(scope, name = "arguments")]
|
||||||
#[derive(Clone, PartialEq, Hash)]
|
#[derive(Clone, Hash)]
|
||||||
|
#[allow(clippy::derived_hash_with_manual_eq)]
|
||||||
pub struct Args {
|
pub struct Args {
|
||||||
/// The span of the whole argument list.
|
/// The span of the whole argument list.
|
||||||
pub span: Span,
|
pub span: Span,
|
||||||
@ -47,7 +48,8 @@ pub struct Args {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// An argument to a function call: `12` or `draw: false`.
|
/// An argument to a function call: `12` or `draw: false`.
|
||||||
#[derive(Debug, Clone, PartialEq, Hash)]
|
#[derive(Debug, Clone, Hash)]
|
||||||
|
#[allow(clippy::derived_hash_with_manual_eq)]
|
||||||
pub struct Arg {
|
pub struct Arg {
|
||||||
/// The span of the whole argument.
|
/// The span of the whole argument.
|
||||||
pub span: Span,
|
pub span: Span,
|
||||||
@ -230,6 +232,27 @@ impl Args {
|
|||||||
|
|
||||||
#[scope]
|
#[scope]
|
||||||
impl Args {
|
impl Args {
|
||||||
|
/// Construct an argument sink in place.
|
||||||
|
///
|
||||||
|
/// This function behaves like `{#let args(..sink) = sink}`.
|
||||||
|
///
|
||||||
|
/// ```example
|
||||||
|
/// #let args = arguments(stroke: red, inset: 1em, [Body])
|
||||||
|
/// #box(..args)
|
||||||
|
/// ```
|
||||||
|
#[func(constructor)]
|
||||||
|
pub fn construct(
|
||||||
|
/// The real arguments (the other argument is just for the docs).
|
||||||
|
/// The docs argument cannot be called `args`.
|
||||||
|
args: &mut Args,
|
||||||
|
/// The arguments to construct.
|
||||||
|
#[external]
|
||||||
|
#[variadic]
|
||||||
|
arguments: Vec<Args>,
|
||||||
|
) -> Args {
|
||||||
|
args.take()
|
||||||
|
}
|
||||||
|
|
||||||
/// Returns the captured positional arguments as an array.
|
/// Returns the captured positional arguments as an array.
|
||||||
#[func(name = "pos", title = "Positional")]
|
#[func(name = "pos", title = "Positional")]
|
||||||
pub fn to_pos(&self) -> Array {
|
pub fn to_pos(&self) -> Array {
|
||||||
@ -250,6 +273,12 @@ impl Args {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl PartialEq for Args {
|
||||||
|
fn eq(&self, other: &Self) -> bool {
|
||||||
|
self.to_pos() == other.to_pos() && self.to_named() == other.to_named()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl Debug for Args {
|
impl Debug for Args {
|
||||||
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
|
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
|
||||||
f.debug_list().entries(&self.items).finish()
|
f.debug_list().entries(&self.items).finish()
|
||||||
@ -263,6 +292,12 @@ impl Repr for Args {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl PartialEq for Arg {
|
||||||
|
fn eq(&self, other: &Self) -> bool {
|
||||||
|
self.name == other.name && self.value.v == other.value.v
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl Repr for Arg {
|
impl Repr for Arg {
|
||||||
fn repr(&self) -> EcoString {
|
fn repr(&self) -> EcoString {
|
||||||
if let Some(name) = &self.name {
|
if let Some(name) = &self.name {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user