Skip to content

Commit

Permalink
feat: revert preselct feat, make it configurable
Browse files Browse the repository at this point in the history
- feat: add new configs to runtime settings
- doc: update doc
  • Loading branch information
wlh320 committed Apr 4, 2023
1 parent a366a6f commit 1005503
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
1 change: 1 addition & 0 deletions doc/nvim.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
17 changes: 17 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -45,6 +48,12 @@ pub struct Settings {
pub trigger_characters: Option<Vec<String>>,
/// if set, completion request with this string will trigger「方案選單」
pub schema_trigger_character: Option<String>,
/// if set, when a delete action arrives the number of max tokens, emit a force new_typing
pub max_tokens: Option<usize>,
/// if CompletionItem is always incomplete
pub always_incomplete: Option<bool>,
/// if preselect first CompletionItem
pub preselect_first: Option<bool>,
}

macro_rules! apply_setting {
Expand Down Expand Up @@ -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(),
}
}
}
Expand Down Expand Up @@ -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();
Expand All @@ -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()];
Expand Down
10 changes: 7 additions & 3 deletions src/lsp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<NumberOrString> {
Expand Down Expand Up @@ -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,
Expand All @@ -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()),
Expand All @@ -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(),
Expand Down

0 comments on commit 1005503

Please sign in to comment.