Add case propagation also with prefix symbol. Fix #244

This commit is contained in:
Federico Terzi 2020-05-10 17:47:27 +02:00
parent e0bdace3f2
commit 949f1e4acf
2 changed files with 44 additions and 3 deletions

View File

@ -92,7 +92,14 @@ impl<'a> From<&'a AutoMatch> for Match{
let first_capitalized : Vec<String> = triggers.iter().map(|trigger| { let first_capitalized : Vec<String> = triggers.iter().map(|trigger| {
let capitalized = trigger.clone(); let capitalized = trigger.clone();
let mut v: Vec<char> = capitalized.chars().collect(); let mut v: Vec<char> = capitalized.chars().collect();
v[0] = v[0].to_uppercase().nth(0).unwrap();
// Capitalize the first alphabetic letter
// See issue #244
let first_alphabetic = v.iter().position(|c| {
c.is_alphabetic()
}).unwrap_or(0);
v[first_alphabetic] = v[first_alphabetic].to_uppercase().nth(0).unwrap();
v.into_iter().collect() v.into_iter().collect()
}).collect(); }).collect();
@ -482,4 +489,30 @@ mod tests {
let _match : Match = serde_yaml::from_str(match_str).unwrap(); let _match : Match = serde_yaml::from_str(match_str).unwrap();
} }
#[test]
fn test_match_propagate_case_with_prefix_symbol() {
let match_str = r###"
trigger: ":hello"
replace: "This is a test"
propagate_case: true
"###;
let _match : Match = serde_yaml::from_str(match_str).unwrap();
assert_eq!(_match.triggers, vec![":hello", ":Hello", ":HELLO"])
}
#[test]
fn test_match_propagate_case_non_alphabetic_should_not_crash() {
let match_str = r###"
trigger: ":.."
replace: "This is a test"
propagate_case: true
"###;
let _match : Match = serde_yaml::from_str(match_str).unwrap();
assert_eq!(_match.triggers, vec![":..", ":..", ":.."])
}
} }

View File

@ -173,8 +173,16 @@ impl super::Renderer for DefaultRenderer {
// Handle case propagation // Handle case propagation
let target_string = if m.propagate_case { let target_string = if m.propagate_case {
let trigger = &m.triggers[trigger_offset]; let trigger = &m.triggers[trigger_offset];
let first_char = trigger.chars().nth(0);
let second_char = trigger.chars().nth(1); // The check should be carried out from the position of the first
// alphabetic letter
// See issue #244
let first_alphabetic = trigger.chars().position(|c| {
c.is_alphabetic()
}).unwrap_or(0);
let first_char = trigger.chars().nth(first_alphabetic);
let second_char = trigger.chars().nth(first_alphabetic + 1);
let mode: i32 = if let Some(first_char) = first_char { let mode: i32 = if let Some(first_char) = first_char {
if first_char.is_uppercase() { if first_char.is_uppercase() {
if let Some(second_char) = second_char { if let Some(second_char) = second_char {