pub struct LineNumber {
pub number: NonZeroUsize,
}Expand description
A 1-based line number, as used in most text editors
This is used for exchanging with the user, and for most
APIs. Disambiguation is done by using either LineNumber
or LineIndex.
Fields§
§number: NonZeroUsizeImplementations§
Source§impl LineNumber
impl LineNumber
pub fn new(number: usize) -> Option<Self>
Sourcepub fn to_index(&self) -> LineIndex
pub fn to_index(&self) -> LineIndex
Examples found in repository?
examples/sort-all-enums/main.rs (line 145)
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 from_index(index: LineIndex) -> Self
pub fn from_index(index: LineIndex) -> Self
Examples found in repository?
examples/sort-all-enums/main.rs (line 141)
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}Trait Implementations§
Source§impl Clone for LineNumber
impl Clone for LineNumber
Source§fn clone(&self) -> LineNumber
fn clone(&self) -> LineNumber
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreSource§impl Debug for LineNumber
impl Debug for LineNumber
Source§impl Display for LineNumber
impl Display for LineNumber
Source§impl FromStr for LineNumber
impl FromStr for LineNumber
Source§impl Ord for LineNumber
impl Ord for LineNumber
Source§fn cmp(&self, other: &LineNumber) -> Ordering
fn cmp(&self, other: &LineNumber) -> Ordering
1.21.0 · Source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
Compares and returns the maximum of two values. Read more
Source§impl PartialEq for LineNumber
impl PartialEq for LineNumber
Source§impl PartialOrd for LineNumber
impl PartialOrd for LineNumber
impl Copy for LineNumber
impl Eq for LineNumber
impl StructuralPartialEq for LineNumber
Auto Trait Implementations§
impl Freeze for LineNumber
impl RefUnwindSafe for LineNumber
impl Send for LineNumber
impl Sync for LineNumber
impl Unpin for LineNumber
impl UnwindSafe for LineNumber
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more