From e83185a687bee418d3e98835dc09d2c7118953a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20=C5=A0tancl?= Date: Wed, 13 Mar 2024 20:35:48 +0100 Subject: [PATCH] Ignore colons (fix #2) --- samples/1.ts | 6 +++--- samples/README.md | 1 + samples/todo.md | 2 +- src/scan.rs | 31 +++++++++++++++++++++---------- 4 files changed, 26 insertions(+), 14 deletions(-) diff --git a/samples/1.ts b/samples/1.ts index 3670194..2349732 100644 --- a/samples/1.ts +++ b/samples/1.ts @@ -2,7 +2,7 @@ function add(foo: any, bar: any): any { // todo@types return foo + bar; } -function subtract(foo: any, bar: any): any { // todo@types add types +function subtract(foo: any, bar: any): any { // todo@types: add types return foo - bar; } @@ -24,12 +24,12 @@ function greet2(name: string) { // todo1 add return typehint console.log(`Hello ${name}`); } -function echo(str: string) { // todo2 add return typehint +function echo(str: string) { // todo2: add return typehint console.log(str); } // console.log('foo'); // todo -// todo generic todo 2 +// todo: generic todo 2 // TODO: generic todo 3 // todo11 invalid todo diff --git a/samples/README.md b/samples/README.md index 1594a68..c7a5dcb 100644 --- a/samples/README.md +++ b/samples/README.md @@ -18,6 +18,7 @@ def - abc - todo0 def + - todo00: ghi - [ ] bar - [ ] baz diff --git a/samples/todo.md b/samples/todo.md index 3cc4cc6..1e20633 100644 --- a/samples/todo.md +++ b/samples/todo.md @@ -3,7 +3,7 @@ - todo00 priority bar ## High priority -- todo0 a +- todo0: a - foo - [ ] bar diff --git a/src/scan.rs b/src/scan.rs index c4991a2..c5073aa 100644 --- a/src/scan.rs +++ b/src/scan.rs @@ -148,7 +148,7 @@ pub fn scan_string(str: String, filename: PathBuf, entries: &mut Vec) { continue; } - for word in line.split_whitespace() { + for mut word in line.split_whitespace() { if ! word.to_lowercase().starts_with("todo") { continue; } @@ -168,9 +168,11 @@ pub fn scan_string(str: String, filename: PathBuf, entries: &mut Vec) { break; } + word = word.trim_end_matches(':'); + // Handles: `todo`, `TODO`, `todo:`, `TODO:` // Also trims `"` and `'` to handle cases like `foo="bar todo"` - if word.to_lowercase().trim_end_matches(':').trim_end_matches('"').trim_end_matches('\'') == "todo" { + if word.to_lowercase().trim_end_matches('"').trim_end_matches('\'') == "todo" { entries.push(Entry { text: text.to_string(), location: Location { @@ -289,8 +291,8 @@ pub fn scan_todo_file(path: &Path, entries: &mut Vec) -> io::Result<()> { } for word in line.split_whitespace() { - if word.to_lowercase().starts_with("todo") && word.chars().any(|ch| PRIORITY_CHARS.contains(&ch)) { - if let Some(priority) = parse_priority(word) { + if word.to_lowercase().trim_end_matches(':').starts_with("todo") && word.chars().any(|ch| PRIORITY_CHARS.contains(&ch)) { + if let Some(priority) = parse_priority(word.trim_end_matches(':')) { entries.push(Entry { text: clean_line(line, word).to_string(), location: Location { @@ -360,8 +362,8 @@ pub fn scan_readme_file(path: &Path, entries: &mut Vec) -> io::Result<()> } for word in line.split_whitespace() { - if word.to_lowercase().starts_with("todo") && word.chars().any(|ch| PRIORITY_CHARS.contains(&ch)) { - if let Some(priority) = parse_priority(word) { + if word.to_lowercase().trim_end_matches(':').starts_with("todo") && word.chars().any(|ch| PRIORITY_CHARS.contains(&ch)) { + if let Some(priority) = parse_priority(word.trim_end_matches(':')) { entries.push(Entry { text: clean_line(line, word).to_string(), location: Location { @@ -926,7 +928,7 @@ mod tests { scan_readme_file(path.as_path(), &mut entries).unwrap(); - assert_eq!(4, entries.len()); + assert_eq!(5, entries.len()); assert_eq!(Entry { data: EntryData::Generic, @@ -947,8 +949,8 @@ mod tests { }, entries[1]); assert_eq!(Entry { - data: EntryData::Generic, - text: String::from("bar"), + data: EntryData::Priority(-1), + text: String::from("ghi"), location: Location { file: path.clone(), line: 21, @@ -957,11 +959,20 @@ mod tests { assert_eq!(Entry { data: EntryData::Generic, - text: String::from("baz"), + text: String::from("bar"), location: Location { file: path.clone(), line: 22, } }, entries[3]); + + assert_eq!(Entry { + data: EntryData::Generic, + text: String::from("baz"), + location: Location { + file: path.clone(), + line: 23, + } + }, entries[4]); } }