Skip to content

Commit

Permalink
add ignore keywords
Browse files Browse the repository at this point in the history
  • Loading branch information
maksym-arutyunyan committed Aug 9, 2024
1 parent d9c3680 commit d3a8a00
Show file tree
Hide file tree
Showing 6 changed files with 136 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ pub enum Strategy {
}

pub fn process_lines(strategy: Strategy, lines: Vec<String>) -> io::Result<Vec<String>> {
if is_ignore_file(&lines) {
return Ok(lines);
}
match strategy {
Strategy::Generic => crate::strategies::generic::process(lines),
Strategy::Bazel => crate::strategies::bazel::process(lines),
Expand All @@ -64,6 +67,14 @@ fn classify(path: &Path, features: Vec<String>) -> 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"),
Expand Down
5 changes: 5 additions & 0 deletions src/strategies/bazel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>) -> io::Result<Vec<String>> {
let re = Regex::new(r"^\s*#\s*Keep\s*sorted\.\s*$")
.map_err(|e| io::Error::new(io::ErrorKind::InvalidInput, e))?;
Expand Down Expand Up @@ -58,6 +60,9 @@ struct Item {

/// Sorts a block of lines, keeping associated comments with their items.
fn sort(block: Vec<String>) -> Vec<String> {
if is_ignore_block(&block) {
return block;
}
let n = block.len();
let mut items = Vec::with_capacity(n);
let mut current_item = Item::default();
Expand Down
5 changes: 5 additions & 0 deletions src/strategies/cargo_toml.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use std::io;

use crate::is_ignore_block;

pub(crate) fn process(lines: Vec<String>) -> io::Result<Vec<String>> {
let mut output_lines = Vec::new();
let mut block = Vec::new();
Expand Down Expand Up @@ -63,6 +65,9 @@ struct Item {

/// Sorts a block of lines, keeping associated comments with their items.
fn sort(block: Vec<String>) -> Vec<String> {
if is_ignore_block(&block) {
return block;
}
let n = block.len();
let mut items = Vec::with_capacity(n);
let mut current_item = Item::default();
Expand Down
5 changes: 5 additions & 0 deletions src/strategies/generic.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use regex::Regex;
use std::io;

use crate::is_ignore_block;

pub(crate) fn process(lines: Vec<String>) -> io::Result<Vec<String>> {
let re = Regex::new(r"^\s*#\s*Keep\s*sorted\.\s*$")
.map_err(|e| io::Error::new(io::ErrorKind::InvalidInput, e))?;
Expand Down Expand Up @@ -40,6 +42,9 @@ struct Item {

/// Sorts a block of lines, keeping associated comments with their items.
fn sort(block: Vec<String>) -> Vec<String> {
if is_ignore_block(&block) {
return block;
}
let n = block.len();
let mut items = Vec::with_capacity(n);
let mut current_item = Item::default();
Expand Down
5 changes: 5 additions & 0 deletions src/strategies/gitignore.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use std::io;

use crate::is_ignore_block;

pub(crate) fn process(lines: Vec<String>) -> io::Result<Vec<String>> {
let mut output_lines = Vec::new();
let mut block = Vec::new();
Expand Down Expand Up @@ -40,6 +42,9 @@ struct Item {

/// Sorts a block of lines, keeping associated comments with their items.
fn sort(block: Vec<String>) -> Vec<String> {
if is_ignore_block(&block) {
return block;
}
let n = block.len();
let mut items = Vec::with_capacity(n);
let mut current_item = Item::default();
Expand Down
105 changes: 105 additions & 0 deletions tests/generic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down

0 comments on commit d3a8a00

Please sign in to comment.