summaryrefslogtreecommitdiff
path: root/src/model/func.rs
diff options
context:
space:
mode:
authorLaurenz <laurmaedje@gmail.com>2022-12-30 19:40:29 +0100
committerLaurenz <laurmaedje@gmail.com>2022-12-30 20:00:50 +0100
commita6d90c1bf1e9fefa0af04206909a40e112d6bb14 (patch)
treefc16276142f74b9a50102a2e855942f7e2593c25 /src/model/func.rs
parentf70cea508cd30fa40770ea989fe2a19e715a357b (diff)
Numbering functions
Diffstat (limited to 'src/model/func.rs')
-rw-r--r--src/model/func.rs50
1 files changed, 34 insertions, 16 deletions
diff --git a/src/model/func.rs b/src/model/func.rs
index 3fb8f4d4..878b717f 100644
--- a/src/model/func.rs
+++ b/src/model/func.rs
@@ -10,13 +10,13 @@ use super::{
};
use crate::diag::{bail, SourceResult, StrResult};
use crate::syntax::ast::{self, AstNode, Expr};
-use crate::syntax::{SourceId, SyntaxNode};
+use crate::syntax::{SourceId, Span, SyntaxNode};
use crate::util::EcoString;
use crate::World;
/// An evaluatable function.
#[derive(Clone, Hash)]
-pub struct Func(Arc<Repr>);
+pub struct Func(Arc<Repr>, Span);
/// The different kinds of function representations.
#[derive(Hash)]
@@ -40,27 +40,33 @@ impl Func {
func: fn(&Vm, &mut Args) -> SourceResult<Value>,
info: FuncInfo,
) -> Self {
- Self(Arc::new(Repr::Native(Native { func, set: None, node: None, info })))
+ Self(
+ Arc::new(Repr::Native(Native { func, set: None, node: None, info })),
+ Span::detached(),
+ )
}
/// Create a new function from a native rust node.
pub fn from_node<T: Node>(mut info: FuncInfo) -> Self {
info.params.extend(T::properties());
- Self(Arc::new(Repr::Native(Native {
- func: |ctx, args| {
- let styles = T::set(args, true)?;
- let content = T::construct(ctx, args)?;
- Ok(Value::Content(content.styled_with_map(styles.scoped())))
- },
- set: Some(|args| T::set(args, false)),
- node: Some(NodeId::of::<T>()),
- info,
- })))
+ Self(
+ Arc::new(Repr::Native(Native {
+ func: |ctx, args| {
+ let styles = T::set(args, true)?;
+ let content = T::construct(ctx, args)?;
+ Ok(Value::Content(content.styled_with_map(styles.scoped())))
+ },
+ set: Some(|args| T::set(args, false)),
+ node: Some(NodeId::of::<T>()),
+ info,
+ })),
+ Span::detached(),
+ )
}
/// Create a new function from a closure.
- pub(super) fn from_closure(closure: Closure) -> Self {
- Self(Arc::new(Repr::Closure(closure)))
+ pub(super) fn from_closure(closure: Closure, span: Span) -> Self {
+ Self(Arc::new(Repr::Closure(closure)), span)
}
/// The name of the function.
@@ -81,6 +87,17 @@ impl Func {
}
}
+ /// The function's span.
+ pub fn span(&self) -> Span {
+ self.1
+ }
+
+ /// Attach a span to the function.
+ pub fn spanned(mut self, span: Span) -> Self {
+ self.1 = span;
+ self
+ }
+
/// The number of positional arguments this function takes, if known.
pub fn argc(&self) -> Option<usize> {
match self.0.as_ref() {
@@ -121,7 +138,8 @@ impl Func {
/// Apply the given arguments to the function.
pub fn with(self, args: Args) -> Self {
- Self(Arc::new(Repr::With(self, args)))
+ let span = self.1;
+ Self(Arc::new(Repr::With(self, args)), span)
}
/// Create a selector for this function's node type, filtering by node's