|
1 | 1 | //! Checks that a list of items is in alphabetical order
|
2 | 2 | //!
|
3 |
| -//! To use, use the following annotation in the code: |
| 3 | +//! Use the following marker in the code: |
4 | 4 | //! ```rust
|
5 | 5 | //! // tidy-alphabetical-start
|
6 | 6 | //! fn aaa() {}
|
@@ -30,27 +30,25 @@ fn is_close_bracket(c: char) -> bool {
|
30 | 30 | }
|
31 | 31 |
|
32 | 32 | // Don't let tidy check this here :D
|
33 |
| -const START_COMMENT: &str = concat!("tidy-alphabetical", "-start"); |
34 |
| -const END_COMMENT: &str = "tidy-alphabetical-end"; |
| 33 | +const START_MARKER: &str = concat!("tidy-alphabetical", "-start"); |
| 34 | +const END_MARKER: &str = "tidy-alphabetical-end"; |
35 | 35 |
|
36 | 36 | fn check_section<'a>(
|
37 | 37 | file: impl Display,
|
38 | 38 | lines: impl Iterator<Item = (usize, &'a str)>,
|
39 | 39 | bad: &mut bool,
|
40 | 40 | ) {
|
41 |
| - let content_lines = lines.take_while(|(_, line)| !line.contains(END_COMMENT)); |
42 |
| - |
43 | 41 | let mut prev_line = String::new();
|
44 | 42 | let mut first_indent = None;
|
45 | 43 | let mut in_split_line = None;
|
46 | 44 |
|
47 |
| - for (line_idx, line) in content_lines { |
48 |
| - if line.contains(START_COMMENT) { |
49 |
| - tidy_error!( |
50 |
| - bad, |
51 |
| - "{file}:{} found `{START_COMMENT}` expecting `{END_COMMENT}`", |
52 |
| - line_idx |
53 |
| - ) |
| 45 | + for (line_idx, line) in lines { |
| 46 | + if line.contains(START_MARKER) { |
| 47 | + tidy_error!(bad, "{file}:{} found `{START_MARKER}` expecting `{END_MARKER}`", line_idx) |
| 48 | + } |
| 49 | + |
| 50 | + if line.contains(END_MARKER) { |
| 51 | + return; |
54 | 52 | }
|
55 | 53 |
|
56 | 54 | let indent = first_indent.unwrap_or_else(|| {
|
@@ -92,19 +90,18 @@ fn check_section<'a>(
|
92 | 90 |
|
93 | 91 | prev_line = line;
|
94 | 92 | }
|
| 93 | + |
| 94 | + tidy_error!(bad, "{file}: reached end of file expecting `{END_MARKER}`") |
95 | 95 | }
|
96 | 96 |
|
97 | 97 | pub fn check(path: &Path, bad: &mut bool) {
|
98 | 98 | walk(path, |path, _is_dir| filter_dirs(path), &mut |entry, contents| {
|
99 | 99 | let file = &entry.path().display();
|
100 | 100 |
|
101 |
| - let mut lines = contents.lines().enumerate().peekable(); |
| 101 | + let mut lines = contents.lines().enumerate(); |
102 | 102 | while let Some((_, line)) = lines.next() {
|
103 |
| - if line.contains(START_COMMENT) { |
| 103 | + if line.contains(START_MARKER) { |
104 | 104 | check_section(file, &mut lines, bad);
|
105 |
| - if lines.peek().is_none() { |
106 |
| - tidy_error!(bad, "{file}: reached end of file expecting `{END_COMMENT}`") |
107 |
| - } |
108 | 105 | }
|
109 | 106 | }
|
110 | 107 | });
|
|
0 commit comments