feat(view): preserve bracket-marked calls without modifiers
Track CallExpr.bracket_marked from `[callee]` parse so Structure display keeps `[fn] …` instead of falling back to `> fn …` when there are no pre-modifiers. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
d5a9301c7f
commit
551f4ff322
5 files changed
+50
-3
No files matched your search
@@ -121,6 +121,9 @@ pub struct CallExpr {
|
||||
/// Pre-bracket modifiers: `礼貌 [问候] x` → `["礼貌"]`, expanded to `礼貌=True` at eval.
|
||||
/// See [bracket-call-modifiers.md](../../doc/design/bracket-call-modifiers.md).
|
||||
pub pre_modifiers: Vec<String>,
|
||||
/// True when the call was authored with `[callee]` (even with no modifiers).
|
||||
/// View prefers `修饰 [callee] …` / `[callee] …` over `> callee …` when set.
|
||||
pub bracket_marked: bool,
|
||||
}
|
||||
|
||||
impl CallExpr {
|
||||
|
||||
@@ -949,6 +949,7 @@ pub fn parse_call_tail(s: &str, prefer_var: bool) -> Result<(CallExpr, usize)> {
|
||||
receiver,
|
||||
args,
|
||||
pre_modifiers: Vec::new(),
|
||||
bracket_marked: false,
|
||||
},
|
||||
after_callee_offset + ws + args_consumed,
|
||||
))
|
||||
@@ -1040,6 +1041,7 @@ fn try_bracket_marked_call(s: &str, prefer_var: bool) -> Result<Option<(CallExpr
|
||||
receiver,
|
||||
args,
|
||||
pre_modifiers,
|
||||
bracket_marked: true,
|
||||
},
|
||||
consumed,
|
||||
)));
|
||||
@@ -1687,6 +1689,7 @@ mod bracket_call_tests {
|
||||
let (c, _) = parse_call_tail(r#"礼貌 [问候] "Marqdo""#, true).unwrap();
|
||||
assert_eq!(c.callee, "问候");
|
||||
assert_eq!(c.pre_modifiers, vec!["礼貌".to_string()]);
|
||||
assert!(c.bracket_marked);
|
||||
assert_eq!(c.args.len(), 1);
|
||||
}
|
||||
|
||||
@@ -1694,9 +1697,11 @@ mod bracket_call_tests {
|
||||
fn nested_paren_arg() {
|
||||
let (c, _) = parse_call_tail(r#"[问候] ([格式化] "x")"#, true).unwrap();
|
||||
assert_eq!(c.callee, "问候");
|
||||
assert!(c.bracket_marked);
|
||||
match &c.args[0] {
|
||||
Arg::Positional(Expr::Call(inner)) => {
|
||||
assert_eq!(inner.callee, "格式化");
|
||||
assert!(inner.bracket_marked);
|
||||
}
|
||||
other => panic!("expected nested call, got {:?}", other),
|
||||
}
|
||||
|
||||
+38
-2
@@ -1249,7 +1249,8 @@ fn stmt_shell(line: u32, inner: &str, mode: StructureMode) -> String {
|
||||
|
||||
fn call_display(call: &CallExpr) -> String {
|
||||
let mut s = String::new();
|
||||
if !call.pre_modifiers.is_empty() {
|
||||
let bracket = call.bracket_marked || !call.pre_modifiers.is_empty();
|
||||
if bracket {
|
||||
for m in &call.pre_modifiers {
|
||||
s.push_str(m);
|
||||
s.push(' ');
|
||||
@@ -1468,7 +1469,7 @@ fn lit_display(lit: &Literal) -> String {
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::ast::{BinaryOp, Expr, Literal};
|
||||
use crate::ast::{Arg, BinaryOp, CallExpr, Expr, Literal};
|
||||
|
||||
#[test]
|
||||
fn expr_gt_is_surface() {
|
||||
@@ -1632,4 +1633,39 @@ mod tests {
|
||||
let llm = crate::parse::parse_source(include_str!("../../tests/ext/llm-import.mq.md")).unwrap();
|
||||
assert!(uses_stream_panel(&llm));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn call_display_bracket_without_modifiers() {
|
||||
let call = CallExpr {
|
||||
callee: "parse".into(),
|
||||
path: Some(vec!["json".into(), "parse".into()]),
|
||||
receiver: None,
|
||||
args: vec![Arg::Named {
|
||||
name: "text".into(),
|
||||
value: Expr::Literal(Literal::Text("{}".into())),
|
||||
}],
|
||||
pre_modifiers: vec![],
|
||||
bracket_marked: true,
|
||||
};
|
||||
let d = call_display(&call);
|
||||
assert!(d.starts_with("[json.parse]"), "{d}");
|
||||
assert!(!d.starts_with("> "), "{d}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn call_display_gt_when_not_bracket() {
|
||||
let call = CallExpr {
|
||||
callee: "print".into(),
|
||||
path: None,
|
||||
receiver: None,
|
||||
args: vec![Arg::Named {
|
||||
name: "text".into(),
|
||||
value: Expr::Literal(Literal::Text("hi".into())),
|
||||
}],
|
||||
pre_modifiers: vec![],
|
||||
bracket_marked: false,
|
||||
};
|
||||
let d = call_display(&call);
|
||||
assert!(d.starts_with("> print"), "{d}");
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user