use crate::ported::exec_hooks::dispatch_function_call;
use crate::ported::params::getsparam;
use crate::ported::zle::compcore::{get_compstate_str, set_compstate_str};
pub fn _in_vared() -> i32 {
let vared = get_compstate_str("vared").unwrap_or_default();
let also: String;
if vared.contains('[') {
if vared.contains(']') {
let head = vared.splitn(2, ']').next().unwrap_or("").replace('[', "-");
set_compstate_str("parameter", &head);
set_compstate_str("context", "value");
also = "-value-".to_string();
} else {
let head = vared.splitn(2, '[').next().unwrap_or("");
set_compstate_str("parameter", head);
set_compstate_str("context", "value");
also = "-value-".to_string();
}
} else {
set_compstate_str("parameter", &vared);
let raw_type = param_type(&vared);
if raw_type.contains("array") || raw_type.contains("assoc") {
set_compstate_str("context", "array_value");
also = "-array-value-".to_string();
} else {
set_compstate_str("context", "value");
also = "-value-".to_string();
}
}
let insert = get_compstate_str("insert").unwrap_or_default();
let cleaned = insert.replace("tab ", "");
set_compstate_str("insert", &cleaned);
dispatch_function_call("_dispatch", &[also.clone(), also]).unwrap_or(1)
}
fn param_type(name: &str) -> String {
if crate::ported::params::getaparam(name).is_some() {
"array".to_string()
} else if getsparam(name).is_some() {
"scalar".to_string()
} else {
String::new()
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::ported::params::setaparam;
#[test]
fn returns_one_without_executor() {
let _g = crate::test_util::global_state_lock();
set_compstate_str("vared", "myvar");
assert_eq!(_in_vared(), 1);
}
#[test]
fn array_param_sets_array_value_context() {
let _g = crate::test_util::global_state_lock();
setaparam("myarr", vec!["a".to_string(), "b".to_string()]);
set_compstate_str("vared", "myarr");
let _ = _in_vared();
assert_eq!(
get_compstate_str("context").as_deref(),
Some("array_value")
);
}
#[test]
fn bracketed_vared_uses_value_context() {
let _g = crate::test_util::global_state_lock();
set_compstate_str("vared", "myarr[key]");
let _ = _in_vared();
assert_eq!(get_compstate_str("context").as_deref(), Some("value"));
assert_eq!(get_compstate_str("parameter").as_deref(), Some("myarr-key"));
}
}