use crate::compsys::ported::_message::_message;
use crate::compsys::ported::_shadow::{_shadow, _unshadow};
use crate::ported::exec_hooks::dispatch_function_call;
use crate::ported::params::{getaparam, setaparam};
use crate::ported::zle::complete::set_compadd_trace;
pub fn _complete_help(args: &[String]) -> i32 {
let target = args
.first()
.filter(|s| !s.is_empty())
.cloned()
.unwrap_or_else(|| "_main_complete".to_string());
let _ = _shadow(&[
"compadd".to_string(),
"compcall".to_string(),
"zstyle".to_string(),
]);
setaparam("_complete_help_funcs", Vec::new());
set_compadd_trace(true);
let ret = dispatch_function_call(&target, &[]).unwrap_or(1);
set_compadd_trace(false);
let _ = _unshadow();
let funcs = getaparam("_complete_help_funcs").unwrap_or_default();
let body = if funcs.is_empty() {
format!("{}: no compadd calls captured", target)
} else {
let mut lines = vec![format!("tags in context :completion:{}:", target)];
for f in &funcs {
lines.push(format!(" compadd {}", f));
}
lines.join("\n")
};
let _ = _message(&["-r".to_string(), body]);
ret
}
#[cfg(test)]
mod tests {
use super::*;
use crate::ported::params::{getaparam, setaparam};
#[test]
fn returns_one_without_executor() {
let _g = crate::test_util::global_state_lock();
assert_eq!(_complete_help(&[]), 1);
}
#[test]
fn clears_funcs_buffer_before_dispatch() {
let _g = crate::test_util::global_state_lock();
setaparam(
"_complete_help_funcs",
vec!["stale_entry".to_string()],
);
let _ = _complete_help(&[]);
let after = getaparam("_complete_help_funcs").unwrap_or_default();
assert!(
!after.iter().any(|s| s == "stale_entry"),
"stale entries must be cleared at widget entry"
);
}
}