From d3a8a007728463c4e7151f28fc4f3ca6c1fa82f8 Mon Sep 17 00:00:00 2001 From: Maksym Arutyunyan Date: Fri, 9 Aug 2024 11:15:27 +0200 Subject: [PATCH] add ignore keywords --- src/lib.rs | 11 ++++ src/strategies/bazel.rs | 5 ++ src/strategies/cargo_toml.rs | 5 ++ src/strategies/generic.rs | 5 ++ src/strategies/gitignore.rs | 5 ++ tests/generic.rs | 105 +++++++++++++++++++++++++++++++++++ 6 files changed, 136 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index c0da577..2ab68fc 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -40,6 +40,9 @@ pub enum Strategy { } pub fn process_lines(strategy: Strategy, lines: Vec) -> io::Result> { + if is_ignore_file(&lines) { + return Ok(lines); + } match strategy { Strategy::Generic => crate::strategies::generic::process(lines), Strategy::Bazel => crate::strategies::bazel::process(lines), @@ -64,6 +67,14 @@ fn classify(path: &Path, features: Vec) -> Strategy { Strategy::Generic } +fn is_ignore_file(lines: &[String]) -> bool { + lines.iter().any(|line| line.contains("keepsorted:ignore-file")) +} + +fn is_ignore_block(lines: &[String]) -> bool { + lines.iter().any(|line| line.contains("keepsorted:ignore-block")) +} + fn is_bazel(path: &Path) -> bool { match path.extension().and_then(|s| s.to_str()) { Some(ext) => matches!(ext, "bazel" | "bzl" | "BUILD" | "WORKSPACE"), diff --git a/src/strategies/bazel.rs b/src/strategies/bazel.rs index 093d5e9..5783544 100644 --- a/src/strategies/bazel.rs +++ b/src/strategies/bazel.rs @@ -2,6 +2,8 @@ use regex::Regex; use std::cmp::Ordering; use std::io; +use crate::is_ignore_block; + pub(crate) fn process(lines: Vec) -> io::Result> { let re = Regex::new(r"^\s*#\s*Keep\s*sorted\.\s*$") .map_err(|e| io::Error::new(io::ErrorKind::InvalidInput, e))?; @@ -58,6 +60,9 @@ struct Item { /// Sorts a block of lines, keeping associated comments with their items. fn sort(block: Vec) -> Vec { + if is_ignore_block(&block) { + return block; + } let n = block.len(); let mut items = Vec::with_capacity(n); let mut current_item = Item::default(); diff --git a/src/strategies/cargo_toml.rs b/src/strategies/cargo_toml.rs index 51000df..24093a6 100644 --- a/src/strategies/cargo_toml.rs +++ b/src/strategies/cargo_toml.rs @@ -1,5 +1,7 @@ use std::io; +use crate::is_ignore_block; + pub(crate) fn process(lines: Vec) -> io::Result> { let mut output_lines = Vec::new(); let mut block = Vec::new(); @@ -63,6 +65,9 @@ struct Item { /// Sorts a block of lines, keeping associated comments with their items. fn sort(block: Vec) -> Vec { + if is_ignore_block(&block) { + return block; + } let n = block.len(); let mut items = Vec::with_capacity(n); let mut current_item = Item::default(); diff --git a/src/strategies/generic.rs b/src/strategies/generic.rs index 9f8f375..d4de28a 100644 --- a/src/strategies/generic.rs +++ b/src/strategies/generic.rs @@ -1,6 +1,8 @@ use regex::Regex; use std::io; +use crate::is_ignore_block; + pub(crate) fn process(lines: Vec) -> io::Result> { let re = Regex::new(r"^\s*#\s*Keep\s*sorted\.\s*$") .map_err(|e| io::Error::new(io::ErrorKind::InvalidInput, e))?; @@ -40,6 +42,9 @@ struct Item { /// Sorts a block of lines, keeping associated comments with their items. fn sort(block: Vec) -> Vec { + if is_ignore_block(&block) { + return block; + } let n = block.len(); let mut items = Vec::with_capacity(n); let mut current_item = Item::default(); diff --git a/src/strategies/gitignore.rs b/src/strategies/gitignore.rs index b1ec523..e8f5c7f 100644 --- a/src/strategies/gitignore.rs +++ b/src/strategies/gitignore.rs @@ -1,5 +1,7 @@ use std::io; +use crate::is_ignore_block; + pub(crate) fn process(lines: Vec) -> io::Result> { let mut output_lines = Vec::new(); let mut block = Vec::new(); @@ -40,6 +42,9 @@ struct Item { /// Sorts a block of lines, keeping associated comments with their items. fn sort(block: Vec) -> Vec { + if is_ignore_block(&block) { + return block; + } let n = block.len(); let mut items = Vec::with_capacity(n); let mut current_item = Item::default(); diff --git a/tests/generic.rs b/tests/generic.rs index f090528..8617f72 100644 --- a/tests/generic.rs +++ b/tests/generic.rs @@ -135,6 +135,111 @@ y ); } +#[test] +fn generic_ignore_file() { + test_inner!( + Generic, + r#" + keepsorted:ignore-file +# Keep sorted. +1b +1a + +# Keep sorted. +2b +2a + +# Keep sorted. +3b +3a + "#, + r#" + keepsorted:ignore-file +# Keep sorted. +1b +1a + +# Keep sorted. +2b +2a + +# Keep sorted. +3b +3a + "# + ); +} + +#[test] +fn generic_ignore_block_inside() { + test_inner!( + Generic, + r#" +# Keep sorted. +1b +1a + +# Keep sorted. + keepsorted:ignore-block +2b +2a + +# Keep sorted. +3b +3a + "#, + r#" +# Keep sorted. +1a +1b + +# Keep sorted. + keepsorted:ignore-block +2b +2a + +# Keep sorted. +3a +3b + "# + ); +} + +#[test] +fn generic_ignore_block_before() { + test_inner!( + Generic, + r#" +# Keep sorted. +1b +1a + + keepsorted:ignore-block +# Keep sorted. +2b +2a + +# Keep sorted. +3b +3a + "#, + r#" +# Keep sorted. +1a +1b + + keepsorted:ignore-block +# Keep sorted. +2b +2a + +# Keep sorted. +3a +3b + "# + ); +} + // TODO: move to the appropriate place. #[test] #[ignore]