From b4a74cb6337451e48dd847e6b444425a52565ce0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20=C5=A0tancl?= Date: Mon, 11 Dec 2023 11:09:02 +0100 Subject: [PATCH] support scanning todo!() Rust macros --- samples/2.rs | 7 ++++++ src/scan.rs | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+) create mode 100644 samples/2.rs diff --git a/samples/2.rs b/samples/2.rs new file mode 100644 index 0000000..abd0066 --- /dev/null +++ b/samples/2.rs @@ -0,0 +1,7 @@ +fn foo() { + // Rust TODOs can only be generic + todo!("generic"); + todo!(); + todo!("@foo not category"); + todo!("00 not priority"); +} diff --git a/src/scan.rs b/src/scan.rs index 9eb788b..b251b6b 100644 --- a/src/scan.rs +++ b/src/scan.rs @@ -89,6 +89,7 @@ fn parse_priority(word: &str) -> Option { } } +/// Remove closing tags, comments, and whitespace fn clean_line<'a>(line: &'a str, delimiter_word: &str) -> &'a str { return line.split_once(delimiter_word).unwrap().1 .trim() @@ -154,6 +155,19 @@ pub fn scan_string(str: String, filename: PathBuf, entries: &mut Vec) { let text = clean_line(line, word); + if word.starts_with("todo!(") { + entries.push(Entry { + text: line.trim().to_string(), + location: Location { + file: filename.clone(), + line: line_num + 1, + }, + data: EntryData::Generic, + }); + + break; + } + // 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" { @@ -773,6 +787,55 @@ mod tests { }, entries[9]); } + #[test] + fn sample_test_rs() { + let mut entries: Vec = vec![]; + + let mut path = std::env::current_dir().unwrap(); + path.push("samples"); + path.push("2.rs"); + + scan_file(path.as_path(), &mut entries).unwrap(); + + assert_eq!(4, entries.len()); + + assert_eq!(Entry { + data: EntryData::Generic, + text: String::from("todo!(\"generic\");"), + location: Location { + file: path.clone(), + line: 3, + } + }, entries[0]); + + assert_eq!(Entry { + data: EntryData::Generic, + text: String::from("todo!();"), + location: Location { + file: path.clone(), + line: 4, + } + }, entries[1]); + + assert_eq!(Entry { + data: EntryData::Generic, + text: String::from("todo!(\"@foo not category\");"), + location: Location { + file: path.clone(), + line: 5, + } + }, entries[2]); + + assert_eq!(Entry { + data: EntryData::Generic, + text: String::from("todo!(\"00 not priority\");"), + location: Location { + file: path.clone(), + line: 6, + } + }, entries[3]); + } + #[test] fn todo_file_test() { let mut entries: Vec = vec![];