Skip to content

Offline, privacy-first grammar checker. Fast, open-source, Rust-powered

feat: implement `#` comment delimeter for full-line and line-end comments

hippietrail0c9b22041151
diff --git a/harper-core/src/spell/hunspell/mod.rs b/harper-core/src/spell/hunspell/mod.rs
index 0d9c0efc8..3d9a6dd85 100644
--- a/harper-core/src/spell/hunspell/mod.rs
+++ b/harper-core/src/spell/hunspell/mod.rs
@@ -41,7 +41,10 @@ mod tests {
pub const TEST_WORD_LIST_WITH_BLANK_LINES: &str = "4\n\nhello\n\ntry/B\nwork/AB\n\n\nblank/";
- pub const TEST_WORD_LIST_WITH_COMMENTS: &str = "4\nhello a word without attributes\ntry/B \t a word with empty attributes\nwork/AB\t a word with one attribute\nblank/ a word with two attributes";
+ pub const TEST_WORD_LIST_WITH_FULL_LINE_COMMENTS: &str =
+ "4\n#\nhello\n#with\ntry/B\nwork/AB\n# some\n# comments aded\nblank/";
+
+ pub const TEST_WORD_LIST_WITH_COMMENTS: &str = "4\nhello # a word without attributes\ntry/B \t # a word with empty attributes\nwork/AB\t #a word with one attribute\nblank/ #a word with two attributes";
pub const TEST_AFFIX_JSON: Lazy<serde_json::Value> = Lazy::new(|| {
json!({
@@ -90,8 +93,9 @@ mod tests {
#[test]
fn correctly_expands_test_files() {
let words = parse_word_list(TEST_WORD_LIST).unwrap();
-
- let attributes: HumanReadableAttributeList = serde_json::from_value(TEST_AFFIX_JSON.clone()).unwrap();
+
+ let attributes: HumanReadableAttributeList =
+ serde_json::from_value(TEST_AFFIX_JSON.clone()).unwrap();
let attributes = attributes.into_normal().unwrap();
let mut expanded = HashMap::new();
@@ -116,8 +120,35 @@ mod tests {
#[test]
fn correctly_expands_test_files_with_blank_lines() {
let words = parse_word_list(TEST_WORD_LIST_WITH_BLANK_LINES).unwrap();
-
- let attributes: HumanReadableAttributeList = serde_json::from_value(TEST_AFFIX_JSON.clone()).unwrap();
+
+ let attributes: HumanReadableAttributeList =
+ serde_json::from_value(TEST_AFFIX_JSON.clone()).unwrap();
+ let attributes = attributes.into_normal().unwrap();
+
+ let mut expanded = HashMap::new();
+
+ attributes.expand_marked_words(words, &mut expanded);
+ let expanded: HashSet<String> = expanded
+ .into_iter()
+ .map(|v| v.0.into_iter().collect())
+ .collect();
+
+ assert_eq!(
+ expanded,
+ vec![
+ "reworked", "rework", "tried", "try", "hello", "worked", "work", "blank"
+ ]
+ .into_iter()
+ .map(|v| v.into())
+ .collect()
+ )
+ }
+
+ fn correctly_expands_test_files_with_full_line_comments() {
+ let words = parse_word_list(TEST_WORD_LIST_WITH_FULL_LINE_COMMENTS).unwrap();
+
+ let attributes: HumanReadableAttributeList =
+ serde_json::from_value(TEST_AFFIX_JSON.clone()).unwrap();
let attributes = attributes.into_normal().unwrap();
let mut expanded = HashMap::new();
@@ -142,8 +173,9 @@ mod tests {
#[test]
fn correctly_expands_test_files_with_comments() {
let words = parse_word_list(TEST_WORD_LIST_WITH_COMMENTS).unwrap();
-
- let attributes: HumanReadableAttributeList = serde_json::from_value(TEST_AFFIX_JSON.clone()).unwrap();
+
+ let attributes: HumanReadableAttributeList =
+ serde_json::from_value(TEST_AFFIX_JSON.clone()).unwrap();
let attributes = attributes.into_normal().unwrap();
let mut expanded = HashMap::new();
diff --git a/harper-core/src/spell/hunspell/word_list.rs b/harper-core/src/spell/hunspell/word_list.rs
index c18f88f37..81e425f07 100644
--- a/harper-core/src/spell/hunspell/word_list.rs
+++ b/harper-core/src/spell/hunspell/word_list.rs
@@ -22,29 +22,25 @@ pub fn parse_word_list(source: &str) -> Result<Vec<MarkedWord>, Error> {
let mut words = Vec::with_capacity(approx_item_count);
for line in lines {
- // blank lines are allowed in the word list
- if line.is_empty() {
+ // Ignore blank lines and full line comments.
+ if line.is_empty() || line.starts_with('#') {
continue;
}
- let word: &str;
- let attr: Option<&str>; // = Option::None;
+ let entry: &str;
+ if let Some((entry_part, _comment_part)) = line.split_once('#') {
+ entry = entry_part.trim_end();
+ } else {
+ entry = line.trim_end();
+ }
- // check for attributes
- if let Some((word_part, attr_part)) = line.split_once('/') {
+ let word: &str;
+ let attr: Option<&str>;
+ if let Some((word_part, attr_part)) = entry.split_once('/') {
word = word_part;
-
- // word with attributes, throw away any trailing whitespace and comments
- attr = match attr_part.find(|c: char| char::is_ascii_whitespace(&c)) {
- Some(i) => Some(&attr_part[..i]),
- None => Some(&attr_part),
- };
+ attr = Some(attr_part);
} else {
- // word without attributes, throw away any trailing whitespace and comments
- word = match line.find(|c: char| char::is_ascii_whitespace(&c)) {
- Some(i) => &line[..i],
- None => line,
- };
+ word = entry;
attr = None;
}