diff --git a/crates/engine/src/design_context_tests.rs b/crates/engine/src/design_context_tests.rs
index 5d6f51304..722edc8b4 100644
--- a/crates/engine/src/design_context_tests.rs
+++ b/crates/engine/src/design_context_tests.rs
@@ -407,3 +407,29 @@ fn a_join_stopped_at_its_budget_says_so() {
let (some, complete) = crate::design_context::referenced(s.doc().unwrap(), &start, &none, 3);
assert_eq!((some.len(), complete), (3, false), "{some:?}");
}
+
+#[test]
+fn code_links_keep_their_value_names_and_carry_source_and_notes() {
+ let mut s = session();
+ let variants: Vec<_> = ["small", "extra_large"]
+ .iter()
+ .map(|v| {
+ let f = node(&mut s, json!({"type": "frame", "name": format!("Size={v}"), "width": 80, "height": 32}));
+ id(&s.execute("component.create", &json!({"ids": [f]})).unwrap())
+ })
+ .collect();
+ let set = id(&s.execute("component.combineVariants", &json!({"ids": variants})).unwrap());
+ s.execute(
+ "codeConnect.set",
+ &json!({"id": set, "name": "Button", "template": "", "source": "src/button.tsx",
+ "notes": "Use for main actions", "values": {"Size": {"extra_large": "xl"}}}),
+ )
+ .unwrap();
+ let instance = id(&s.execute("instance.create", &json!({"component": variants[1]})).unwrap());
+ let v = context(&mut s, json!({"ids": [instance]}));
+ let link = &v["components"][set.to_string()]["codeLinks"][0];
+ assert_eq!(link["values"]["Size"]["extra_large"], "xl", "a design value keeps its name: {link}");
+ let code = &only(&v)["code"][0];
+ assert_eq!((code["source"].as_str(), code["notes"].as_str()), (Some("src/button.tsx"), Some("Use for main actions")), "{code}");
+ assert!(code["code"].as_str().unwrap().contains("xl"), "{code}");
+}
diff --git a/crates/engine/src/dev/code_connect.rs b/crates/engine/src/dev/code_connect.rs
index e730c5c48..07b03a180 100644
--- a/crates/engine/src/dev/code_connect.rs
+++ b/crates/engine/src/dev/code_connect.rs
@@ -601,7 +601,15 @@ pub fn variants(doc: &Document, owner: NodeId) -> Vec<(String, Vec)> {
/// A link as the connect file holds it.
fn link_json(component: &str, l: &CodeLink) -> Value {
- let mut v = json!({"component": component, "language": l.language, "importPath": l.import_path, "name": l.name, "template": l.template});
+ let mut v = link_fields(l);
+ v["component"] = json!(component);
+ v
+}
+
+/// A link's own fields, as the connect file and `inspect.designContext` give them: its keys are
+/// the file's, and a mapping's property names and values stay as written.
+pub(crate) fn link_fields(l: &CodeLink) -> Value {
+ let mut v = json!({"language": l.language, "importPath": l.import_path, "name": l.name, "template": l.template});
if !l.source.is_empty() {
v["source"] = json!(l.source);
}
@@ -633,10 +641,7 @@ fn entry(doc: &Document, id: NodeId, root: Option<&str>) -> Option {
.iter()
.map(|l| {
let lang = CodeLang::from_id(&l.language);
- let mut v = link_json(&n.name, l);
- if let Some(o) = v.as_object_mut() {
- o.remove("component");
- }
+ let mut v = link_fields(l);
v["import"] = json!(lang.map(|k| import_line(l, k)));
v["snippet"] = json!(lang.and_then(|k| connected_code(doc, id, k, l)));
if let Some(root) = root.filter(|_| !l.source.is_empty()) {
@@ -746,7 +751,6 @@ pub fn specs() -> Vec {
doc.nodes().filter(|n| owner(doc, n.id) == Some(n.id)).flat_map(|n| n.code_links.iter().map(|l| link_json(&n.name, l))).collect();
let key =
|v: &Value| (v["component"].as_str().unwrap_or_default().to_string(), v["language"].as_str().unwrap_or_default().to_string());
- links.sort_by_key(key);
let count = links.len();
// The file also holds the project's settings and the links of other designs: only
// the links of this file's components are this command's.
diff --git a/crates/engine/src/dev/design_context.rs b/crates/engine/src/dev/design_context.rs
index c21467110..6ad850804 100644
--- a/crates/engine/src/dev/design_context.rs
+++ b/crates/engine/src/dev/design_context.rs
@@ -178,7 +178,14 @@ fn code(doc: &Document, id: NodeId) -> Vec {
.filter_map(|l| {
let lang = CodeLang::from_id(&l.language)?;
let snippet = code_connect::connected_code(doc, id, lang, l)?;
- Some(json!({"language": l.language, "importPath": l.import_path, "name": l.name, "code": snippet}))
+ // The snippet already holds the template and the values it maps.
+ let mut v = code_connect::link_fields(l);
+ if let Some(o) = v.as_object_mut() {
+ o.remove("template");
+ o.remove("values");
+ }
+ v["code"] = json!(snippet);
+ Some(v)
})
.collect()
}
@@ -355,7 +362,7 @@ fn components_json(doc: &Document, ids: &BTreeSet) -> Value {
v["description"] = json!(description);
}
if !n.code_links.is_empty() {
- v["codeLinks"] = camel(serde_json::to_value(&n.code_links).unwrap_or(Value::Null));
+ v["codeLinks"] = json!(n.code_links.iter().map(code_connect::link_fields).collect::>());
}
out.insert(id.to_string(), v);
}
diff --git a/docs/mcp.md b/docs/mcp.md
index b2aba1f43..800428a15 100644
--- a/docs/mcp.md
+++ b/docs/mcp.md
@@ -450,7 +450,7 @@ Each layer in `nodes` has `id`, `name`, `type` and:
with the `name` of each. `explicit` holds the value they resolved to.
- `unsupported`: `{property, why}` for what has no direct CSS equivalent. The value stays in `explicit`.
- `code`: for a component or instance connected to code, per language, `{language, importPath, name,
- code}`, with the instance's own values in `code`. A connected instance stops there: `childCount`
+ source?, notes?, code}`, with the instance's own values in `code`. A connected instance stops there: `childCount`
says what it holds, and `get_node` reads it.
- `children`, or `childCount` where the tree stops.