From 265c156f0ef7ffb48772df2199318135e9b36b4d Mon Sep 17 00:00:00 2001 From: Takayuki Maeda Date: Tue, 21 Sep 2021 00:27:04 +0900 Subject: [PATCH] add reserved words --- src/components/completion.rs | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/src/components/completion.rs b/src/components/completion.rs index df643c5..848c355 100644 --- a/src/components/completion.rs +++ b/src/components/completion.rs @@ -11,7 +11,10 @@ use tui::{ Frame, }; -const RESERVED_WORDS: &[&str] = &["IN", "AND", "OR", "NOT", "NULL", "IS"]; +const RESERVED_WORDS_IN_WHERE_CLAUSE: &[&str] = &["IN", "AND", "OR", "NOT", "NULL", "IS"]; +const ALL_RESERVED_WORDS: &[&str] = &[ + "IN", "AND", "OR", "NOT", "NULL", "IS", "SELECT", "UPDATE", "DELETE", "FROM", "LIMIT", "WHERE", +]; pub struct CompletionComponent { key_config: KeyConfig, @@ -21,12 +24,19 @@ pub struct CompletionComponent { } impl CompletionComponent { - pub fn new(key_config: KeyConfig, word: impl Into) -> Self { + pub fn new(key_config: KeyConfig, word: impl Into, all: bool) -> Self { Self { key_config, state: ListState::default(), word: word.into(), - candidates: RESERVED_WORDS.iter().map(|w| w.to_string()).collect(), + candidates: if all { + ALL_RESERVED_WORDS.iter().map(|w| w.to_string()).collect() + } else { + RESERVED_WORDS_IN_WHERE_CLAUSE + .iter() + .map(|w| w.to_string()) + .collect() + }, } } @@ -145,7 +155,7 @@ mod test { #[test] fn test_filterd_candidates_lowercase() { assert_eq!( - CompletionComponent::new(KeyConfig::default(), "an") + CompletionComponent::new(KeyConfig::default(), "an", false) .filterd_candidates() .collect::>(), vec![&"AND".to_string()] @@ -155,7 +165,7 @@ mod test { #[test] fn test_filterd_candidates_uppercase() { assert_eq!( - CompletionComponent::new(KeyConfig::default(), "AN") + CompletionComponent::new(KeyConfig::default(), "AN", false) .filterd_candidates() .collect::>(), vec![&"AND".to_string()] @@ -165,14 +175,14 @@ mod test { #[test] fn test_filterd_candidates_multiple_candidates() { assert_eq!( - CompletionComponent::new(KeyConfig::default(), "n") + CompletionComponent::new(KeyConfig::default(), "n", false) .filterd_candidates() .collect::>(), vec![&"NOT".to_string(), &"NULL".to_string()] ); assert_eq!( - CompletionComponent::new(KeyConfig::default(), "N") + CompletionComponent::new(KeyConfig::default(), "N", false) .filterd_candidates() .collect::>(), vec![&"NOT".to_string(), &"NULL".to_string()]