From ad77372ec2e9a5662833d603a70028d377174ab6 Mon Sep 17 00:00:00 2001 From: zilcH40 Date: Sat, 7 Oct 2023 11:03:14 +0800 Subject: [PATCH] feat: allow custom paging characters --- src/config.rs | 16 ++++++++++++++++ src/lsp.rs | 3 ++- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/src/config.rs b/src/config.rs index cdbba4f..336f6f4 100644 --- a/src/config.rs +++ b/src/config.rs @@ -20,6 +20,9 @@ pub struct Config { /// max number of candidates #[serde(default = "default_max_candidates")] pub max_candidates: usize, + /// if not empty, these characters will trigger completion for paging + #[serde(default = "default_paging_characters")] + pub paging_characters: Vec, /// if not empty, only trigger completion with special keys #[serde(default = "default_trigger_characters")] pub trigger_characters: Vec, @@ -44,6 +47,8 @@ pub struct Settings { pub enabled: Option, /// max number of candidates pub max_candidates: Option, + /// if not empty, these characters will trigger completion for paging + pub paging_characters: Option>, /// if not empty, only trigger completion with special keys pub trigger_characters: Option>, /// if set, completion request with this string will trigger「方案選單」 @@ -79,6 +84,7 @@ impl Default for Config { user_data_dir: default_user_data_dir(), log_dir: default_log_dir(), max_candidates: default_max_candidates(), + paging_characters: default_paging_characters(), trigger_characters: default_trigger_characters(), schema_trigger_character: default_schema_trigger_character(), max_tokens: default_max_tokens(), @@ -108,6 +114,10 @@ fn default_trigger_characters() -> Vec { Vec::default() } +fn default_paging_characters() -> Vec { + ["-", "=", ",", "."].map(|x| x.to_string()).to_vec() +} + fn default_shared_data_dir() -> PathBuf { PathBuf::from("/usr/share/rime-data") } @@ -153,6 +163,7 @@ fn test_apply_settings() { let settings: Settings = Settings { enabled: Some(false), max_candidates: Some(100), + paging_characters: Some(vec![",".to_string(), ".".to_string()]), trigger_characters: Some(vec!["foo".to_string()]), schema_trigger_character: Some(String::from("bar")), max_tokens: None, @@ -163,6 +174,7 @@ fn test_apply_settings() { let mut test_val = vec!["baz".to_string()]; apply_setting!(config <- settings.enabled); apply_setting!(config <- settings.max_candidates); + apply_setting!(config <- settings.paging_characters); apply_setting!(config <- settings.trigger_characters, |v| { test_val = v.clone(); }); @@ -170,6 +182,10 @@ fn test_apply_settings() { // verify assert_eq!(config.enabled, false); assert_eq!(config.max_candidates, 100); + assert_eq!( + config.paging_characters, + vec![",".to_string(), ".".to_string()] + ); assert_eq!(config.trigger_characters, vec!["foo".to_string()]); assert_eq!(config.schema_trigger_character, String::from("bar")); assert_eq!(test_val, vec!["foo".to_string()]); diff --git a/src/lsp.rs b/src/lsp.rs index 60dac6d..bc101fb 100644 --- a/src/lsp.rs +++ b/src/lsp.rs @@ -87,6 +87,7 @@ impl Backend { let mut config = self.config.write().await; apply_setting!(config <- settings.enabled); apply_setting!(config <- settings.max_candidates); + apply_setting!(config <- settings.paging_characters); apply_setting!(config <- settings.trigger_characters, |v| { self.compile_regex(&v).await; }); @@ -272,7 +273,7 @@ impl LanguageServer for Backend { .await; // set LSP triggers let triggers = { - let mut triggers = [".", ",", "-", "="].map(|x| x.to_string()).to_vec(); // pages + let mut triggers = self.config.read().await.paging_characters.clone(); // for paging let user_triggers = &self.config.read().await.trigger_characters; triggers.extend_from_slice(user_triggers); triggers