From d2cc31ee49d673f343ce5089071ef3628c3cdc97 Mon Sep 17 00:00:00 2001 From: Akshay Date: Mon, 17 May 2021 16:55:09 +0530 Subject: add tab to complete env variables --- src/lisp/eval.rs | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) (limited to 'src/lisp/eval.rs') diff --git a/src/lisp/eval.rs b/src/lisp/eval.rs index b39730b..784cba0 100644 --- a/src/lisp/eval.rs +++ b/src/lisp/eval.rs @@ -20,6 +20,15 @@ pub struct Evaluator<'ctx, 'global> { pub context: Context, } +impl<'ctx, 'global> Evaluator<'ctx, 'global> { + pub fn new(app: &'global mut AppState<'ctx>) -> Self { + Self { + app, + context: Vec::new(), + } + } +} + impl<'ctx, 'global> Evaluator<'ctx, 'global> where 'ctx: 'global, @@ -337,6 +346,25 @@ pub fn lookup(env_list: &[Environment], key: &str) -> Result(env_list: &'a [Environment], prefix: &str) -> Vec<&'a str> { + fn helper<'h>(env_list: &'h [Environment], prefix: &str, completions: &mut Vec>) { + if !env_list.is_empty() { + let nearest_env = env_list.last().unwrap(); + completions.push( + nearest_env + .keys() + .filter(|k| k.starts_with(prefix)) + .map(|s| &s[prefix.len()..]) + .collect::>(), + ); + helper(&env_list[..env_list.len() - 1], prefix, completions); + } + } + let mut completions = Vec::new(); + helper(env_list, prefix, &mut completions); + completions.into_iter().flatten().collect() +} + #[cfg(test)] mod tests { use super::*; -- cgit v1.2.3