Skip to content

Commit c9aba68

Browse files
committed
make primitive docs struct
1 parent 757fa58 commit c9aba68

File tree

7 files changed

+89
-47
lines changed

7 files changed

+89
-47
lines changed

docs/site.js

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -937,32 +937,32 @@ imports.wbg.__wbindgen_debug_string = function(arg0, arg1) {
937937
imports.wbg.__wbindgen_throw = function(arg0, arg1) {
938938
throw new Error(getStringFromWasm0(arg0, arg1));
939939
};
940-
imports.wbg.__wbindgen_closure_wrapper195 = function(arg0, arg1, arg2) {
941-
const ret = makeMutClosure(arg0, arg1, 6, __wbg_adapter_34);
940+
imports.wbg.__wbindgen_closure_wrapper217 = function(arg0, arg1, arg2) {
941+
const ret = makeMutClosure(arg0, arg1, 18, __wbg_adapter_34);
942942
return addHeapObject(ret);
943943
};
944-
imports.wbg.__wbindgen_closure_wrapper197 = function(arg0, arg1, arg2) {
945-
const ret = makeMutClosure(arg0, arg1, 6, __wbg_adapter_37);
944+
imports.wbg.__wbindgen_closure_wrapper219 = function(arg0, arg1, arg2) {
945+
const ret = makeMutClosure(arg0, arg1, 18, __wbg_adapter_37);
946946
return addHeapObject(ret);
947947
};
948-
imports.wbg.__wbindgen_closure_wrapper199 = function(arg0, arg1, arg2) {
949-
const ret = makeMutClosure(arg0, arg1, 6, __wbg_adapter_34);
948+
imports.wbg.__wbindgen_closure_wrapper221 = function(arg0, arg1, arg2) {
949+
const ret = makeMutClosure(arg0, arg1, 18, __wbg_adapter_34);
950950
return addHeapObject(ret);
951951
};
952-
imports.wbg.__wbindgen_closure_wrapper1029 = function(arg0, arg1, arg2) {
953-
const ret = makeMutClosure(arg0, arg1, 300, __wbg_adapter_42);
952+
imports.wbg.__wbindgen_closure_wrapper1013 = function(arg0, arg1, arg2) {
953+
const ret = makeMutClosure(arg0, arg1, 294, __wbg_adapter_42);
954954
return addHeapObject(ret);
955955
};
956-
imports.wbg.__wbindgen_closure_wrapper1176 = function(arg0, arg1, arg2) {
957-
const ret = makeMutClosure(arg0, arg1, 411, __wbg_adapter_45);
956+
imports.wbg.__wbindgen_closure_wrapper1160 = function(arg0, arg1, arg2) {
957+
const ret = makeMutClosure(arg0, arg1, 405, __wbg_adapter_45);
958958
return addHeapObject(ret);
959959
};
960-
imports.wbg.__wbindgen_closure_wrapper1413 = function(arg0, arg1, arg2) {
961-
const ret = makeMutClosure(arg0, arg1, 507, __wbg_adapter_48);
960+
imports.wbg.__wbindgen_closure_wrapper1397 = function(arg0, arg1, arg2) {
961+
const ret = makeMutClosure(arg0, arg1, 501, __wbg_adapter_48);
962962
return addHeapObject(ret);
963963
};
964-
imports.wbg.__wbindgen_closure_wrapper1527 = function(arg0, arg1, arg2) {
965-
const ret = makeMutClosure(arg0, arg1, 546, __wbg_adapter_51);
964+
imports.wbg.__wbindgen_closure_wrapper1511 = function(arg0, arg1, arg2) {
965+
const ret = makeMutClosure(arg0, arg1, 540, __wbg_adapter_51);
966966
return addHeapObject(ret);
967967
};
968968

docs/site_bg.wasm

243 KB
Binary file not shown.

docs/styles.css

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,7 @@ table {
294294
background-color: #000a;
295295
white-space: pre-wrap;
296296
font-family: "Code Font";
297+
z-index: 1;
297298
}
298299

299300
.variadic-function-button {

site/src/editor.rs

Lines changed: 1 addition & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -216,36 +216,7 @@ pub fn Editor(
216216
let onclick = move |_| replace_code(&p.to_string());
217217
let onmouseover = move |_| {
218218
if let Some(doc) = p.doc() {
219-
let mut doc_lines: Vec<String> =
220-
doc.lines().map(str::trim).map(Into::into).collect();
221-
for i in (0..doc_lines.len()).rev() {
222-
if let Some(ex_line) = doc_lines[i].strip_prefix("ex:") {
223-
match Uiua::with_backend(&WebBackend::default()).load_str(ex_line) {
224-
Ok(env) => {
225-
let stack = env.take_stack();
226-
for item in stack {
227-
for line in item.show().lines().rev() {
228-
doc_lines.insert(i + 1, line.into());
229-
}
230-
}
231-
}
232-
Err(e) => doc_lines.insert(
233-
i + 1,
234-
format!(
235-
"error: {}",
236-
e.to_string()
237-
.lines()
238-
.next()
239-
.unwrap_or_default()
240-
.split_once(' ')
241-
.unwrap_or_default()
242-
.1
243-
),
244-
),
245-
}
246-
}
247-
}
248-
set_glyph_doc.set(doc_lines.join("\n"));
219+
set_glyph_doc.set(doc.to_string());
249220
_ = glyph_doc_element().style().remove_property("display");
250221
}
251222
};

site/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ mod code {
152152
"".to_string()
153153
};
154154
let title = match (prim.doc(), show_name) {
155-
(Some(doc), true) => doc.into(),
155+
(Some(doc), true) => doc.to_string(),
156156
(Some(doc), false) => format!("{}: {}", prim.name().unwrap_or_default(), doc),
157157
(None, true) => String::new(),
158158
(None, false) => prim.name().unwrap_or_default().into(),

site/styles.css

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,7 @@ table {
294294
background-color: #000a;
295295
white-space: pre-wrap;
296296
font-family: "Code Font";
297+
z-index: 1;
297298
}
298299

299300
.variadic-function-button {

src/primitive.rs

Lines changed: 71 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use std::{
22
f64::{consts::PI, INFINITY},
33
fmt,
44
rc::Rc,
5+
sync::OnceLock,
56
};
67

78
use crate::{
@@ -100,16 +101,84 @@ macro_rules! primitive {
100101
_ => None
101102
}
102103
}
103-
pub fn doc(&self) -> Option<&'static str> {
104+
pub fn doc(&self) -> Option<PrimDoc> {
104105
match self {
105-
$(Primitive::$name => Some(concat!($($doc, "\n"),*)).filter(|s| !s.is_empty()),)*
106+
$(Primitive::$name => {
107+
let doc = concat!($($doc, "\n"),*);
108+
static DOC: OnceLock<[PrimDocLine; 0 $(+ {_ = $doc; 1})*]> = OnceLock::new();
109+
if doc.is_empty() {
110+
return None;
111+
}
112+
Some(PrimDoc(DOC.get_or_init(|| [$({
113+
let doc = $doc;
114+
if let Some(ex) = doc.trim().strip_prefix("ex:") {
115+
let input = ex.trim();
116+
let output = Uiua::with_backend(&StdIo::default()).load_str(input)
117+
.map(|env| env.take_stack().into_iter().map(|val| val.show()).collect())
118+
.map_err(|e| e.to_string().lines().next().unwrap_or_default()
119+
.split_once(' ').unwrap_or_default().1.into());
120+
PrimDocLine::Example(PrimExample {
121+
input,
122+
output
123+
})
124+
} else {
125+
PrimDocLine::Text(doc.trim())
126+
}
127+
}),*])))
128+
},)*
106129
_ => None,
107130
}
108131
}
109132
}
110133
};
111134
}
112135

136+
#[derive(Debug)]
137+
pub struct PrimDoc(pub &'static [PrimDocLine]);
138+
139+
impl fmt::Display for PrimDoc {
140+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
141+
for line in self.0 {
142+
match line {
143+
PrimDocLine::Text(text) => writeln!(f, "{}", text)?,
144+
PrimDocLine::Example(ex) => {
145+
writeln!(f, "ex: {}", ex.input)?;
146+
match &ex.output {
147+
Ok(output) => {
148+
for formatted in output {
149+
for (i, line) in formatted.lines().enumerate() {
150+
if i == 0 {
151+
write!(f, " => ")?
152+
} else {
153+
write!(f, " ")?;
154+
}
155+
writeln!(f, "{line}")?;
156+
}
157+
}
158+
}
159+
Err(e) => {
160+
writeln!(f, " => error: {e}")?;
161+
}
162+
}
163+
}
164+
}
165+
}
166+
Ok(())
167+
}
168+
}
169+
170+
#[derive(Debug)]
171+
pub enum PrimDocLine {
172+
Text(&'static str),
173+
Example(PrimExample),
174+
}
175+
176+
#[derive(Debug)]
177+
pub struct PrimExample {
178+
input: &'static str,
179+
output: Result<Vec<String>, String>,
180+
}
181+
113182
primitive!(
114183
// Stack ops
115184
/// Duplicate the top value on the stack

0 commit comments

Comments
 (0)