Add usable table/map stdlib with put, and show run bindings in view.

Authors can update nested collections via table.put/表.改 instead of json.set chains; view’s right rail adds collapsible Variables from the entry env.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
cflmyandCursor committed 2026-08-12 11:49:48 +08:00
1 parent 91d51f3f0a
commit 25b7be6739
26 files changed
+1577 -123

No files matched your search

+82 -3
View File
@@ -391,17 +391,89 @@ section.block > h2 {
}
@media (max-width: 960px) {
.struct-wrap { grid-template-columns: 1fr; }
.outline-panel { position: static; }
.outline-rail, .outline-panel { position: static; }
}
.outline-panel {
.outline-rail {
position: sticky;
top: 1rem;
display: flex;
flex-direction: column;
gap: 0.55rem;
min-width: 0;
}
.outline-section {
background: var(--surface);
border: 1px solid var(--line);
border-radius: var(--radius);
padding: 0.55rem 0.6rem 0.65rem;
padding: 0.35rem 0.55rem 0.55rem;
box-shadow: 0 1px 0 rgba(0,0,0,0.02);
}
.outline-summary {
cursor: pointer;
font: 600 0.72rem/1.3 var(--sans);
color: var(--muted);
letter-spacing: 0.02em;
text-transform: uppercase;
list-style: none;
padding: 0.2rem 0 0.35rem;
}
.outline-summary::-webkit-details-marker { display: none; }
.outline-panel {
position: static;
background: transparent;
border: none;
border-radius: 0;
padding: 0;
box-shadow: none;
}
.vars-panel {
max-height: min(50vh, 28rem);
overflow: auto;
font: 0.72rem/1.35 var(--mono);
}
.vars-list {
list-style: none;
margin: 0;
padding: 0;
}
.vars-item {
margin: 0 0 0.55rem;
padding-bottom: 0.45rem;
border-bottom: 1px solid var(--line);
}
.vars-item:last-child {
margin-bottom: 0;
padding-bottom: 0;
border-bottom: none;
}
.vars-name {
font-weight: 600;
color: var(--ink);
margin-bottom: 0.2rem;
word-break: break-all;
}
.vars-table {
width: 100%;
border-collapse: collapse;
font: inherit;
}
.vars-table th,
.vars-table td {
border: 1px solid var(--line);
padding: 0.15rem 0.3rem;
text-align: left;
vertical-align: top;
word-break: break-word;
}
.vars-table th {
background: #f5f5f7;
font-weight: 600;
color: var(--muted);
}
.vars-idx, .vars-key { color: var(--muted); white-space: nowrap; }
.vars-scalar { white-space: pre-wrap; }
.vars-empty, .vars-skipped { color: var(--muted); margin: 0.2rem 0; }
.vars-nested > summary { cursor: pointer; color: var(--muted); }
.fn-search {
width: 100%;
box-sizing: border-box;
@@ -1423,6 +1495,13 @@ pub fn layout(title: &str, nav: &str, main: &str) -> String {
scrollIfPinned();
}}
if (thinkingEl && (answerNode && answerNode.data)) thinkingEl.open = false;
if (typeof ev.bindings_html === "string") {{
var vp = document.getElementById("vars-panel");
if (vp) {{
vp.innerHTML = ev.bindings_html
|| "<p class=\\"vars-empty\\">No bindings</p>";
}}
}}
}}
}}
}}
+39 -12
View File
@@ -23,7 +23,10 @@ use tiny_http::{Header, Method, Request, Response, Server, StatusCode};
use crate::input_feed::{effective_stdin, extract_frontmatter_stdin, split_stdin_text};
use crate::view::debug_page::{build_debug_model, page_debug};
use crate::view::html::{escape, page_file, page_index, LinkMode};
use crate::view::render::{collect_input_prompts, render_module_structure, FileViewModel};
use crate::view::render::{
collect_input_prompts, render_bindings_html, render_module_structure, render_right_rail,
FileViewModel,
};
use crate::{run_file_capture, RunOptions};
pub struct ViewOptions {
@@ -306,18 +309,21 @@ pub(crate) fn build_file_view(
) -> Result<FileViewModel> {
let mut source = fs::read_to_string(abs).with_context(|| format!("read {}", abs.display()))?;
// Structure shows this file only — do not merge imported lib bodies into the tree.
let (mut structure, mut outline, input_prompts) = match crate::parse::parse_source(&source) {
Ok(module) => (
render_module_structure(&module, &source),
crate::view::render::render_function_outline(&module),
collect_input_prompts(&module),
),
let (mut structure, mut module_opt, input_prompts) = match crate::parse::parse_source(&source) {
Ok(module) => {
let prompts = collect_input_prompts(&module);
(
render_module_structure(&module, &source),
Some(module),
prompts,
)
}
Err(e) => (
format!(
"<div class=\"err\">parse/load error: {}</div>",
escape(&tidy_user_error(&format!("{e:#}"), abs, rel))
),
String::new(),
None,
Vec::new(),
),
};
@@ -332,8 +338,14 @@ pub(crate) fn build_file_view(
opts.sleep_limit_ms = if live { Some(30_000) } else { Some(0) };
// Optional: sandbox to the whole view tree so sibling folders are reachable.
opts.fs_root = Some(root.root.clone());
let (stdout, stderr, ok, plots) = if awaiting_input || auto_stream {
(String::new(), String::new(), true, Vec::new())
let (stdout, stderr, ok, plots, mut bindings_html) = if awaiting_input || auto_stream {
(
String::new(),
String::new(),
true,
Vec::new(),
String::new(),
)
} else {
match run_file_capture(abs, &opts) {
Ok(cap) => (
@@ -341,12 +353,14 @@ pub(crate) fn build_file_view(
String::new(),
true,
cap.plots.into_iter().map(|p| p.svg).collect(),
render_bindings_html(&cap.bindings),
),
Err(e) => (
String::new(),
tidy_user_error(&format!("{e:#}"), abs, rel),
false,
Vec::new(),
String::new(),
),
}
};
@@ -356,11 +370,21 @@ pub(crate) fn build_file_view(
source = next;
if let Ok(module) = crate::parse::parse_source(&source) {
structure = render_module_structure(&module, &source);
outline = crate::view::render::render_function_outline(&module);
module_opt = Some(module);
}
}
}
outline.push_str(&crate::view::render::render_writeback_outline(&source));
let wb = crate::view::render::render_writeback_outline(&source);
let outline = match &module_opt {
Some(module) => render_right_rail(module, &bindings_html, &wb),
None => {
if bindings_html.is_empty() && wb.is_empty() {
String::new()
} else {
render_right_rail(&crate::ast::Module::default(), &bindings_html, &wb)
}
}
};
Ok(FileViewModel {
rel_path: rel.to_string(),
source,
@@ -374,6 +398,7 @@ pub(crate) fn build_file_view(
awaiting_input,
auto_stream,
plots,
bindings_html: std::mem::take(&mut bindings_html),
})
}
@@ -607,12 +632,14 @@ fn api_run(root: &ViewRoot, mut request: Request) -> Result<()> {
Ok(cap) => {
// Keep the terminal frame small — tokens already streamed via bus.
let preview: String = cap.stdout.chars().take(500).collect();
let bindings_html = render_bindings_html(&cap.bindings);
let done = serde_json::json!({
"type": "done",
"run_id": run_id,
"path": rel_clone,
"ok": true,
"result": preview,
"bindings_html": bindings_html,
});
crate::host::event_bus::EventBus::global().publish_json(&done.to_string());
}
+7
View File
@@ -31,6 +31,13 @@ pub fn write_static(opts: OutputOptions) -> Result<()> {
vm.stderr = "execution skipped (--no-exec)".into();
vm.ok = true;
vm.plots.clear();
vm.bindings_html = String::from(r#"<p class="vars-skipped">skipped</p>"#);
// Rebuild outline so Variables shows skipped.
if let Ok(module) = crate::parse::parse_source(&vm.source) {
let wb = crate::view::render::render_writeback_outline(&vm.source);
vm.outline_html =
crate::view::render::render_right_rail(&module, &vm.bindings_html, &wb);
}
}
let links = LinkMode::Static {
from: Some(rel.clone()),
+201
View File
@@ -41,6 +41,8 @@ pub struct FileViewModel {
pub auto_stream: bool,
/// SVG plots from math lib (embedded in Execution).
pub plots: Vec<String>,
/// HTML for Variables panel (entry bindings after run).
pub bindings_html: String,
}
/// Collect prompt strings from every `input` / `输入` call in the module (pre-order).
@@ -237,6 +239,184 @@ pub fn render_function_outline(module: &Module) -> String {
out
}
/// Right rail: collapsible Functions + Variables (+ optional writeback HTML appended by caller).
pub fn render_right_rail(module: &Module, bindings_html: &str, writeback_html: &str) -> String {
let fn_inner = if module.functions.is_empty() {
r#"<p class="vars-empty">No functions</p>"#.to_string()
} else {
let mut tree = String::from(
r#"<input type="search" id="fn-search" class="fn-search" placeholder="Search functions…" autocomplete="off" spellcheck="false"/>
<ul class="outline-tree">"#,
);
for fun in &module.functions {
tree.push_str(&outline_fun(fun, ""));
}
tree.push_str("</ul>");
tree
};
let vars = if bindings_html.is_empty() {
r#"<p class="vars-empty">No bindings</p>"#
} else {
bindings_html
};
format!(
r#"<div class="outline-rail" id="outline-rail">
<details class="outline-section" open>
<summary class="outline-summary">Functions</summary>
<div class="outline-panel" id="fn-outline">{fn_inner}</div>
</details>
<details class="outline-section" open>
<summary class="outline-summary">Variables</summary>
<div class="vars-panel" id="vars-panel">{vars}</div>
</details>
{wb}
</div>"#,
fn_inner = fn_inner,
vars = vars,
wb = writeback_html,
)
}
/// Render entry bindings as HTML tables (List / Map / records).
pub fn render_bindings_html(bindings: &HashMap<String, crate::value::Value>) -> String {
if bindings.is_empty() {
return String::new();
}
let mut names: Vec<&String> = bindings.keys().collect();
names.sort();
let mut out = String::from(r#"<ul class="vars-list">"#);
for name in names {
let Some(val) = bindings.get(name) else {
continue;
};
out.push_str(&format!(
r#"<li class="vars-item"><div class="vars-name">{}</div><div class="vars-value">{}</div></li>"#,
escape(name),
render_value_html(val, 0)
));
}
out.push_str("</ul>");
out
}
const BIND_MAX_DEPTH: usize = 2;
fn render_value_html(v: &crate::value::Value, depth: usize) -> String {
use crate::value::Value;
match v {
Value::List(xs) => {
if let Some(keys) = record_keys(xs) {
return render_records_table(xs, &keys, depth);
}
let mut s = String::from(r#"<table class="vars-table"><thead><tr><th>#</th><th>value</th></tr></thead><tbody>"#);
for (i, item) in xs.iter().enumerate() {
s.push_str(&format!(
"<tr><td class=\"vars-idx\">{}</td><td>{}</td></tr>",
i + 1,
cell_html(item, depth)
));
}
if xs.is_empty() {
s.push_str(r#"<tr><td colspan="2" class="vars-empty">∅</td></tr>"#);
}
s.push_str("</tbody></table>");
s
}
Value::Map(pairs) => {
let mut s = String::from(
r#"<table class="vars-table"><thead><tr><th>key</th><th>value</th></tr></thead><tbody>"#,
);
for (k, val) in pairs {
s.push_str(&format!(
"<tr><td class=\"vars-key\">{}</td><td>{}</td></tr>",
escape(k),
cell_html(val, depth)
));
}
if pairs.is_empty() {
s.push_str(r#"<tr><td colspan="2" class="vars-empty">∅</td></tr>"#);
}
s.push_str("</tbody></table>");
s
}
other => format!(r#"<span class="vars-scalar">{}</span>"#, escape(&other.as_display())),
}
}
fn cell_html(v: &crate::value::Value, depth: usize) -> String {
use crate::value::Value;
match v {
Value::List(_) | Value::Map(_) if depth >= BIND_MAX_DEPTH => {
format!(
r#"<details class="vars-nested"><summary>{}</summary><span class="vars-scalar">{}</span></details>"#,
escape(&brief_type(v)),
escape(&v.as_display())
)
}
Value::List(_) | Value::Map(_) => render_value_html(v, depth + 1),
other => escape(&other.as_display()),
}
}
fn brief_type(v: &crate::value::Value) -> String {
use crate::value::Value;
match v {
Value::List(xs) => format!("List({})", xs.len()),
Value::Map(xs) => format!("Map({})", xs.len()),
_ => "…".into(),
}
}
fn record_keys(xs: &[crate::value::Value]) -> Option<Vec<String>> {
use crate::value::Value;
if xs.is_empty() {
return None;
}
let mut keys: Vec<String> = Vec::new();
for (i, item) in xs.iter().enumerate() {
let Value::Map(pairs) = item else {
return None;
};
if i == 0 {
keys = pairs.iter().map(|(k, _)| k.clone()).collect();
if keys.is_empty() {
return None;
}
}
}
Some(keys)
}
fn render_records_table(
xs: &[crate::value::Value],
keys: &[String],
depth: usize,
) -> String {
use crate::value::Value;
let mut s = String::from(r#"<table class="vars-table"><thead><tr><th>#</th>"#);
for k in keys {
s.push_str(&format!("<th>{}</th>", escape(k)));
}
s.push_str("</tr></thead><tbody>");
for (i, item) in xs.iter().enumerate() {
let Value::Map(pairs) = item else {
continue;
};
s.push_str(&format!(r#"<tr><td class="vars-idx">{}</td>"#, i + 1));
for k in keys {
let cell = pairs
.iter()
.find(|(kk, _)| kk == k)
.map(|(_, v)| cell_html(v, depth))
.unwrap_or_else(|| String::from(""));
s.push_str(&format!("<td>{cell}</td>"));
}
s.push_str("</tr>");
}
s.push_str("</tbody></table>");
s
}
fn outline_fun(fun: &Function, parent_path: &str) -> String {
let fn_path = fn_path(parent_path, &fun.name);
let mut s = format!(
@@ -998,4 +1178,25 @@ mod tests {
assert!(outline.contains("href=\"#fn-"), "{outline}");
assert!(outline.contains("ol-meta\">h"), "{outline}");
}
#[test]
fn right_rail_has_functions_and_variables() {
let src = include_str!("../../tests/structure/hello.mq.md");
let module = crate::parse::parse_source(src).unwrap();
let mut bind = HashMap::new();
bind.insert(
"xs".into(),
crate::value::Value::List(vec![
crate::value::Value::Text("a".into()),
crate::value::Value::Text("b".into()),
]),
);
let html = render_right_rail(&module, &render_bindings_html(&bind), "");
assert!(html.contains("outline-rail"), "{html}");
assert!(html.contains("Functions"), "{html}");
assert!(html.contains("Variables"), "{html}");
assert!(html.contains("vars-panel"), "{html}");
assert!(html.contains("vars-table"), "{html}");
assert!(html.contains(">xs<") || html.contains("vars-name\">xs"), "{html}");
}
}