From 78922102e47fbf62162b30015b21f64e16525588 Mon Sep 17 00:00:00 2001 From: cflmy Date: Sun, 18 Oct 2026 13:56:50 +0800 Subject: [PATCH] fix(view): anchor imported-function returns at their call site Track the caller line on CallFrame and use it for return annotations instead of the callee library line. Return 950 stays visible on the actual foreign/writeback calls; the Markdown table remains intact and no longer receives stray return chips. --- public/features/21-persistence.mq.md | 6 +++--- public/features/21-持久化.mq.md | 6 +++--- src/capture.rs | 3 +++ src/interp.rs | 1 + src/view/annot.rs | 17 +++++++++++++++-- 5 files changed, 25 insertions(+), 8 deletions(-) diff --git a/public/features/21-persistence.mq.md b/public/features/21-persistence.mq.md index 29963a9..e0071e2 100644 --- a/public/features/21-persistence.mq.md +++ b/public/features/21-persistence.mq.md @@ -25,9 +25,9 @@ For an exploratory or audit document, pair `foreign` with `lib/writeback`. `writ | Step | What it does | |------|--------------| -| `foreign.run` / `foreign.session` | Execute the foreign code and return a Marqdo value | -| `writeback.record` | Persist that value in a named slot | -| `git diff` | Review the changed result like any other document change | +| foreign.run / foreign.session | Execute the foreign code and return a Marqdo value | +| writeback.record | Persist that value in a named slot | +| git diff | Review the changed result like any other document change | A real writeback in this page. diff --git a/public/features/21-持久化.mq.md b/public/features/21-持久化.mq.md index a16794b..6a751de 100644 --- a/public/features/21-持久化.mq.md +++ b/public/features/21-持久化.mq.md @@ -25,9 +25,9 @@ import writeback:lib/writeback.mq.md | 步骤 | 作用 | |------|------| -| `外联.运行` / `外联.会话` | 执行外联代码并返回 Marqdo 值 | -| `writeback.record` | 把该值持久化到命名槽位 | -| `git diff` | 像审阅普通文档改动一样审阅结果 | +| 外联.运行 / 外联.会话 | 执行外联代码并返回 Marqdo 值 | +| writeback.record | 把该值持久化到命名槽位 | +| git diff | 像审阅普通文档改动一样审阅结果 | 这一页本身就是完整示例。 diff --git a/src/capture.rs b/src/capture.rs index b914e95..5ba01b2 100644 --- a/src/capture.rs +++ b/src/capture.rs @@ -89,7 +89,10 @@ impl StmtEventKind { pub struct CallFrame { pub call_id: u32, pub fn_name: String, + /// Definition line of the callee. pub fn_line: u32, + /// Call site line in the executing document (0 for the entry call). + pub call_line: u32, pub ordinal: u32, pub args_brief: String, } diff --git a/src/interp.rs b/src/interp.rs index 283c47c..2f5609a 100644 --- a/src/interp.rs +++ b/src/interp.rs @@ -208,6 +208,7 @@ impl Interpreter { call_id, fn_name: fun.name.clone(), fn_line: fun.span.line, + call_line: self.current_span.line, ordinal, args_brief, }); diff --git a/src/view/annot.rs b/src/view/annot.rs index 7f6d08a..8cc15cb 100644 --- a/src/view/annot.rs +++ b/src/view/annot.rs @@ -205,9 +205,22 @@ pub fn build_view_annots(module: &Module, source: &str, cap: &RunCapture) -> Vec last_ret.insert((ev.line, ev.call_id, ev.loop_id, ev.iter), ev); } } + let call_sites: HashMap = cap + .view_trace + .calls + .iter() + .map(|fr| (fr.call_id, fr.call_line)) + .collect(); for ((line, _, _, _), ev) in last_ret { + // Return events from an imported library carry the callee's line + // number. Anchor the chip at the caller's line instead. + let anchor_line = call_sites + .get(&ev.call_id) + .copied() + .filter(|l| *l > 0) + .unwrap_or(line); let text = lines - .get(line.saturating_sub(1) as usize) + .get(anchor_line.saturating_sub(1) as usize) .copied() .unwrap_or(""); let mut start = None; @@ -222,7 +235,7 @@ pub fn build_view_annots(module: &Module, source: &str, cap: &RunCapture) -> Vec out.push(ViewAnnot { id: next_id("r"), kind: AnnotKind::Return, - anchor_line: line, + anchor_line, anchor_byte_start: start, anchor_byte_end: end, label: "return".into(),