Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
maksym-arutyunyan committed Aug 9, 2024
1 parent c16afda commit e6fd63b
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 29 deletions.
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down Expand Up @@ -60,8 +60,8 @@ In Bazel files, keepsorted sorts lines within `[...]` blocks that start with `#
```bazel
DEPENDENCIES = [
# Keep sorted
"b",
"a",
"b",
]
```

Expand Down
36 changes: 18 additions & 18 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ use std::path::Path;

pub mod strategies;

static RE_KEEP_SORTED: Lazy<Regex> = Lazy::new(re_keyword_keep_sorted);
static RE_IGNORE_FILE: Lazy<Regex> = Lazy::new(re_keyword_ignore_file);

static RE_IGNORE_BLOCK: Lazy<Regex> = Lazy::new(re_keyword_ignore_block);

pub fn process_file(path: &Path, features: Vec<String>) -> io::Result<()> {
Expand Down Expand Up @@ -73,23 +73,6 @@ fn classify(path: &Path, features: Vec<String>) -> 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))
}
Expand Down Expand Up @@ -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();
Expand All @@ -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();
Expand All @@ -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();
Expand Down
5 changes: 2 additions & 3 deletions src/strategies/generic.rs
Original file line number Diff line number Diff line change
@@ -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<String>) -> io::Result<Vec<String>> {
let re = re_keyword_keep_sorted();
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 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()]);
}
Expand Down

0 comments on commit e6fd63b

Please sign in to comment.