Skip to content

Commit

Permalink
Fix config merge for context.
Browse files Browse the repository at this point in the history
  • Loading branch information
huacnlee committed Jun 20, 2024
1 parent 848225c commit 019fa14
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 89 deletions.
2 changes: 1 addition & 1 deletion .autocorrectrc.template
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
rules:
# Default rules: https://github.com/huacnlee/autocorrect/raw/main/autocorrect/.autocorrectrc.default
spellcheck: 2
# Enable or disable in spatial context
# Enable or disable in special context
context:
# Enable or disable to format codeblock in Markdown or AsciiDoc etc.
codeblock: 1
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,10 @@ rules:
halfwidth-punctuation: 1
# Spellcheck
spellcheck: 2
# Enable or disable in a specific context
context:
# Enable or disable to format codeblock in Markdown or AsciiDoc etc.
codeblock: 1
textRules:
# Config special rules for some texts
# For example, if we wants to let "Hello你好" just warning, and "Hi你好" to ignore
Expand Down
180 changes: 97 additions & 83 deletions autocorrect-website/public/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,98 +76,112 @@
}
}
},
"spellcheck": {
"default": {
"mode": null,
"words": []
},
"allOf": [
{
"$ref": "#/definitions/SpellcheckConfig"
}
]
},
"textRules": {
"context": {
"default": {},
"type": "object",
"description": "Config text rules, when AutoCorrect matches the text, it will apply the rules. \nFor instance for ignore some text:\n{ \"Hello你好\": \"off\" }",
"description": "Enable or disable in special context",
"additionalProperties": {
"$ref": "#/definitions/SeverityMode"
},
"properties": {
"codeblock": {
"description": "Enable or disable to format codeblock in Markdown or AsciiDoc etc.",
"default": 0,
"$ref": "#/definitions/SeverityMode"
}
},
"spellcheck": {
"default": {
"mode": null,
"words": []
},
"allOf": [
{
"$ref": "#/definitions/SpellcheckConfig"
}
]
},
"textRules": {
"default": {},
"type": "object",
"description": "Config text rules, when AutoCorrect matches the text, it will apply the rules. \nFor instance for ignore some text:\n{ \"Hello 你好\": \"off\" }",
"additionalProperties": {
"$ref": "#/definitions/SeverityMode"
}
}
}
},
"definitions": {
"SeverityMode": {
"type": [
"number",
"string"
],
"enum": [
"off",
0,
"error",
1,
"warning",
2
]
},
"FileType": {
"type": "string",
"enum": [
"html",
"yaml",
"sql",
"rust",
"ruby",
"elixir",
"go",
"javascript",
"css",
"json",
"python",
"objective_c",
"strings",
"csharp",
"swift",
"java",
"scala",
"kotlin",
"php",
"dart",
"markdown",
"latex",
"asciidoc",
"gettext",
"conf",
"c",
"xml",
"jupyter",
"text"
]
},
"SpellcheckConfig": {
"type": "object",
"properties": {
"mode": {
"description": "DEPRACTED: use `rules.spellcheck` instead",
"default": null,
"anyOf": [
{
"$ref": "#/definitions/SeverityMode"
},
{
"type": "null"
"definitions": {
"SeverityMode": {
"type": [
"number",
"string"
],
"enum": [
"off",
0,
"error",
1,
"warning",
2
]
},
"FileType": {
"type": "string",
"enum": [
"html",
"yaml",
"sql",
"rust",
"ruby",
"elixir",
"go",
"javascript",
"css",
"json",
"python",
"objective_c",
"strings",
"csharp",
"swift",
"java",
"scala",
"kotlin",
"php",
"dart",
"markdown",
"latex",
"asciidoc",
"gettext",
"conf",
"c",
"xml",
"jupyter",
"text"
]
},
"SpellcheckConfig": {
"type": "object",
"properties": {
"mode": {
"description": "DEPRACTED: use `rules.spellcheck` instead",
"default": null,
"anyOf": [
{
"$ref": "#/definitions/SeverityMode"
},
{
"type": "null"
}
]
},
"words": {
"default": [],
"type": "array",
"items": {
"type": "string"
}
]
},
"words": {
"default": [],
"type": "array",
"items": {
"type": "string"
}
}
}
}
}
}
23 changes: 19 additions & 4 deletions autocorrect/src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,11 +155,16 @@ impl Config {
}

// DEPRECATED: since 2.0.0, remove this in 3.0.0
if let Some(mode) = config.spellcheck.mode.clone() {
if let Some(mode) = config.spellcheck.mode {
println!("DEPRECATED: `spellcheck.mode` use `rules.spellcheck` instead since 2.0.0");
self.spellcheck.mode = Some(mode.clone());
self.spellcheck.mode = Some(mode);
self.rules.insert("spellcheck".to_string(), mode);
}

config.context.iter().for_each(|(k, v)| {
self.context.insert(k.to_owned(), v.to_owned());
});

config.text_rules.iter().for_each(|(k, v)| {
self.text_rules.insert(k.to_owned(), v.to_owned());
});
Expand Down Expand Up @@ -355,6 +360,10 @@ mod tests {
rules: map! {
"foo".to_owned() => SeverityMode::Error,
},
context: map! {
"foo".to_owned() => SeverityMode::Error,
"foo1".to_owned() => SeverityMode::Off,
},
text_rules: map! {
"a".to_owned() => SeverityMode::Off,
"hello".to_owned() => SeverityMode::Error
Expand All @@ -368,13 +377,16 @@ mod tests {
words: vec!["foo".to_string(), "bar".to_string(), "baz".to_string()],
..Default::default()
},
..Default::default()
};

let config1 = Config {
rules: map! {
"bar".to_owned() => SeverityMode::Warning,
},
context: map! {
"foo".to_owned() => SeverityMode::Warning,
"foo2".to_owned() => SeverityMode::Off,
},
text_rules: map! {
"world".to_owned() => SeverityMode::Off
},
Expand All @@ -387,7 +399,6 @@ mod tests {
words: vec!["foo1".to_string(), "bar1".to_string()],
..Default::default()
},
..Default::default()
};

config.merge(&config1).unwrap();
Expand All @@ -399,6 +410,10 @@ mod tests {
};
assert_eq!(new_rules, config.rules);

assert_eq!(config.context.get("foo"), Some(&SeverityMode::Warning));
assert_eq!(config.context.get("foo1"), Some(&SeverityMode::Off));
assert_eq!(config.context.get("foo2"), Some(&SeverityMode::Off));

let new_text_rules = map! {
"a".to_owned() => SeverityMode::Off,
"hello".to_owned() => SeverityMode::Error,
Expand Down
2 changes: 1 addition & 1 deletion autocorrect/src/rule/rule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ impl Rule {
let config = crate::Config::current();

if let Some(s) = config.rules.get(&self.name) {
s.clone()
*s
} else {
SeverityMode::Off
}
Expand Down

0 comments on commit 019fa14

Please sign in to comment.