From bacb7a94a4df8f03486e9f8b36afb5ec32bfa546 Mon Sep 17 00:00:00 2001 From: Rokas Puzonas Date: Thu, 4 Aug 2022 21:58:37 +0000 Subject: [PATCH] fix bug when todos have the message --- src/main.rs | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/src/main.rs b/src/main.rs index 5be3bb0..792f426 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,8 +3,6 @@ use comment_parser::CommentParser; use ignore::Walk; use regex::Regex; -// TODO: Line number location breaks when multiple remainders exist with same content - // TODO: Add language support for tsx, jsx, lua #[derive(Debug)] @@ -17,17 +15,15 @@ struct Reminder { static ALLOWED_VERBS: [&str; 3] = ["TODO", "FIXME", "BUG"]; -fn get_row_and_column(contents: &str, substr: &str) -> (u32, u32) { - let occurence = contents.find(substr).unwrap(); +fn get_row_and_column(contents: &str, substr: &str, from: usize) -> (u32, u32) { + let occurence = contents[from..].find(substr).unwrap() + from; let row = (contents.chars().take(occurence).filter(|c| *c == '\n').count()+1) as u32; let col = (occurence - contents[..occurence].rfind('\n').unwrap_or(0)) as u32; (row, col) } -fn list_reminders

(path: P) -> Vec -where - P: AsRef +fn list_reminders>(path: P) -> Vec { let mut reminders = vec![]; let reminder_pattern: Regex = Regex::new(r"^[A-Z]+.*:").unwrap(); @@ -45,6 +41,7 @@ where let rules = rules.unwrap(); let file_contents = fs::read_to_string(path).unwrap(); let parser = CommentParser::new(&file_contents, rules); + let mut search_from = 0; for comment in parser { let text = comment.text().trim_start(); for line in text.lines() { @@ -53,13 +50,15 @@ where let is_allowed = ALLOWED_VERBS.into_iter().any(|v| line.starts_with(v)); if !is_allowed { continue; } - let (row, col) = get_row_and_column(&file_contents, line); + let (row, col) = get_row_and_column(&file_contents, line, search_from); reminders.push(Reminder { file: path.to_path_buf(), row, col, contents: line.into() - }) + }); + + search_from += file_contents[search_from..].find(line).unwrap()+1; } } }