diff --git a/README.md b/README.md index 219f68e..c1d14c2 100644 --- a/README.md +++ b/README.md @@ -10,20 +10,20 @@ The tool can also recognize comments attached to non-comment lines, like this: # Before: dependencies = [ # Keep sorted. - # TODO: remove this dependency. - 'ccc', - 'bbb', - 'aaa', -] -# After: -dependencies = [ # Keep sorted. + 'aaa', # TODO: remove this dependency. 'aaa', 'bbb', + 'bbb', + # TODO: remove this dependency. 'ccc', + 'ccc', +] ] ``` +# After: +dependencies = [ You can see more examples in the ./tests/e2e-tests/ directory. @@ -60,8 +60,8 @@ In Bazel files, keepsorted sorts lines within `[...]` blocks that start with `# ```bazel DEPENDENCIES = [ # Keep sorted - "b", "a", + "b", ] ``` diff --git a/src/lib.rs b/src/lib.rs index b65dce8..71859ce 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -6,8 +6,8 @@ use std::path::Path; pub mod strategies; +static RE_KEEP_SORTED: Lazy = Lazy::new(re_keyword_keep_sorted); static RE_IGNORE_FILE: Lazy = Lazy::new(re_keyword_ignore_file); - static RE_IGNORE_BLOCK: Lazy = Lazy::new(re_keyword_ignore_block); pub fn process_file(path: &Path, features: Vec) -> io::Result<()> { @@ -73,23 +73,6 @@ fn classify(path: &Path, features: Vec) -> Strategy { Strategy::Generic } -fn re_keyword_keep_sorted() -> Regex { - Regex::new( - r"(?i)^\s*(#|\/\/|#\s+keepsorted\s*:|\/\/\s+keepsorted\s*:)\s*keep\s+sorted\s*\.?\s*$", - ) - .expect("Failed to build regex for keep sorted") -} - -fn re_keyword_ignore_file() -> Regex { - Regex::new(r"(?i)^\s*(#|\/\/)\s*keepsorted\s*:\s*ignore\s+file\s*\.?\s*$") - .expect("Failed to build regex for ignore file") -} - -fn re_keyword_ignore_block() -> Regex { - Regex::new(r"(?i)^\s*(#|\/\/)\s*keepsorted\s*:\s*ignore\s+block\s*\.?\s*$") - .expect("Failed to build regex for ignore block") -} - fn is_ignore_file(lines: &[String]) -> bool { lines.iter().any(|x| RE_IGNORE_FILE.is_match(x)) } @@ -117,6 +100,13 @@ fn is_codeowners(path: &Path) -> bool { path.is_file() && path.file_name() == Some(std::ffi::OsStr::new("CODEOWNERS")) } +fn re_keyword_keep_sorted() -> Regex { + Regex::new( + r"(?i)^\s*(#|\/\/|#\s+keepsorted\s*:|\/\/\s+keepsorted\s*:)\s*keep\s+sorted\s*\.?\s*$", + ) + .expect("Failed to build regex for keep sorted") +} + #[test] fn test_re_keyword_keep_sorted() { let re = re_keyword_keep_sorted(); @@ -135,6 +125,11 @@ fn test_re_keyword_keep_sorted() { } } +fn re_keyword_ignore_file() -> Regex { + Regex::new(r"(?i)^\s*(#|\/\/)\s*keepsorted\s*:\s*ignore\s+file\s*\.?\s*$") + .expect("Failed to build regex for ignore file") +} + #[test] fn test_re_keyword_ignore_file() { let re = re_keyword_ignore_file(); @@ -150,6 +145,11 @@ fn test_re_keyword_ignore_file() { } } +fn re_keyword_ignore_block() -> Regex { + Regex::new(r"(?i)^\s*(#|\/\/)\s*keepsorted\s*:\s*ignore\s+block\s*\.?\s*$") + .expect("Failed to build regex for ignore block") +} + #[test] fn test_re_keyword_ignore_block() { let re = re_keyword_ignore_block(); diff --git a/src/strategies/generic.rs b/src/strategies/generic.rs index 57075d6..8051f5e 100644 --- a/src/strategies/generic.rs +++ b/src/strategies/generic.rs @@ -1,16 +1,15 @@ use std::io; -use crate::{is_ignore_block, re_keyword_keep_sorted}; +use crate::{is_ignore_block, RE_KEEP_SORTED}; pub(crate) fn process(lines: Vec) -> io::Result> { - let re = re_keyword_keep_sorted(); let mut output_lines: Vec = Vec::new(); let mut block = Vec::new(); let mut is_sorting_block = false; let mut is_ignore_block_prev_line = false; for line in lines { - if re.is_match(&line) { + if RE_KEEP_SORTED.is_match(&line) { if let Some(prev_line) = output_lines.last() { is_ignore_block_prev_line = is_ignore_block(&[prev_line.clone()]); }