refactor a bit to better readability
This commit is contained in:
parent
5bd286e3e1
commit
e3e9386580
25
src/main.rs
25
src/main.rs
@ -5,6 +5,8 @@ use regex::Regex;
|
|||||||
|
|
||||||
// TODO: Line number location breaks when multiple remainders exist with same content
|
// TODO: Line number location breaks when multiple remainders exist with same content
|
||||||
|
|
||||||
|
// TODO: Add language support for tsx, jsx, lua
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
struct Reminder {
|
struct Reminder {
|
||||||
file: PathBuf,
|
file: PathBuf,
|
||||||
@ -13,13 +15,22 @@ struct Reminder {
|
|||||||
contents: String
|
contents: String
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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();
|
||||||
|
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<P>(path: P) -> Vec<Reminder>
|
fn list_reminders<P>(path: P) -> Vec<Reminder>
|
||||||
where
|
where
|
||||||
P: AsRef<Path>
|
P: AsRef<Path>
|
||||||
{
|
{
|
||||||
let mut reminders = vec![];
|
let mut reminders = vec![];
|
||||||
let reminder_pattern = Regex::new(r"^[A-Z]+:").unwrap();
|
let reminder_pattern: Regex = Regex::new(r"^[A-Z]+.*:").unwrap();
|
||||||
|
|
||||||
for result in Walk::new(path) {
|
for result in Walk::new(path) {
|
||||||
if result.is_err() { continue; }
|
if result.is_err() { continue; }
|
||||||
@ -36,20 +47,20 @@ where
|
|||||||
let parser = CommentParser::new(&file_contents, rules);
|
let parser = CommentParser::new(&file_contents, rules);
|
||||||
for comment in parser {
|
for comment in parser {
|
||||||
let text = comment.text().trim_start();
|
let text = comment.text().trim_start();
|
||||||
if reminder_pattern.is_match(text) {
|
if !reminder_pattern.is_match(text) { continue; }
|
||||||
let occurence = file_contents.find(text).unwrap();
|
|
||||||
let row = (file_contents.chars().take(occurence).filter(|c| *c == '\n').count()+1) as u32;
|
|
||||||
let col = (occurence - file_contents[..occurence].rfind('\n').unwrap_or(0)) as u32;
|
|
||||||
|
|
||||||
|
let is_allowed = ALLOWED_VERBS.into_iter().any(|v| text.starts_with(v));
|
||||||
|
if !is_allowed { continue; }
|
||||||
|
|
||||||
|
let (row, col) = get_row_and_column(&file_contents, text);
|
||||||
reminders.push(Reminder {
|
reminders.push(Reminder {
|
||||||
file: path.to_path_buf(),
|
file: path.to_path_buf(),
|
||||||
row,
|
row,
|
||||||
col,
|
col,
|
||||||
contents: text.to_string()
|
contents: text.lines().next().unwrap().into()
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
reminders
|
reminders
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user