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 ++++++++++++++++++++++++++++ src/lisp/lex.rs | 1 + 2 files changed, 29 insertions(+) (limited to 'src/lisp') 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::*; diff --git a/src/lisp/lex.rs b/src/lisp/lex.rs index 754a23f..6e59469 100644 --- a/src/lisp/lex.rs +++ b/src/lisp/lex.rs @@ -235,6 +235,7 @@ fn parse_string(input: &str) -> Result<(usize, Token<'_>), ParseErrorKind> { fn is_ident(ch: char) -> bool { match ch { + // "!$%&*+-./<=>?^_|#" '!' | '$' | '%' | '&' | '*' | '+' | '-' | '.' | '/' | '<' | '=' | '>' | '?' | '^' | '_' | '|' | '#' => true, _ if ch.is_alphanumeric() => true, -- cgit v1.2.3