use crate::compsys::ported::shared::{zstyle_T, zstyle_t};
use crate::ported::params::{getiparam, getsparam, setsparam};
use crate::ported::zle::compcore::set_compstate_str;
pub fn _list() -> i32 {
let _fn_scope = crate::compsys::ported::shared::FnScope::enter("_list");
if getiparam("_matcher_num") > 1 {
return 1;
}
let curcontext = getsparam("curcontext").unwrap_or_default();
let ctx = format!(":completion:{}:", curcontext);
let (pre, suf) = if zstyle_t(&ctx, "word") == 0 {
let histno = getsparam("HISTNO").unwrap_or_default();
let lbuf = getsparam("LBUFFER").unwrap_or_default();
let rbuf = getsparam("RBUFFER").unwrap_or_default();
(format!("{}{}", histno, lbuf), rbuf)
} else {
(
getsparam("PREFIX").unwrap_or_default(),
getsparam("SUFFIX").unwrap_or_default(),
)
};
let condition_on = zstyle_T(&ctx, "condition") == 0;
let last_pre = getsparam("_list_prefix").unwrap_or_default();
let last_suf = getsparam("_list_suffix").unwrap_or_default();
if condition_on && (pre != last_pre || suf != last_suf) {
set_compstate_str("insert", "");
set_compstate_str("list", "list force");
let _ = setsparam("_list_prefix", &pre);
let _ = setsparam("_list_suffix", &suf);
}
1
}
#[cfg(test)]
mod tests {
use super::*;
use crate::ported::params::setiparam;
#[test]
fn always_returns_one() {
let _g = crate::test_util::global_state_lock();
setiparam("_matcher_num", 1);
assert_eq!(_list(), 1);
}
#[test]
fn matcher_num_gt_one_short_circuits() {
let _g = crate::test_util::global_state_lock();
setiparam("_matcher_num", 5);
let _ = setsparam("_list_prefix", "untouched");
assert_eq!(_list(), 1);
assert_eq!(getsparam("_list_prefix").as_deref(), Some("untouched"));
setiparam("_matcher_num", 0);
}
#[test]
fn condition_zero_leaves_the_saved_pair_alone() {
let _g = crate::test_util::global_state_lock();
let ops = crate::ported::zsh_h::options {
ind: [0u8; crate::ported::zsh_h::MAX_OPS],
args: Vec::new(),
argscount: 0,
argsalloc: 0,
};
let ctx = ":completion:lcz:lcz:lcz:";
setiparam("_matcher_num", 0);
let _ = setsparam("curcontext", "lcz:lcz:lcz");
let _ = setsparam("PREFIX", "moved");
let _ = setsparam("SUFFIX", "");
let _ = setsparam("_list_prefix", "stale");
let _ = setsparam("_list_suffix", "stale");
crate::ported::modules::zutil::bin_zstyle(
"zstyle",
&[ctx.to_string(), "condition".to_string(), "0".to_string()],
&ops,
0,
);
assert_eq!(_list(), 1);
let landed = getsparam("_list_prefix");
crate::ported::modules::zutil::bin_zstyle(
"zstyle",
&["-d".to_string(), ctx.to_string(), "condition".to_string()],
&ops,
0,
);
crate::ported::params::unsetparam("curcontext");
assert_eq!(
landed.as_deref(),
Some("stale"),
"sh:31 — `condition 0` must skip the body, leaving `_list_prefix` as it was"
);
}
}