Skip to content

Commit

Permalink
feat: allow custom paging characters
Browse files Browse the repository at this point in the history
  • Loading branch information
wlh320 committed Oct 7, 2023
1 parent e5a060c commit ad77372
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
16 changes: 16 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>,
/// if not empty, only trigger completion with special keys
#[serde(default = "default_trigger_characters")]
pub trigger_characters: Vec<String>,
Expand All @@ -44,6 +47,8 @@ pub struct Settings {
pub enabled: Option<bool>,
/// max number of candidates
pub max_candidates: Option<usize>,
/// if not empty, these characters will trigger completion for paging
pub paging_characters: Option<Vec<String>>,
/// if not empty, only trigger completion with special keys
pub trigger_characters: Option<Vec<String>>,
/// if set, completion request with this string will trigger「方案選單」
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -108,6 +114,10 @@ fn default_trigger_characters() -> Vec<String> {
Vec::default()
}

fn default_paging_characters() -> Vec<String> {
["-", "=", ",", "."].map(|x| x.to_string()).to_vec()
}

fn default_shared_data_dir() -> PathBuf {
PathBuf::from("/usr/share/rime-data")
}
Expand Down Expand Up @@ -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,
Expand All @@ -163,13 +174,18 @@ 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();
});
apply_setting!(config <- settings.schema_trigger_character);
// 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()]);
Expand Down
3 changes: 2 additions & 1 deletion src/lsp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
});
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit ad77372

Please sign in to comment.