pub struct LocList {
pub locs: Vec<Loc>,
}
Expand description
A list of Lines of Code
To sort it, you focus it, which specifies the area to sort, then you call sort it.
Fields§
§locs: Vec<Loc>
Implementations§
Source§impl LocList
impl LocList
pub fn read<R: BufRead>(reader: R, lang: Language) -> CsResult<Self>
pub fn read_str(s: &str, lang: Language) -> CsResult<LocList>
Sourcepub fn read_file<P: AsRef<Path>>(path: P, lang: Language) -> CsResult<LocList>
pub fn read_file<P: AsRef<Path>>(path: P, lang: Language) -> CsResult<LocList>
Examples found in repository?
20fn main() -> CsResult<()> {
21 let args = Args::parse();
22 let loc_list = LocList::read_file(&args.path, Language::Rust)?;
23 let Some(sort_around) = args.sort_around else {
24 loc_list.print_debug(" WHOLE FILE ");
25 return Ok(());
26 };
27 let focused = loc_list.focus_around_line_number(sort_around)?;
28 focused.print_debug();
29 let blocks = focused.clone().focus.into_blocks();
30 for (i, block) in blocks.iter().enumerate() {
31 block.print_debug(&format!(" BLOCK {i} "));
32 }
33 let loc_list = focused.sort();
34 println!("------------ RESULT ------------");
35 print!("{}", loc_list);
36 Ok(())
37}
More examples
67fn main() {
68 let args = Args::parse();
69 let files = get_all_rust_files(args.path, &args.include).unwrap();
70 eprintln!("Found {} rust files", files.len());
71 let mut no_complete_count = 0;
72 let mut errors = 0;
73 let mut ok_count = 0;
74 for file in files {
75 let loc_list = LocList::read_file(file.to_str().unwrap(), Language::Rust);
76 let loc_list = match loc_list {
77 Ok(loc_list) => loc_list,
78 Err(e) => {
79 eprintln!("{} {:?} : {}", "ERROR".red(), file, e);
80 errors += 1;
81 continue;
82 }
83 };
84 if loc_list.is_complete() {
85 ok_count += 1;
86 continue;
87 }
88 if !loc_list.has_content() {
89 eprintln!("{} {:?}", "EMPTY".yellow(), file);
90 ok_count += 1;
91 continue;
92 }
93 eprintln!("{} {:?}", "NOT COMPLETE".yellow(), file);
94 no_complete_count += 1;
95 }
96 eprintln!("OK files: {}", ok_count);
97 eprintln!("Erroring files: {}", errors);
98 eprintln!("Uncomplete files: {}", no_complete_count);
99}
86fn main() -> CsResult<()> {
87 let start = std::time::Instant::now();
88 let args = Args::parse();
89 let files = get_all_rust_files(args.path, &args.include, &args.exclude)?;
90 eprintln!("Found {} rust files", files.len());
91 let mut sorted_enum_count = 0;
92 let mut ok_files_count = 0;
93 let mut invalid_files_count = 0;
94 let mut incomplete_files_count = 0;
95 let mut excluded_enums_count = 0;
96 let mut modified_files_count = 0;
97 let mut empty_files_count = 0;
98 for file in &files {
99 let loc_list = LocList::read_file(file, Language::Rust);
100 let mut loc_list = match loc_list {
101 Ok(loc_list) => loc_list,
102 Err(e) => {
103 eprintln!("{} in {}: {:?}", "ERROR".red(), file.display(), e);
104 invalid_files_count += 1;
105 continue;
106 }
107 };
108 if !loc_list.has_content() {
109 empty_files_count += 1;
110 continue;
111 }
112 if !loc_list.is_complete() {
113 eprintln!(
114 "skipping {} ({})",
115 file.display(),
116 "not consistent enough".yellow()
117 );
118 incomplete_files_count += 1;
119 continue;
120 }
121 let mut modified = false;
122 let mut line_idx = 0;
123 ok_files_count += 1;
124 while line_idx + 2 < loc_list.len() {
125 let loc = &loc_list.locs[line_idx];
126 if !loc.starts_normal {
127 line_idx += 1;
128 continue;
129 }
130 let content = &loc.content;
131 let Some((_, name)) =
132 regex_captures!(r"^[\s\w()]*\benum\s+([^({]+)\s+\{\s**$", content)
133 else {
134 line_idx += 1;
135 continue;
136 };
137
138 // We look whether the annotations before the enum contain any one
139 // of the excluding keywords
140 let whole_enum_range = loc_list
141 .block_range_of_line_number(LineNumber::from_index(line_idx))
142 .unwrap();
143 let whole_enum_range = loc_list.trimmed_range(whole_enum_range);
144 let excluding_keyword = EXCLUDING_KEYWORDS.iter().find(|&keyword| {
145 loc_list.locs[whole_enum_range.start.to_index()..line_idx]
146 .iter()
147 .any(|loc| loc.sort_key.contains(keyword))
148 });
149 if let Some(excluding_keyword) = excluding_keyword {
150 eprintln!("skipping enum {} ({})", name, excluding_keyword.yellow());
151 excluded_enums_count += 1;
152 line_idx = whole_enum_range.end.to_index() + 1;
153 continue;
154 }
155 loc_list.print_range_debug(
156 &format!(" sorting enum {} ", name.blue()),
157 whole_enum_range,
158 );
159 let range = loc_list.range_around_line_index(line_idx + 1).unwrap();
160 loc_list.sort_range(range).unwrap();
161 line_idx = range.end.to_index() + 2;
162 sorted_enum_count += 1;
163 modified = true;
164 }
165 if modified {
166 loc_list.write_file(file)?;
167 eprintln!("wrote {}", file.display());
168 modified_files_count += 1;
169 }
170 }
171 eprintln!("\nDone in {:.3}s\n", start.elapsed().as_secs_f32());
172 eprintln!("I analyzed {} files", files.len());
173 let mut problems = Vec::new();
174 if empty_files_count > 0 {
175 problems.push(format!("{} empty files", empty_files_count));
176 }
177 if incomplete_files_count > 0 {
178 problems.push(format!("{} incomplete files", incomplete_files_count));
179 }
180 if invalid_files_count > 0 {
181 problems.push(format!("{} invalid files", invalid_files_count));
182 }
183 if problems.is_empty() {
184 eprintln!("All {} files were ok", ok_files_count);
185 } else {
186 eprintln!(
187 "{} files were OK but I encountered {}",
188 ok_files_count,
189 problems.join(", ")
190 );
191 }
192 if excluded_enums_count > 0 {
193 eprintln!(
194 "I excluded {} enums whose annotation contained excluding keywords",
195 excluded_enums_count
196 );
197 }
198 eprintln!(
199 "I sorted {} enums in {} files",
200 sorted_enum_count, modified_files_count
201 );
202 Ok(())
203}
Sourcepub fn write_file<P: AsRef<Path>>(&self, path: P) -> CsResult<()>
pub fn write_file<P: AsRef<Path>>(&self, path: P) -> CsResult<()>
Examples found in repository?
86fn main() -> CsResult<()> {
87 let start = std::time::Instant::now();
88 let args = Args::parse();
89 let files = get_all_rust_files(args.path, &args.include, &args.exclude)?;
90 eprintln!("Found {} rust files", files.len());
91 let mut sorted_enum_count = 0;
92 let mut ok_files_count = 0;
93 let mut invalid_files_count = 0;
94 let mut incomplete_files_count = 0;
95 let mut excluded_enums_count = 0;
96 let mut modified_files_count = 0;
97 let mut empty_files_count = 0;
98 for file in &files {
99 let loc_list = LocList::read_file(file, Language::Rust);
100 let mut loc_list = match loc_list {
101 Ok(loc_list) => loc_list,
102 Err(e) => {
103 eprintln!("{} in {}: {:?}", "ERROR".red(), file.display(), e);
104 invalid_files_count += 1;
105 continue;
106 }
107 };
108 if !loc_list.has_content() {
109 empty_files_count += 1;
110 continue;
111 }
112 if !loc_list.is_complete() {
113 eprintln!(
114 "skipping {} ({})",
115 file.display(),
116 "not consistent enough".yellow()
117 );
118 incomplete_files_count += 1;
119 continue;
120 }
121 let mut modified = false;
122 let mut line_idx = 0;
123 ok_files_count += 1;
124 while line_idx + 2 < loc_list.len() {
125 let loc = &loc_list.locs[line_idx];
126 if !loc.starts_normal {
127 line_idx += 1;
128 continue;
129 }
130 let content = &loc.content;
131 let Some((_, name)) =
132 regex_captures!(r"^[\s\w()]*\benum\s+([^({]+)\s+\{\s**$", content)
133 else {
134 line_idx += 1;
135 continue;
136 };
137
138 // We look whether the annotations before the enum contain any one
139 // of the excluding keywords
140 let whole_enum_range = loc_list
141 .block_range_of_line_number(LineNumber::from_index(line_idx))
142 .unwrap();
143 let whole_enum_range = loc_list.trimmed_range(whole_enum_range);
144 let excluding_keyword = EXCLUDING_KEYWORDS.iter().find(|&keyword| {
145 loc_list.locs[whole_enum_range.start.to_index()..line_idx]
146 .iter()
147 .any(|loc| loc.sort_key.contains(keyword))
148 });
149 if let Some(excluding_keyword) = excluding_keyword {
150 eprintln!("skipping enum {} ({})", name, excluding_keyword.yellow());
151 excluded_enums_count += 1;
152 line_idx = whole_enum_range.end.to_index() + 1;
153 continue;
154 }
155 loc_list.print_range_debug(
156 &format!(" sorting enum {} ", name.blue()),
157 whole_enum_range,
158 );
159 let range = loc_list.range_around_line_index(line_idx + 1).unwrap();
160 loc_list.sort_range(range).unwrap();
161 line_idx = range.end.to_index() + 2;
162 sorted_enum_count += 1;
163 modified = true;
164 }
165 if modified {
166 loc_list.write_file(file)?;
167 eprintln!("wrote {}", file.display());
168 modified_files_count += 1;
169 }
170 }
171 eprintln!("\nDone in {:.3}s\n", start.elapsed().as_secs_f32());
172 eprintln!("I analyzed {} files", files.len());
173 let mut problems = Vec::new();
174 if empty_files_count > 0 {
175 problems.push(format!("{} empty files", empty_files_count));
176 }
177 if incomplete_files_count > 0 {
178 problems.push(format!("{} incomplete files", incomplete_files_count));
179 }
180 if invalid_files_count > 0 {
181 problems.push(format!("{} invalid files", invalid_files_count));
182 }
183 if problems.is_empty() {
184 eprintln!("All {} files were ok", ok_files_count);
185 } else {
186 eprintln!(
187 "{} files were OK but I encountered {}",
188 ok_files_count,
189 problems.join(", ")
190 );
191 }
192 if excluded_enums_count > 0 {
193 eprintln!(
194 "I excluded {} enums whose annotation contained excluding keywords",
195 excluded_enums_count
196 );
197 }
198 eprintln!(
199 "I sorted {} enums in {} files",
200 sorted_enum_count, modified_files_count
201 );
202 Ok(())
203}
Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Examples found in repository?
86fn main() -> CsResult<()> {
87 let start = std::time::Instant::now();
88 let args = Args::parse();
89 let files = get_all_rust_files(args.path, &args.include, &args.exclude)?;
90 eprintln!("Found {} rust files", files.len());
91 let mut sorted_enum_count = 0;
92 let mut ok_files_count = 0;
93 let mut invalid_files_count = 0;
94 let mut incomplete_files_count = 0;
95 let mut excluded_enums_count = 0;
96 let mut modified_files_count = 0;
97 let mut empty_files_count = 0;
98 for file in &files {
99 let loc_list = LocList::read_file(file, Language::Rust);
100 let mut loc_list = match loc_list {
101 Ok(loc_list) => loc_list,
102 Err(e) => {
103 eprintln!("{} in {}: {:?}", "ERROR".red(), file.display(), e);
104 invalid_files_count += 1;
105 continue;
106 }
107 };
108 if !loc_list.has_content() {
109 empty_files_count += 1;
110 continue;
111 }
112 if !loc_list.is_complete() {
113 eprintln!(
114 "skipping {} ({})",
115 file.display(),
116 "not consistent enough".yellow()
117 );
118 incomplete_files_count += 1;
119 continue;
120 }
121 let mut modified = false;
122 let mut line_idx = 0;
123 ok_files_count += 1;
124 while line_idx + 2 < loc_list.len() {
125 let loc = &loc_list.locs[line_idx];
126 if !loc.starts_normal {
127 line_idx += 1;
128 continue;
129 }
130 let content = &loc.content;
131 let Some((_, name)) =
132 regex_captures!(r"^[\s\w()]*\benum\s+([^({]+)\s+\{\s**$", content)
133 else {
134 line_idx += 1;
135 continue;
136 };
137
138 // We look whether the annotations before the enum contain any one
139 // of the excluding keywords
140 let whole_enum_range = loc_list
141 .block_range_of_line_number(LineNumber::from_index(line_idx))
142 .unwrap();
143 let whole_enum_range = loc_list.trimmed_range(whole_enum_range);
144 let excluding_keyword = EXCLUDING_KEYWORDS.iter().find(|&keyword| {
145 loc_list.locs[whole_enum_range.start.to_index()..line_idx]
146 .iter()
147 .any(|loc| loc.sort_key.contains(keyword))
148 });
149 if let Some(excluding_keyword) = excluding_keyword {
150 eprintln!("skipping enum {} ({})", name, excluding_keyword.yellow());
151 excluded_enums_count += 1;
152 line_idx = whole_enum_range.end.to_index() + 1;
153 continue;
154 }
155 loc_list.print_range_debug(
156 &format!(" sorting enum {} ", name.blue()),
157 whole_enum_range,
158 );
159 let range = loc_list.range_around_line_index(line_idx + 1).unwrap();
160 loc_list.sort_range(range).unwrap();
161 line_idx = range.end.to_index() + 2;
162 sorted_enum_count += 1;
163 modified = true;
164 }
165 if modified {
166 loc_list.write_file(file)?;
167 eprintln!("wrote {}", file.display());
168 modified_files_count += 1;
169 }
170 }
171 eprintln!("\nDone in {:.3}s\n", start.elapsed().as_secs_f32());
172 eprintln!("I analyzed {} files", files.len());
173 let mut problems = Vec::new();
174 if empty_files_count > 0 {
175 problems.push(format!("{} empty files", empty_files_count));
176 }
177 if incomplete_files_count > 0 {
178 problems.push(format!("{} incomplete files", incomplete_files_count));
179 }
180 if invalid_files_count > 0 {
181 problems.push(format!("{} invalid files", invalid_files_count));
182 }
183 if problems.is_empty() {
184 eprintln!("All {} files were ok", ok_files_count);
185 } else {
186 eprintln!(
187 "{} files were OK but I encountered {}",
188 ok_files_count,
189 problems.join(", ")
190 );
191 }
192 if excluded_enums_count > 0 {
193 eprintln!(
194 "I excluded {} enums whose annotation contained excluding keywords",
195 excluded_enums_count
196 );
197 }
198 eprintln!(
199 "I sorted {} enums in {} files",
200 sorted_enum_count, modified_files_count
201 );
202 Ok(())
203}
Sourcepub fn sort_range(&mut self, range: LineNumberRange) -> CsResult<()>
pub fn sort_range(&mut self, range: LineNumberRange) -> CsResult<()>
Examples found in repository?
86fn main() -> CsResult<()> {
87 let start = std::time::Instant::now();
88 let args = Args::parse();
89 let files = get_all_rust_files(args.path, &args.include, &args.exclude)?;
90 eprintln!("Found {} rust files", files.len());
91 let mut sorted_enum_count = 0;
92 let mut ok_files_count = 0;
93 let mut invalid_files_count = 0;
94 let mut incomplete_files_count = 0;
95 let mut excluded_enums_count = 0;
96 let mut modified_files_count = 0;
97 let mut empty_files_count = 0;
98 for file in &files {
99 let loc_list = LocList::read_file(file, Language::Rust);
100 let mut loc_list = match loc_list {
101 Ok(loc_list) => loc_list,
102 Err(e) => {
103 eprintln!("{} in {}: {:?}", "ERROR".red(), file.display(), e);
104 invalid_files_count += 1;
105 continue;
106 }
107 };
108 if !loc_list.has_content() {
109 empty_files_count += 1;
110 continue;
111 }
112 if !loc_list.is_complete() {
113 eprintln!(
114 "skipping {} ({})",
115 file.display(),
116 "not consistent enough".yellow()
117 );
118 incomplete_files_count += 1;
119 continue;
120 }
121 let mut modified = false;
122 let mut line_idx = 0;
123 ok_files_count += 1;
124 while line_idx + 2 < loc_list.len() {
125 let loc = &loc_list.locs[line_idx];
126 if !loc.starts_normal {
127 line_idx += 1;
128 continue;
129 }
130 let content = &loc.content;
131 let Some((_, name)) =
132 regex_captures!(r"^[\s\w()]*\benum\s+([^({]+)\s+\{\s**$", content)
133 else {
134 line_idx += 1;
135 continue;
136 };
137
138 // We look whether the annotations before the enum contain any one
139 // of the excluding keywords
140 let whole_enum_range = loc_list
141 .block_range_of_line_number(LineNumber::from_index(line_idx))
142 .unwrap();
143 let whole_enum_range = loc_list.trimmed_range(whole_enum_range);
144 let excluding_keyword = EXCLUDING_KEYWORDS.iter().find(|&keyword| {
145 loc_list.locs[whole_enum_range.start.to_index()..line_idx]
146 .iter()
147 .any(|loc| loc.sort_key.contains(keyword))
148 });
149 if let Some(excluding_keyword) = excluding_keyword {
150 eprintln!("skipping enum {} ({})", name, excluding_keyword.yellow());
151 excluded_enums_count += 1;
152 line_idx = whole_enum_range.end.to_index() + 1;
153 continue;
154 }
155 loc_list.print_range_debug(
156 &format!(" sorting enum {} ", name.blue()),
157 whole_enum_range,
158 );
159 let range = loc_list.range_around_line_index(line_idx + 1).unwrap();
160 loc_list.sort_range(range).unwrap();
161 line_idx = range.end.to_index() + 2;
162 sorted_enum_count += 1;
163 modified = true;
164 }
165 if modified {
166 loc_list.write_file(file)?;
167 eprintln!("wrote {}", file.display());
168 modified_files_count += 1;
169 }
170 }
171 eprintln!("\nDone in {:.3}s\n", start.elapsed().as_secs_f32());
172 eprintln!("I analyzed {} files", files.len());
173 let mut problems = Vec::new();
174 if empty_files_count > 0 {
175 problems.push(format!("{} empty files", empty_files_count));
176 }
177 if incomplete_files_count > 0 {
178 problems.push(format!("{} incomplete files", incomplete_files_count));
179 }
180 if invalid_files_count > 0 {
181 problems.push(format!("{} invalid files", invalid_files_count));
182 }
183 if problems.is_empty() {
184 eprintln!("All {} files were ok", ok_files_count);
185 } else {
186 eprintln!(
187 "{} files were OK but I encountered {}",
188 ok_files_count,
189 problems.join(", ")
190 );
191 }
192 if excluded_enums_count > 0 {
193 eprintln!(
194 "I excluded {} enums whose annotation contained excluding keywords",
195 excluded_enums_count
196 );
197 }
198 eprintln!(
199 "I sorted {} enums in {} files",
200 sorted_enum_count, modified_files_count
201 );
202 Ok(())
203}
pub fn sort_around_line_index(&mut self, line_index: LineIndex) -> CsResult<()>
pub fn sort_around_line_number( &mut self, line_number: LineNumber, ) -> CsResult<()>
pub fn focus_all(self) -> CsResult<Focused>
pub fn focus(self, range: LineNumberRange) -> CsResult<Focused>
pub fn focus_around_line_index(self, line_idx: LineIndex) -> CsResult<Focused>
Sourcepub fn focus_around_line_number(
self,
line_number: LineNumber,
) -> CsResult<Focused>
pub fn focus_around_line_number( self, line_number: LineNumber, ) -> CsResult<Focused>
Examples found in repository?
20fn main() -> CsResult<()> {
21 let args = Args::parse();
22 let loc_list = LocList::read_file(&args.path, Language::Rust)?;
23 let Some(sort_around) = args.sort_around else {
24 loc_list.print_debug(" WHOLE FILE ");
25 return Ok(());
26 };
27 let focused = loc_list.focus_around_line_number(sort_around)?;
28 focused.print_debug();
29 let blocks = focused.clone().focus.into_blocks();
30 for (i, block) in blocks.iter().enumerate() {
31 block.print_debug(&format!(" BLOCK {i} "));
32 }
33 let loc_list = focused.sort();
34 println!("------------ RESULT ------------");
35 print!("{}", loc_list);
36 Ok(())
37}
pub fn line_at_number(&self, line_number: LineNumber) -> Option<&Loc>
Sourcepub fn print_range_debug(&self, label: &str, range: LineNumberRange)
pub fn print_range_debug(&self, label: &str, range: LineNumberRange)
Examples found in repository?
86fn main() -> CsResult<()> {
87 let start = std::time::Instant::now();
88 let args = Args::parse();
89 let files = get_all_rust_files(args.path, &args.include, &args.exclude)?;
90 eprintln!("Found {} rust files", files.len());
91 let mut sorted_enum_count = 0;
92 let mut ok_files_count = 0;
93 let mut invalid_files_count = 0;
94 let mut incomplete_files_count = 0;
95 let mut excluded_enums_count = 0;
96 let mut modified_files_count = 0;
97 let mut empty_files_count = 0;
98 for file in &files {
99 let loc_list = LocList::read_file(file, Language::Rust);
100 let mut loc_list = match loc_list {
101 Ok(loc_list) => loc_list,
102 Err(e) => {
103 eprintln!("{} in {}: {:?}", "ERROR".red(), file.display(), e);
104 invalid_files_count += 1;
105 continue;
106 }
107 };
108 if !loc_list.has_content() {
109 empty_files_count += 1;
110 continue;
111 }
112 if !loc_list.is_complete() {
113 eprintln!(
114 "skipping {} ({})",
115 file.display(),
116 "not consistent enough".yellow()
117 );
118 incomplete_files_count += 1;
119 continue;
120 }
121 let mut modified = false;
122 let mut line_idx = 0;
123 ok_files_count += 1;
124 while line_idx + 2 < loc_list.len() {
125 let loc = &loc_list.locs[line_idx];
126 if !loc.starts_normal {
127 line_idx += 1;
128 continue;
129 }
130 let content = &loc.content;
131 let Some((_, name)) =
132 regex_captures!(r"^[\s\w()]*\benum\s+([^({]+)\s+\{\s**$", content)
133 else {
134 line_idx += 1;
135 continue;
136 };
137
138 // We look whether the annotations before the enum contain any one
139 // of the excluding keywords
140 let whole_enum_range = loc_list
141 .block_range_of_line_number(LineNumber::from_index(line_idx))
142 .unwrap();
143 let whole_enum_range = loc_list.trimmed_range(whole_enum_range);
144 let excluding_keyword = EXCLUDING_KEYWORDS.iter().find(|&keyword| {
145 loc_list.locs[whole_enum_range.start.to_index()..line_idx]
146 .iter()
147 .any(|loc| loc.sort_key.contains(keyword))
148 });
149 if let Some(excluding_keyword) = excluding_keyword {
150 eprintln!("skipping enum {} ({})", name, excluding_keyword.yellow());
151 excluded_enums_count += 1;
152 line_idx = whole_enum_range.end.to_index() + 1;
153 continue;
154 }
155 loc_list.print_range_debug(
156 &format!(" sorting enum {} ", name.blue()),
157 whole_enum_range,
158 );
159 let range = loc_list.range_around_line_index(line_idx + 1).unwrap();
160 loc_list.sort_range(range).unwrap();
161 line_idx = range.end.to_index() + 2;
162 sorted_enum_count += 1;
163 modified = true;
164 }
165 if modified {
166 loc_list.write_file(file)?;
167 eprintln!("wrote {}", file.display());
168 modified_files_count += 1;
169 }
170 }
171 eprintln!("\nDone in {:.3}s\n", start.elapsed().as_secs_f32());
172 eprintln!("I analyzed {} files", files.len());
173 let mut problems = Vec::new();
174 if empty_files_count > 0 {
175 problems.push(format!("{} empty files", empty_files_count));
176 }
177 if incomplete_files_count > 0 {
178 problems.push(format!("{} incomplete files", incomplete_files_count));
179 }
180 if invalid_files_count > 0 {
181 problems.push(format!("{} invalid files", invalid_files_count));
182 }
183 if problems.is_empty() {
184 eprintln!("All {} files were ok", ok_files_count);
185 } else {
186 eprintln!(
187 "{} files were OK but I encountered {}",
188 ok_files_count,
189 problems.join(", ")
190 );
191 }
192 if excluded_enums_count > 0 {
193 eprintln!(
194 "I excluded {} enums whose annotation contained excluding keywords",
195 excluded_enums_count
196 );
197 }
198 eprintln!(
199 "I sorted {} enums in {} files",
200 sorted_enum_count, modified_files_count
201 );
202 Ok(())
203}
Sourcepub fn print_debug(&self, label: &str)
pub fn print_debug(&self, label: &str)
Examples found in repository?
20fn main() -> CsResult<()> {
21 let args = Args::parse();
22 let loc_list = LocList::read_file(&args.path, Language::Rust)?;
23 let Some(sort_around) = args.sort_around else {
24 loc_list.print_debug(" WHOLE FILE ");
25 return Ok(());
26 };
27 let focused = loc_list.focus_around_line_number(sort_around)?;
28 focused.print_debug();
29 let blocks = focused.clone().focus.into_blocks();
30 for (i, block) in blocks.iter().enumerate() {
31 block.print_debug(&format!(" BLOCK {i} "));
32 }
33 let loc_list = focused.sort();
34 println!("------------ RESULT ------------");
35 print!("{}", loc_list);
36 Ok(())
37}
Sourcepub fn trimmed_range(&self, range: LineNumberRange) -> LineNumberRange
pub fn trimmed_range(&self, range: LineNumberRange) -> LineNumberRange
Examples found in repository?
86fn main() -> CsResult<()> {
87 let start = std::time::Instant::now();
88 let args = Args::parse();
89 let files = get_all_rust_files(args.path, &args.include, &args.exclude)?;
90 eprintln!("Found {} rust files", files.len());
91 let mut sorted_enum_count = 0;
92 let mut ok_files_count = 0;
93 let mut invalid_files_count = 0;
94 let mut incomplete_files_count = 0;
95 let mut excluded_enums_count = 0;
96 let mut modified_files_count = 0;
97 let mut empty_files_count = 0;
98 for file in &files {
99 let loc_list = LocList::read_file(file, Language::Rust);
100 let mut loc_list = match loc_list {
101 Ok(loc_list) => loc_list,
102 Err(e) => {
103 eprintln!("{} in {}: {:?}", "ERROR".red(), file.display(), e);
104 invalid_files_count += 1;
105 continue;
106 }
107 };
108 if !loc_list.has_content() {
109 empty_files_count += 1;
110 continue;
111 }
112 if !loc_list.is_complete() {
113 eprintln!(
114 "skipping {} ({})",
115 file.display(),
116 "not consistent enough".yellow()
117 );
118 incomplete_files_count += 1;
119 continue;
120 }
121 let mut modified = false;
122 let mut line_idx = 0;
123 ok_files_count += 1;
124 while line_idx + 2 < loc_list.len() {
125 let loc = &loc_list.locs[line_idx];
126 if !loc.starts_normal {
127 line_idx += 1;
128 continue;
129 }
130 let content = &loc.content;
131 let Some((_, name)) =
132 regex_captures!(r"^[\s\w()]*\benum\s+([^({]+)\s+\{\s**$", content)
133 else {
134 line_idx += 1;
135 continue;
136 };
137
138 // We look whether the annotations before the enum contain any one
139 // of the excluding keywords
140 let whole_enum_range = loc_list
141 .block_range_of_line_number(LineNumber::from_index(line_idx))
142 .unwrap();
143 let whole_enum_range = loc_list.trimmed_range(whole_enum_range);
144 let excluding_keyword = EXCLUDING_KEYWORDS.iter().find(|&keyword| {
145 loc_list.locs[whole_enum_range.start.to_index()..line_idx]
146 .iter()
147 .any(|loc| loc.sort_key.contains(keyword))
148 });
149 if let Some(excluding_keyword) = excluding_keyword {
150 eprintln!("skipping enum {} ({})", name, excluding_keyword.yellow());
151 excluded_enums_count += 1;
152 line_idx = whole_enum_range.end.to_index() + 1;
153 continue;
154 }
155 loc_list.print_range_debug(
156 &format!(" sorting enum {} ", name.blue()),
157 whole_enum_range,
158 );
159 let range = loc_list.range_around_line_index(line_idx + 1).unwrap();
160 loc_list.sort_range(range).unwrap();
161 line_idx = range.end.to_index() + 2;
162 sorted_enum_count += 1;
163 modified = true;
164 }
165 if modified {
166 loc_list.write_file(file)?;
167 eprintln!("wrote {}", file.display());
168 modified_files_count += 1;
169 }
170 }
171 eprintln!("\nDone in {:.3}s\n", start.elapsed().as_secs_f32());
172 eprintln!("I analyzed {} files", files.len());
173 let mut problems = Vec::new();
174 if empty_files_count > 0 {
175 problems.push(format!("{} empty files", empty_files_count));
176 }
177 if incomplete_files_count > 0 {
178 problems.push(format!("{} incomplete files", incomplete_files_count));
179 }
180 if invalid_files_count > 0 {
181 problems.push(format!("{} invalid files", invalid_files_count));
182 }
183 if problems.is_empty() {
184 eprintln!("All {} files were ok", ok_files_count);
185 } else {
186 eprintln!(
187 "{} files were OK but I encountered {}",
188 ok_files_count,
189 problems.join(", ")
190 );
191 }
192 if excluded_enums_count > 0 {
193 eprintln!(
194 "I excluded {} enums whose annotation contained excluding keywords",
195 excluded_enums_count
196 );
197 }
198 eprintln!(
199 "I sorted {} enums in {} files",
200 sorted_enum_count, modified_files_count
201 );
202 Ok(())
203}
pub fn count_blank_lines_at_start(&self) -> usize
Sourcepub fn full_range(&self) -> Option<LineNumberRange>
pub fn full_range(&self) -> Option<LineNumberRange>
If the list isn’t empty, return a range covering it wholly
pub fn check_range(&self, range: LineNumberRange) -> CsResult<()>
Sourcepub fn range_exists(&self, range: LineNumberRange) -> bool
pub fn range_exists(&self, range: LineNumberRange) -> bool
Check whether the range is valid and contains at least one line
pub fn range_has_content(&self, range: LineNumberRange) -> bool
Sourcepub fn has_content(&self) -> bool
pub fn has_content(&self) -> bool
Examples found in repository?
67fn main() {
68 let args = Args::parse();
69 let files = get_all_rust_files(args.path, &args.include).unwrap();
70 eprintln!("Found {} rust files", files.len());
71 let mut no_complete_count = 0;
72 let mut errors = 0;
73 let mut ok_count = 0;
74 for file in files {
75 let loc_list = LocList::read_file(file.to_str().unwrap(), Language::Rust);
76 let loc_list = match loc_list {
77 Ok(loc_list) => loc_list,
78 Err(e) => {
79 eprintln!("{} {:?} : {}", "ERROR".red(), file, e);
80 errors += 1;
81 continue;
82 }
83 };
84 if loc_list.is_complete() {
85 ok_count += 1;
86 continue;
87 }
88 if !loc_list.has_content() {
89 eprintln!("{} {:?}", "EMPTY".yellow(), file);
90 ok_count += 1;
91 continue;
92 }
93 eprintln!("{} {:?}", "NOT COMPLETE".yellow(), file);
94 no_complete_count += 1;
95 }
96 eprintln!("OK files: {}", ok_count);
97 eprintln!("Erroring files: {}", errors);
98 eprintln!("Uncomplete files: {}", no_complete_count);
99}
More examples
86fn main() -> CsResult<()> {
87 let start = std::time::Instant::now();
88 let args = Args::parse();
89 let files = get_all_rust_files(args.path, &args.include, &args.exclude)?;
90 eprintln!("Found {} rust files", files.len());
91 let mut sorted_enum_count = 0;
92 let mut ok_files_count = 0;
93 let mut invalid_files_count = 0;
94 let mut incomplete_files_count = 0;
95 let mut excluded_enums_count = 0;
96 let mut modified_files_count = 0;
97 let mut empty_files_count = 0;
98 for file in &files {
99 let loc_list = LocList::read_file(file, Language::Rust);
100 let mut loc_list = match loc_list {
101 Ok(loc_list) => loc_list,
102 Err(e) => {
103 eprintln!("{} in {}: {:?}", "ERROR".red(), file.display(), e);
104 invalid_files_count += 1;
105 continue;
106 }
107 };
108 if !loc_list.has_content() {
109 empty_files_count += 1;
110 continue;
111 }
112 if !loc_list.is_complete() {
113 eprintln!(
114 "skipping {} ({})",
115 file.display(),
116 "not consistent enough".yellow()
117 );
118 incomplete_files_count += 1;
119 continue;
120 }
121 let mut modified = false;
122 let mut line_idx = 0;
123 ok_files_count += 1;
124 while line_idx + 2 < loc_list.len() {
125 let loc = &loc_list.locs[line_idx];
126 if !loc.starts_normal {
127 line_idx += 1;
128 continue;
129 }
130 let content = &loc.content;
131 let Some((_, name)) =
132 regex_captures!(r"^[\s\w()]*\benum\s+([^({]+)\s+\{\s**$", content)
133 else {
134 line_idx += 1;
135 continue;
136 };
137
138 // We look whether the annotations before the enum contain any one
139 // of the excluding keywords
140 let whole_enum_range = loc_list
141 .block_range_of_line_number(LineNumber::from_index(line_idx))
142 .unwrap();
143 let whole_enum_range = loc_list.trimmed_range(whole_enum_range);
144 let excluding_keyword = EXCLUDING_KEYWORDS.iter().find(|&keyword| {
145 loc_list.locs[whole_enum_range.start.to_index()..line_idx]
146 .iter()
147 .any(|loc| loc.sort_key.contains(keyword))
148 });
149 if let Some(excluding_keyword) = excluding_keyword {
150 eprintln!("skipping enum {} ({})", name, excluding_keyword.yellow());
151 excluded_enums_count += 1;
152 line_idx = whole_enum_range.end.to_index() + 1;
153 continue;
154 }
155 loc_list.print_range_debug(
156 &format!(" sorting enum {} ", name.blue()),
157 whole_enum_range,
158 );
159 let range = loc_list.range_around_line_index(line_idx + 1).unwrap();
160 loc_list.sort_range(range).unwrap();
161 line_idx = range.end.to_index() + 2;
162 sorted_enum_count += 1;
163 modified = true;
164 }
165 if modified {
166 loc_list.write_file(file)?;
167 eprintln!("wrote {}", file.display());
168 modified_files_count += 1;
169 }
170 }
171 eprintln!("\nDone in {:.3}s\n", start.elapsed().as_secs_f32());
172 eprintln!("I analyzed {} files", files.len());
173 let mut problems = Vec::new();
174 if empty_files_count > 0 {
175 problems.push(format!("{} empty files", empty_files_count));
176 }
177 if incomplete_files_count > 0 {
178 problems.push(format!("{} incomplete files", incomplete_files_count));
179 }
180 if invalid_files_count > 0 {
181 problems.push(format!("{} invalid files", invalid_files_count));
182 }
183 if problems.is_empty() {
184 eprintln!("All {} files were ok", ok_files_count);
185 } else {
186 eprintln!(
187 "{} files were OK but I encountered {}",
188 ok_files_count,
189 problems.join(", ")
190 );
191 }
192 if excluded_enums_count > 0 {
193 eprintln!(
194 "I excluded {} enums whose annotation contained excluding keywords",
195 excluded_enums_count
196 );
197 }
198 eprintln!(
199 "I sorted {} enums in {} files",
200 sorted_enum_count, modified_files_count
201 );
202 Ok(())
203}
pub fn last_significant_char(&self) -> Option<char>
pub fn last_line_with_content(&self) -> Option<&Loc>
pub fn last_line_in_range_with_content( &self, range: LineNumberRange, ) -> Option<&Loc>
Sourcepub fn is_range_complete(&self, range: LineNumberRange) -> bool
pub fn is_range_complete(&self, range: LineNumberRange) -> bool
Warning: doesn’t check whether a bigger range wouldn’t be complete too, so a range being complete doesn’t mean it is a block.
Sourcepub fn is_complete(&self) -> bool
pub fn is_complete(&self) -> bool
Examples found in repository?
67fn main() {
68 let args = Args::parse();
69 let files = get_all_rust_files(args.path, &args.include).unwrap();
70 eprintln!("Found {} rust files", files.len());
71 let mut no_complete_count = 0;
72 let mut errors = 0;
73 let mut ok_count = 0;
74 for file in files {
75 let loc_list = LocList::read_file(file.to_str().unwrap(), Language::Rust);
76 let loc_list = match loc_list {
77 Ok(loc_list) => loc_list,
78 Err(e) => {
79 eprintln!("{} {:?} : {}", "ERROR".red(), file, e);
80 errors += 1;
81 continue;
82 }
83 };
84 if loc_list.is_complete() {
85 ok_count += 1;
86 continue;
87 }
88 if !loc_list.has_content() {
89 eprintln!("{} {:?}", "EMPTY".yellow(), file);
90 ok_count += 1;
91 continue;
92 }
93 eprintln!("{} {:?}", "NOT COMPLETE".yellow(), file);
94 no_complete_count += 1;
95 }
96 eprintln!("OK files: {}", ok_count);
97 eprintln!("Erroring files: {}", errors);
98 eprintln!("Uncomplete files: {}", no_complete_count);
99}
More examples
86fn main() -> CsResult<()> {
87 let start = std::time::Instant::now();
88 let args = Args::parse();
89 let files = get_all_rust_files(args.path, &args.include, &args.exclude)?;
90 eprintln!("Found {} rust files", files.len());
91 let mut sorted_enum_count = 0;
92 let mut ok_files_count = 0;
93 let mut invalid_files_count = 0;
94 let mut incomplete_files_count = 0;
95 let mut excluded_enums_count = 0;
96 let mut modified_files_count = 0;
97 let mut empty_files_count = 0;
98 for file in &files {
99 let loc_list = LocList::read_file(file, Language::Rust);
100 let mut loc_list = match loc_list {
101 Ok(loc_list) => loc_list,
102 Err(e) => {
103 eprintln!("{} in {}: {:?}", "ERROR".red(), file.display(), e);
104 invalid_files_count += 1;
105 continue;
106 }
107 };
108 if !loc_list.has_content() {
109 empty_files_count += 1;
110 continue;
111 }
112 if !loc_list.is_complete() {
113 eprintln!(
114 "skipping {} ({})",
115 file.display(),
116 "not consistent enough".yellow()
117 );
118 incomplete_files_count += 1;
119 continue;
120 }
121 let mut modified = false;
122 let mut line_idx = 0;
123 ok_files_count += 1;
124 while line_idx + 2 < loc_list.len() {
125 let loc = &loc_list.locs[line_idx];
126 if !loc.starts_normal {
127 line_idx += 1;
128 continue;
129 }
130 let content = &loc.content;
131 let Some((_, name)) =
132 regex_captures!(r"^[\s\w()]*\benum\s+([^({]+)\s+\{\s**$", content)
133 else {
134 line_idx += 1;
135 continue;
136 };
137
138 // We look whether the annotations before the enum contain any one
139 // of the excluding keywords
140 let whole_enum_range = loc_list
141 .block_range_of_line_number(LineNumber::from_index(line_idx))
142 .unwrap();
143 let whole_enum_range = loc_list.trimmed_range(whole_enum_range);
144 let excluding_keyword = EXCLUDING_KEYWORDS.iter().find(|&keyword| {
145 loc_list.locs[whole_enum_range.start.to_index()..line_idx]
146 .iter()
147 .any(|loc| loc.sort_key.contains(keyword))
148 });
149 if let Some(excluding_keyword) = excluding_keyword {
150 eprintln!("skipping enum {} ({})", name, excluding_keyword.yellow());
151 excluded_enums_count += 1;
152 line_idx = whole_enum_range.end.to_index() + 1;
153 continue;
154 }
155 loc_list.print_range_debug(
156 &format!(" sorting enum {} ", name.blue()),
157 whole_enum_range,
158 );
159 let range = loc_list.range_around_line_index(line_idx + 1).unwrap();
160 loc_list.sort_range(range).unwrap();
161 line_idx = range.end.to_index() + 2;
162 sorted_enum_count += 1;
163 modified = true;
164 }
165 if modified {
166 loc_list.write_file(file)?;
167 eprintln!("wrote {}", file.display());
168 modified_files_count += 1;
169 }
170 }
171 eprintln!("\nDone in {:.3}s\n", start.elapsed().as_secs_f32());
172 eprintln!("I analyzed {} files", files.len());
173 let mut problems = Vec::new();
174 if empty_files_count > 0 {
175 problems.push(format!("{} empty files", empty_files_count));
176 }
177 if incomplete_files_count > 0 {
178 problems.push(format!("{} incomplete files", incomplete_files_count));
179 }
180 if invalid_files_count > 0 {
181 problems.push(format!("{} invalid files", invalid_files_count));
182 }
183 if problems.is_empty() {
184 eprintln!("All {} files were ok", ok_files_count);
185 } else {
186 eprintln!(
187 "{} files were OK but I encountered {}",
188 ok_files_count,
189 problems.join(", ")
190 );
191 }
192 if excluded_enums_count > 0 {
193 eprintln!(
194 "I excluded {} enums whose annotation contained excluding keywords",
195 excluded_enums_count
196 );
197 }
198 eprintln!(
199 "I sorted {} enums in {} files",
200 sorted_enum_count, modified_files_count
201 );
202 Ok(())
203}
Sourcepub fn block_ranges_in_range(
&self,
range: LineNumberRange,
) -> Vec<LineNumberRange>
pub fn block_ranges_in_range( &self, range: LineNumberRange, ) -> Vec<LineNumberRange>
Assuming the provided range you pass is valid enough, give the ranges of the blocks in it.
Sourcepub fn block_range_of_line_number(
&self,
line_number: LineNumber,
) -> CsResult<LineNumberRange>
pub fn block_range_of_line_number( &self, line_number: LineNumber, ) -> CsResult<LineNumberRange>
Find the block the line is part of
For example, if you give the opening line of a struct
(i.e a like like pub struct Foo {
), you’ll get
the whole struct with its doc comments, annotations, inner
fields, til the closing brace. You also get the empty lines
before which should stick with the block.
If you give the line of a field, you’ll get the field definition with its own comment and annotation.
Examples found in repository?
86fn main() -> CsResult<()> {
87 let start = std::time::Instant::now();
88 let args = Args::parse();
89 let files = get_all_rust_files(args.path, &args.include, &args.exclude)?;
90 eprintln!("Found {} rust files", files.len());
91 let mut sorted_enum_count = 0;
92 let mut ok_files_count = 0;
93 let mut invalid_files_count = 0;
94 let mut incomplete_files_count = 0;
95 let mut excluded_enums_count = 0;
96 let mut modified_files_count = 0;
97 let mut empty_files_count = 0;
98 for file in &files {
99 let loc_list = LocList::read_file(file, Language::Rust);
100 let mut loc_list = match loc_list {
101 Ok(loc_list) => loc_list,
102 Err(e) => {
103 eprintln!("{} in {}: {:?}", "ERROR".red(), file.display(), e);
104 invalid_files_count += 1;
105 continue;
106 }
107 };
108 if !loc_list.has_content() {
109 empty_files_count += 1;
110 continue;
111 }
112 if !loc_list.is_complete() {
113 eprintln!(
114 "skipping {} ({})",
115 file.display(),
116 "not consistent enough".yellow()
117 );
118 incomplete_files_count += 1;
119 continue;
120 }
121 let mut modified = false;
122 let mut line_idx = 0;
123 ok_files_count += 1;
124 while line_idx + 2 < loc_list.len() {
125 let loc = &loc_list.locs[line_idx];
126 if !loc.starts_normal {
127 line_idx += 1;
128 continue;
129 }
130 let content = &loc.content;
131 let Some((_, name)) =
132 regex_captures!(r"^[\s\w()]*\benum\s+([^({]+)\s+\{\s**$", content)
133 else {
134 line_idx += 1;
135 continue;
136 };
137
138 // We look whether the annotations before the enum contain any one
139 // of the excluding keywords
140 let whole_enum_range = loc_list
141 .block_range_of_line_number(LineNumber::from_index(line_idx))
142 .unwrap();
143 let whole_enum_range = loc_list.trimmed_range(whole_enum_range);
144 let excluding_keyword = EXCLUDING_KEYWORDS.iter().find(|&keyword| {
145 loc_list.locs[whole_enum_range.start.to_index()..line_idx]
146 .iter()
147 .any(|loc| loc.sort_key.contains(keyword))
148 });
149 if let Some(excluding_keyword) = excluding_keyword {
150 eprintln!("skipping enum {} ({})", name, excluding_keyword.yellow());
151 excluded_enums_count += 1;
152 line_idx = whole_enum_range.end.to_index() + 1;
153 continue;
154 }
155 loc_list.print_range_debug(
156 &format!(" sorting enum {} ", name.blue()),
157 whole_enum_range,
158 );
159 let range = loc_list.range_around_line_index(line_idx + 1).unwrap();
160 loc_list.sort_range(range).unwrap();
161 line_idx = range.end.to_index() + 2;
162 sorted_enum_count += 1;
163 modified = true;
164 }
165 if modified {
166 loc_list.write_file(file)?;
167 eprintln!("wrote {}", file.display());
168 modified_files_count += 1;
169 }
170 }
171 eprintln!("\nDone in {:.3}s\n", start.elapsed().as_secs_f32());
172 eprintln!("I analyzed {} files", files.len());
173 let mut problems = Vec::new();
174 if empty_files_count > 0 {
175 problems.push(format!("{} empty files", empty_files_count));
176 }
177 if incomplete_files_count > 0 {
178 problems.push(format!("{} incomplete files", incomplete_files_count));
179 }
180 if invalid_files_count > 0 {
181 problems.push(format!("{} invalid files", invalid_files_count));
182 }
183 if problems.is_empty() {
184 eprintln!("All {} files were ok", ok_files_count);
185 } else {
186 eprintln!(
187 "{} files were OK but I encountered {}",
188 ok_files_count,
189 problems.join(", ")
190 );
191 }
192 if excluded_enums_count > 0 {
193 eprintln!(
194 "I excluded {} enums whose annotation contained excluding keywords",
195 excluded_enums_count
196 );
197 }
198 eprintln!(
199 "I sorted {} enums in {} files",
200 sorted_enum_count, modified_files_count
201 );
202 Ok(())
203}
Sourcepub fn into_blocks(self) -> Vec<LocList>
pub fn into_blocks(self) -> Vec<LocList>
Examples found in repository?
20fn main() -> CsResult<()> {
21 let args = Args::parse();
22 let loc_list = LocList::read_file(&args.path, Language::Rust)?;
23 let Some(sort_around) = args.sort_around else {
24 loc_list.print_debug(" WHOLE FILE ");
25 return Ok(());
26 };
27 let focused = loc_list.focus_around_line_number(sort_around)?;
28 focused.print_debug();
29 let blocks = focused.clone().focus.into_blocks();
30 for (i, block) in blocks.iter().enumerate() {
31 block.print_debug(&format!(" BLOCK {i} "));
32 }
33 let loc_list = focused.sort();
34 println!("------------ RESULT ------------");
35 print!("{}", loc_list);
36 Ok(())
37}
pub fn range_around_line_number( &self, line_number: LineNumber, ) -> CsResult<LineNumberRange>
Sourcepub fn range_around_line_index(
&self,
line_idx: LineIndex,
) -> CsResult<LineNumberRange>
pub fn range_around_line_index( &self, line_idx: LineIndex, ) -> CsResult<LineNumberRange>
Examples found in repository?
86fn main() -> CsResult<()> {
87 let start = std::time::Instant::now();
88 let args = Args::parse();
89 let files = get_all_rust_files(args.path, &args.include, &args.exclude)?;
90 eprintln!("Found {} rust files", files.len());
91 let mut sorted_enum_count = 0;
92 let mut ok_files_count = 0;
93 let mut invalid_files_count = 0;
94 let mut incomplete_files_count = 0;
95 let mut excluded_enums_count = 0;
96 let mut modified_files_count = 0;
97 let mut empty_files_count = 0;
98 for file in &files {
99 let loc_list = LocList::read_file(file, Language::Rust);
100 let mut loc_list = match loc_list {
101 Ok(loc_list) => loc_list,
102 Err(e) => {
103 eprintln!("{} in {}: {:?}", "ERROR".red(), file.display(), e);
104 invalid_files_count += 1;
105 continue;
106 }
107 };
108 if !loc_list.has_content() {
109 empty_files_count += 1;
110 continue;
111 }
112 if !loc_list.is_complete() {
113 eprintln!(
114 "skipping {} ({})",
115 file.display(),
116 "not consistent enough".yellow()
117 );
118 incomplete_files_count += 1;
119 continue;
120 }
121 let mut modified = false;
122 let mut line_idx = 0;
123 ok_files_count += 1;
124 while line_idx + 2 < loc_list.len() {
125 let loc = &loc_list.locs[line_idx];
126 if !loc.starts_normal {
127 line_idx += 1;
128 continue;
129 }
130 let content = &loc.content;
131 let Some((_, name)) =
132 regex_captures!(r"^[\s\w()]*\benum\s+([^({]+)\s+\{\s**$", content)
133 else {
134 line_idx += 1;
135 continue;
136 };
137
138 // We look whether the annotations before the enum contain any one
139 // of the excluding keywords
140 let whole_enum_range = loc_list
141 .block_range_of_line_number(LineNumber::from_index(line_idx))
142 .unwrap();
143 let whole_enum_range = loc_list.trimmed_range(whole_enum_range);
144 let excluding_keyword = EXCLUDING_KEYWORDS.iter().find(|&keyword| {
145 loc_list.locs[whole_enum_range.start.to_index()..line_idx]
146 .iter()
147 .any(|loc| loc.sort_key.contains(keyword))
148 });
149 if let Some(excluding_keyword) = excluding_keyword {
150 eprintln!("skipping enum {} ({})", name, excluding_keyword.yellow());
151 excluded_enums_count += 1;
152 line_idx = whole_enum_range.end.to_index() + 1;
153 continue;
154 }
155 loc_list.print_range_debug(
156 &format!(" sorting enum {} ", name.blue()),
157 whole_enum_range,
158 );
159 let range = loc_list.range_around_line_index(line_idx + 1).unwrap();
160 loc_list.sort_range(range).unwrap();
161 line_idx = range.end.to_index() + 2;
162 sorted_enum_count += 1;
163 modified = true;
164 }
165 if modified {
166 loc_list.write_file(file)?;
167 eprintln!("wrote {}", file.display());
168 modified_files_count += 1;
169 }
170 }
171 eprintln!("\nDone in {:.3}s\n", start.elapsed().as_secs_f32());
172 eprintln!("I analyzed {} files", files.len());
173 let mut problems = Vec::new();
174 if empty_files_count > 0 {
175 problems.push(format!("{} empty files", empty_files_count));
176 }
177 if incomplete_files_count > 0 {
178 problems.push(format!("{} incomplete files", incomplete_files_count));
179 }
180 if invalid_files_count > 0 {
181 problems.push(format!("{} invalid files", invalid_files_count));
182 }
183 if problems.is_empty() {
184 eprintln!("All {} files were ok", ok_files_count);
185 } else {
186 eprintln!(
187 "{} files were OK but I encountered {}",
188 ok_files_count,
189 problems.join(", ")
190 );
191 }
192 if excluded_enums_count > 0 {
193 eprintln!(
194 "I excluded {} enums whose annotation contained excluding keywords",
195 excluded_enums_count
196 );
197 }
198 eprintln!(
199 "I sorted {} enums in {} files",
200 sorted_enum_count, modified_files_count
201 );
202 Ok(())
203}