feat(config): introduce the uppercase_style option for matches (related to #625)
This commit is contained in:
parent
1fad2039d6
commit
5a729f5810
|
@ -21,15 +21,18 @@ use crate::{
|
||||||
counter::next_id,
|
counter::next_id,
|
||||||
matches::{
|
matches::{
|
||||||
group::{path::resolve_imports, MatchGroup},
|
group::{path::resolve_imports, MatchGroup},
|
||||||
Match, Variable,
|
Match, UpperCasingStyle, Variable,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use log::warn;
|
use log::{error, warn};
|
||||||
use parse::YAMLMatchGroup;
|
use parse::YAMLMatchGroup;
|
||||||
use std::convert::{TryFrom, TryInto};
|
use std::convert::{TryFrom, TryInto};
|
||||||
|
|
||||||
use self::{parse::{YAMLMatch, YAMLVariable}, util::convert_params};
|
use self::{
|
||||||
|
parse::{YAMLMatch, YAMLVariable},
|
||||||
|
util::convert_params,
|
||||||
|
};
|
||||||
use crate::matches::{MatchCause, MatchEffect, TextEffect, TriggerCause};
|
use crate::matches::{MatchCause, MatchEffect, TextEffect, TriggerCause};
|
||||||
|
|
||||||
use super::Importer;
|
use super::Importer;
|
||||||
|
@ -89,6 +92,10 @@ impl TryFrom<YAMLMatch> for Match {
|
||||||
type Error = anyhow::Error;
|
type Error = anyhow::Error;
|
||||||
|
|
||||||
fn try_from(yaml_match: YAMLMatch) -> Result<Self, Self::Error> {
|
fn try_from(yaml_match: YAMLMatch) -> Result<Self, Self::Error> {
|
||||||
|
if yaml_match.uppercase_style.is_some() && yaml_match.propagate_case.is_none() {
|
||||||
|
warn!("specifying the 'uppercase_style' option without 'propagate_case' has no effect");
|
||||||
|
}
|
||||||
|
|
||||||
let triggers = if let Some(trigger) = yaml_match.trigger {
|
let triggers = if let Some(trigger) = yaml_match.trigger {
|
||||||
Some(vec![trigger])
|
Some(vec![trigger])
|
||||||
} else if let Some(triggers) = yaml_match.triggers {
|
} else if let Some(triggers) = yaml_match.triggers {
|
||||||
|
@ -97,6 +104,24 @@ impl TryFrom<YAMLMatch> for Match {
|
||||||
None
|
None
|
||||||
};
|
};
|
||||||
|
|
||||||
|
let uppercase_style = match yaml_match
|
||||||
|
.uppercase_style
|
||||||
|
.map(|s| s.to_lowercase())
|
||||||
|
.as_deref()
|
||||||
|
{
|
||||||
|
Some("uppercase") => UpperCasingStyle::Uppercase,
|
||||||
|
Some("capitalize") => UpperCasingStyle::Capitalize,
|
||||||
|
Some("capitalize_words") => UpperCasingStyle::CapitalizeWords,
|
||||||
|
Some(style) => {
|
||||||
|
error!(
|
||||||
|
"unrecognized uppercase_style: {:?}, falling back to the default",
|
||||||
|
style
|
||||||
|
);
|
||||||
|
TriggerCause::default().uppercase_style
|
||||||
|
}
|
||||||
|
_ => TriggerCause::default().uppercase_style,
|
||||||
|
};
|
||||||
|
|
||||||
let cause = if let Some(triggers) = triggers {
|
let cause = if let Some(triggers) = triggers {
|
||||||
MatchCause::Trigger(TriggerCause {
|
MatchCause::Trigger(TriggerCause {
|
||||||
triggers,
|
triggers,
|
||||||
|
@ -111,6 +136,7 @@ impl TryFrom<YAMLMatch> for Match {
|
||||||
propagate_case: yaml_match
|
propagate_case: yaml_match
|
||||||
.propagate_case
|
.propagate_case
|
||||||
.unwrap_or(TriggerCause::default().propagate_case),
|
.unwrap_or(TriggerCause::default().propagate_case),
|
||||||
|
uppercase_style,
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
MatchCause::None
|
MatchCause::None
|
||||||
|
@ -163,7 +189,10 @@ impl TryFrom<YAMLVariable> for Variable {
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
use crate::{matches::{Match, Params, Value}, util::tests::use_test_directory};
|
use crate::{
|
||||||
|
matches::{Match, Params, Value},
|
||||||
|
util::tests::use_test_directory,
|
||||||
|
};
|
||||||
use std::fs::create_dir_all;
|
use std::fs::create_dir_all;
|
||||||
|
|
||||||
fn create_match(yaml: &str) -> Result<Match> {
|
fn create_match(yaml: &str) -> Result<Match> {
|
||||||
|
@ -332,6 +361,73 @@ mod tests {
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn uppercase_style_maps_correctly() {
|
||||||
|
assert_eq!(
|
||||||
|
create_match(
|
||||||
|
r#"
|
||||||
|
trigger: "Hello"
|
||||||
|
replace: "world"
|
||||||
|
uppercase_style: "capitalize"
|
||||||
|
"#
|
||||||
|
)
|
||||||
|
.unwrap()
|
||||||
|
.cause
|
||||||
|
.into_trigger()
|
||||||
|
.unwrap()
|
||||||
|
.uppercase_style,
|
||||||
|
UpperCasingStyle::Capitalize,
|
||||||
|
);
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
create_match(
|
||||||
|
r#"
|
||||||
|
trigger: "Hello"
|
||||||
|
replace: "world"
|
||||||
|
uppercase_style: "capitalize_words"
|
||||||
|
"#
|
||||||
|
)
|
||||||
|
.unwrap()
|
||||||
|
.cause
|
||||||
|
.into_trigger()
|
||||||
|
.unwrap()
|
||||||
|
.uppercase_style,
|
||||||
|
UpperCasingStyle::CapitalizeWords,
|
||||||
|
);
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
create_match(
|
||||||
|
r#"
|
||||||
|
trigger: "Hello"
|
||||||
|
replace: "world"
|
||||||
|
uppercase_style: "uppercase"
|
||||||
|
"#
|
||||||
|
)
|
||||||
|
.unwrap()
|
||||||
|
.cause
|
||||||
|
.into_trigger()
|
||||||
|
.unwrap()
|
||||||
|
.uppercase_style,
|
||||||
|
UpperCasingStyle::Uppercase,
|
||||||
|
);
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
create_match(
|
||||||
|
r#"
|
||||||
|
trigger: "Hello"
|
||||||
|
replace: "world"
|
||||||
|
uppercase_style: "invalid"
|
||||||
|
"#
|
||||||
|
)
|
||||||
|
.unwrap()
|
||||||
|
.cause
|
||||||
|
.into_trigger()
|
||||||
|
.unwrap()
|
||||||
|
.uppercase_style,
|
||||||
|
UpperCasingStyle::Uppercase,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn vars_maps_correctly() {
|
fn vars_maps_correctly() {
|
||||||
let mut params = Params::new();
|
let mut params = Params::new();
|
||||||
|
|
|
@ -91,6 +91,9 @@ pub struct YAMLMatch {
|
||||||
#[serde(default)]
|
#[serde(default)]
|
||||||
pub propagate_case: Option<bool>,
|
pub propagate_case: Option<bool>,
|
||||||
|
|
||||||
|
#[serde(default)]
|
||||||
|
pub uppercase_style: Option<String>,
|
||||||
|
|
||||||
#[serde(default)]
|
#[serde(default)]
|
||||||
pub force_clipboard: Option<bool>,
|
pub force_clipboard: Option<bool>,
|
||||||
|
|
||||||
|
|
|
@ -50,7 +50,7 @@ impl Default for Match {
|
||||||
|
|
||||||
// Causes
|
// Causes
|
||||||
|
|
||||||
#[derive(Debug, Clone, Eq, Hash, PartialEq)]
|
#[derive(Debug, Clone, Eq, Hash, PartialEq, EnumAsInner)]
|
||||||
pub enum MatchCause {
|
pub enum MatchCause {
|
||||||
None,
|
None,
|
||||||
Trigger(TriggerCause),
|
Trigger(TriggerCause),
|
||||||
|
@ -66,6 +66,7 @@ pub struct TriggerCause {
|
||||||
pub right_word: bool,
|
pub right_word: bool,
|
||||||
|
|
||||||
pub propagate_case: bool,
|
pub propagate_case: bool,
|
||||||
|
pub uppercase_style: UpperCasingStyle,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for TriggerCause {
|
impl Default for TriggerCause {
|
||||||
|
@ -75,13 +76,21 @@ impl Default for TriggerCause {
|
||||||
left_word: false,
|
left_word: false,
|
||||||
right_word: false,
|
right_word: false,
|
||||||
propagate_case: false,
|
propagate_case: false,
|
||||||
|
uppercase_style: UpperCasingStyle::Uppercase,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
||||||
|
pub enum UpperCasingStyle {
|
||||||
|
Uppercase,
|
||||||
|
Capitalize,
|
||||||
|
CapitalizeWords,
|
||||||
|
}
|
||||||
|
|
||||||
// Effects
|
// Effects
|
||||||
|
|
||||||
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
#[derive(Debug, Clone, PartialEq, Eq, Hash, EnumAsInner)]
|
||||||
pub enum MatchEffect {
|
pub enum MatchEffect {
|
||||||
None,
|
None,
|
||||||
Text(TextEffect),
|
Text(TextEffect),
|
||||||
|
|
Loading…
Reference in New Issue
Block a user