Skip to content

Commit

Permalink
update strategies
Browse files Browse the repository at this point in the history
  • Loading branch information
maksym-arutyunyan committed Aug 9, 2024
1 parent d3a8a00 commit 298ad61
Show file tree
Hide file tree
Showing 8 changed files with 335 additions and 18 deletions.
13 changes: 9 additions & 4 deletions src/strategies/bazel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ pub(crate) fn process(lines: Vec<String>) -> io::Result<Vec<String>> {
let mut block = Vec::new();
let mut is_scope = false;
let mut is_sorting_block = false;
let mut is_ignore_block_prev_line = false;

for line in lines {
// Trim the input line
Expand All @@ -24,13 +25,17 @@ pub(crate) fn process(lines: Vec<String>) -> io::Result<Vec<String>> {
output_lines.push(line);
} else if is_scope {
if re.is_match(&line) {
if let Some(prev_line) = output_lines.last() {
is_ignore_block_prev_line = is_ignore_block(&[prev_line.clone()]);
}
is_sorting_block = true;
output_lines.push(line);
} else if is_sorting_block
&& (line_without_comment.contains(']') || line.trim().is_empty())
{
block = sort(block, is_ignore_block_prev_line);
is_ignore_block_prev_line = false;
is_sorting_block = false;
block = sort(block);
output_lines.append(&mut block);
output_lines.push(line);
} else if is_sorting_block {
Expand All @@ -44,7 +49,7 @@ pub(crate) fn process(lines: Vec<String>) -> io::Result<Vec<String>> {
}

if is_sorting_block {
block = sort(block);
block = sort(block, is_ignore_block_prev_line);
output_lines.append(&mut block);
}

Expand All @@ -59,8 +64,8 @@ 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) {
fn sort(block: Vec<String>, is_ignore_block_prev_line: bool) -> Vec<String> {
if is_ignore_block_prev_line || is_ignore_block(&block) {
return block;
}
let n = block.len();
Expand Down
15 changes: 10 additions & 5 deletions src/strategies/cargo_toml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,27 @@ 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 output_lines: Vec<String> = 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 {
let trimmed = line.trim();
let line_without_comment = trimmed.split('#').next().unwrap_or("").trim();

if is_block_start(&line) {
if let Some(prev_line) = output_lines.last() {
is_ignore_block_prev_line = is_ignore_block(&[prev_line.clone()]);
}
is_sorting_block = true;
output_lines.push(line);
} else if is_sorting_block
&& (line.trim().is_empty() || line_without_comment.starts_with('['))
{
block = sort(block, is_ignore_block_prev_line);
is_ignore_block_prev_line = false;
is_sorting_block = false;
block = sort(block);
output_lines.append(&mut block);
output_lines.push(line);
} else if is_sorting_block {
Expand All @@ -29,7 +34,7 @@ pub(crate) fn process(lines: Vec<String>) -> io::Result<Vec<String>> {
}

if is_sorting_block {
block = sort(block);
block = sort(block, is_ignore_block_prev_line);
output_lines.append(&mut block);
}

Expand Down Expand Up @@ -64,8 +69,8 @@ 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) {
fn sort(block: Vec<String>, is_ignore_block_prev_line: bool) -> Vec<String> {
if is_ignore_block_prev_line || is_ignore_block(&block) {
return block;
}
let n = block.len();
Expand Down
15 changes: 10 additions & 5 deletions src/strategies/generic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,22 @@ 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))?;
let mut output_lines = Vec::new();
let mut output_lines: Vec<String> = 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 let Some(prev_line) = output_lines.last() {
is_ignore_block_prev_line = is_ignore_block(&[prev_line.clone()]);
}
is_sorting_block = true;
output_lines.push(line);
} else if is_sorting_block && line.trim().is_empty() {
block = sort(block, is_ignore_block_prev_line);
is_ignore_block_prev_line = false;
is_sorting_block = false;
block = sort(block);
output_lines.append(&mut block);
output_lines.push(line);
} else if is_sorting_block {
Expand All @@ -27,7 +32,7 @@ pub(crate) fn process(lines: Vec<String>) -> io::Result<Vec<String>> {
}

if is_sorting_block {
block = sort(block);
block = sort(block, is_ignore_block_prev_line);
output_lines.append(&mut block);
}

Expand All @@ -41,8 +46,8 @@ 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) {
fn sort(block: Vec<String>, is_ignore_block_prev_line: bool) -> Vec<String> {
if is_ignore_block_prev_line || is_ignore_block(&block) {
return block;
}
let n = block.len();
Expand Down
13 changes: 9 additions & 4 deletions src/strategies/gitignore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,24 @@ pub(crate) fn process(lines: Vec<String>) -> io::Result<Vec<String>> {
let mut output_lines = 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 !line.trim().is_empty() {
if is_single_line_comment(&line) {
// Skip opening comment.
output_lines.push(line);
} else {
if let Some(prev_line) = output_lines.last() {
is_ignore_block_prev_line = is_ignore_block(&[prev_line.clone()]);
}
is_sorting_block = true;
block.push(line);
}
} else if is_sorting_block {
block = sort(block, is_ignore_block_prev_line);
is_ignore_block_prev_line = false;
is_sorting_block = false;
block = sort(block);
output_lines.append(&mut block);
output_lines.push(line);
} else {
Expand All @@ -27,7 +32,7 @@ pub(crate) fn process(lines: Vec<String>) -> io::Result<Vec<String>> {
}

if is_sorting_block {
block = sort(block);
block = sort(block, is_ignore_block_prev_line);
output_lines.append(&mut block);
}

Expand All @@ -41,8 +46,8 @@ 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) {
fn sort(block: Vec<String>, is_ignore_block_prev_line: bool) -> Vec<String> {
if is_ignore_block_prev_line || is_ignore_block(&block) {
return block;
}
let n = block.len();
Expand Down
100 changes: 100 additions & 0 deletions tests/bazel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,106 @@ block_2 = [
);
}

#[test]
fn bazel_ignore_file() {
test_inner!(
Bazel,
r#"
# keepsorted:ignore-file
block_1 = [
# Keep sorted.
"b",
"a",
],
block_2 = [
# Keep sorted.
"y",
"x",
],
"#,
r#"
# keepsorted:ignore-file
block_1 = [
# Keep sorted.
"b",
"a",
],
block_2 = [
# Keep sorted.
"y",
"x",
],
"#
);
}

#[test]
fn bazel_ignore_block_inside() {
test_inner!(
Bazel,
r#"
block_1 = [
# Keep sorted.
# keepsorted:ignore-block
"b",
"a",
],
block_2 = [
# Keep sorted.
"y",
"x",
],
"#,
r#"
block_1 = [
# Keep sorted.
# keepsorted:ignore-block
"b",
"a",
],
block_2 = [
# Keep sorted.
"x",
"y",
],
"#
);
}

#[test]
fn bazel_ignore_block_before() {
test_inner!(
Bazel,
r#"
block_1 = [
# keepsorted:ignore-block
# Keep sorted.
"b",
"a",
],
block_2 = [
# Keep sorted.
"y",
"x",
],
"#,
r#"
block_1 = [
# keepsorted:ignore-block
# Keep sorted.
"b",
"a",
],
block_2 = [
# Keep sorted.
"x",
"y",
],
"#
);
}


#[test]
fn bazel_blocks_with_select() {
test_inner!(
Expand Down
81 changes: 81 additions & 0 deletions tests/cargo_toml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,87 @@ y = "4"
);
}

#[test]
fn cargo_toml_ignore_file() {
test_inner!(
CargoToml,
r#"
# keepsorted:ignore-file
[dependencies]
b = "2"
a = "1"
[dev-dependencies]
y = "4"
x = "3"
"#,
r#"
# keepsorted:ignore-file
[dependencies]
b = "2"
a = "1"
[dev-dependencies]
y = "4"
x = "3"
"#
);
}

#[test]
fn cargo_toml_ignore_block_inside() {
test_inner!(
CargoToml,
r#"
[dependencies]
# keepsorted:ignore-block
b = "2"
a = "1"
[dev-dependencies]
y = "4"
x = "3"
"#,
r#"
[dependencies]
# keepsorted:ignore-block
b = "2"
a = "1"
[dev-dependencies]
x = "3"
y = "4"
"#
);
}

#[test]
fn cargo_toml_ignore_block_before() {
test_inner!(
CargoToml,
r#"
# keepsorted:ignore-block
[dependencies]
b = "2"
a = "1"
[dev-dependencies]
y = "4"
x = "3"
"#,
r#"
# keepsorted:ignore-block
[dependencies]
b = "2"
a = "1"
[dev-dependencies]
x = "3"
y = "4"
"#
);
}

#[test]
fn cargo_toml_nested_list() {
test_inner!(
Expand Down
Loading

0 comments on commit 298ad61

Please sign in to comment.