From 1005503a7f5baa5baa8c75270e9f873c928ec2df Mon Sep 17 00:00:00 2001 From: zilcH40 Date: Tue, 4 Apr 2023 21:00:08 +0800 Subject: [PATCH] feat: revert preselct feat, make it configurable - feat: add new configs to runtime settings - doc: update doc --- doc/nvim.md | 1 + src/config.rs | 17 +++++++++++++++++ src/lsp.rs | 10 +++++++--- 3 files changed, 25 insertions(+), 3 deletions(-) diff --git a/doc/nvim.md b/doc/nvim.md index fcb9b9e..3b9cf5c 100644 --- a/doc/nvim.md +++ b/doc/nvim.md @@ -19,6 +19,7 @@ local start_rime = function() schema_trigger_character = "&" -- [since v0.2.0] 当输入此字符串时请求补全会触发 “方案选单” always_incomplete = false -- [since v0.2.3] true 强制补全永远刷新整个列表,而不是使用过滤 max_tokens = 0 -- [since v0.2.3] 大于 0 表示会在删除到这个字符个数的时候,重建所有候选词,而不使用删除字符操作 + preselect_first = false -- [since v0.2.3] 是否默认第一个候选项是选中状态,default false }, }); vim.lsp.buf_attach_client(0, client_id) diff --git a/src/config.rs b/src/config.rs index 7169ad8..cdbba4f 100644 --- a/src/config.rs +++ b/src/config.rs @@ -32,6 +32,9 @@ pub struct Config { /// if CompletionItem is always incomplete #[serde(default = "default_always_incomplete")] pub always_incomplete: bool, + /// if preselect first CompletionItem + #[serde(default = "default_preselect_first")] + pub preselect_first: bool, } /// settings that can be tweaked during running @@ -45,6 +48,12 @@ pub struct Settings { pub trigger_characters: Option>, /// if set, completion request with this string will trigger「方案選單」 pub schema_trigger_character: Option, + /// if set, when a delete action arrives the number of max tokens, emit a force new_typing + pub max_tokens: Option, + /// if CompletionItem is always incomplete + pub always_incomplete: Option, + /// if preselect first CompletionItem + pub preselect_first: Option, } macro_rules! apply_setting { @@ -74,6 +83,7 @@ impl Default for Config { schema_trigger_character: default_schema_trigger_character(), max_tokens: default_max_tokens(), always_incomplete: default_always_incomplete(), + preselect_first: default_preselect_first(), } } } @@ -116,6 +126,10 @@ fn default_schema_trigger_character() -> String { String::default() } +fn default_preselect_first() -> bool { + false +} + #[test] fn test_default_config() { let config: Config = Default::default(); @@ -141,6 +155,9 @@ fn test_apply_settings() { max_candidates: Some(100), trigger_characters: Some(vec!["foo".to_string()]), schema_trigger_character: Some(String::from("bar")), + max_tokens: None, + always_incomplete: None, + preselect_first: None, }; // apply settings with macro let mut test_val = vec!["baz".to_string()]; diff --git a/src/lsp.rs b/src/lsp.rs index 93f8ee8..60dac6d 100644 --- a/src/lsp.rs +++ b/src/lsp.rs @@ -91,6 +91,9 @@ impl Backend { self.compile_regex(&v).await; }); apply_setting!(config <- settings.schema_trigger_character); + apply_setting!(config <- settings.max_tokens); + apply_setting!(config <- settings.always_incomplete); + apply_setting!(config <- settings.preselect_first); } async fn create_work_done_progress(&self, token: NumberOrString) -> Result { @@ -201,7 +204,8 @@ impl Backend { utils::build_order_to_sort_text(max_candidates) }; let is_selecting = new_input.is_selecting(); - let candidate_to_completion_item = |i: usize, c: Candidate| -> CompletionItem { + let preselect_enabled = self.config.read().await.preselect_first; + let candidate_to_completion_item = |(i, c): (usize, Candidate)| -> CompletionItem { let text = match is_selecting { true => submitted.clone() + &c.text, false => c.text, @@ -217,7 +221,7 @@ impl Backend { CompletionItem { label, label_details, - preselect: Some(i == 0), + preselect: (preselect_enabled && i == 0).then_some(true), kind: Some(CompletionItemKind::TEXT), detail: utils::option_string(c.comment), filter_text: Some(filter_text.clone()), @@ -238,7 +242,7 @@ impl Backend { let item_iter = candidates .into_iter() .enumerate() - .map(|(i, c)| candidate_to_completion_item(i, c)); + .map(candidate_to_completion_item); Some(CompletionList { is_incomplete: (self.config.read().await.always_incomplete || is_incomplete), items: item_iter.collect(),