on master help sign in
Cosmetic changes and better comments for the indent auto-detect code.
commitecb39da3e06eeeabe844cafb76feb233052ce4cb authorNathan Vegdahl <cessen@cessen.com> dateSun 13 Jun 2021, 20:11 (5 years ago) parent4faf1d3
1 file changed+22−15
helix-view/src/document.rs+22−15
helix-view/src/document.rs+22−15
@@ -284,8 +284,11 @@ impl Document {
284284 // Index 0 is for tabs, the rest are 1-8 spaces.
285285 let histogram: [usize; 9] = {
286286 let mut histogram = [0; 9];
287− let mut prev_line_indent = (false, 0usize); // (was_tabs, count)
287+ let mut prev_line_is_tabs = false;
288+ let mut prev_line_leading_count = 0usize;
288289
290+ // Loop through the lines, checking for and recording indentation
291+ // increases as we go.
289292 'outer: for line in self.text.lines().take(1000) {
290293 let mut c_iter = line.chars();
291294
@@ -295,21 +298,22 @@ impl Document {
295298 Some(' ') => false,
296299
297300 // Ignore blank lines.
298− Some(c) if char_is_linebreak(c) => continue 'outer,
301+ Some(c) if char_is_linebreak(c) => continue,
299302
300303 _ => {
301− prev_line_indent = (false, 0usize);
302− continue 'outer;
304+ prev_line_is_tabs = false;
305+ prev_line_leading_count = 0;
306+ continue;
303307 }
304308 };
305309
306− // Count the total leading tab/space characters.
307− let mut count = 1;
310+ // Count the line's total leading tab/space characters.
311+ let mut leading_count = 1;
308312 let mut count_is_done = false;
309313 for c in c_iter {
310314 match c {
311− '\t' if is_tabs && !count_is_done => count += 1,
312− ' ' if !is_tabs && !count_is_done => count += 1,
315+ '\t' if is_tabs && !count_is_done => leading_count += 1,
316+ ' ' if !is_tabs && !count_is_done => leading_count += 1,
313317
314318 // We stop counting if we hit whitespace that doesn't
315319 // qualify as indent or doesn't match the leading
@@ -324,27 +328,30 @@ impl Document {
324328 }
325329
326330 // Bound the worst-case execution time for weird text files.
327− if count > 256 {
331+ if leading_count > 256 {
328332 continue 'outer;
329333 }
330334 }
331335
332− // Update stats.
333− if (prev_line_indent.0 == is_tabs || prev_line_indent.1 == 0)
334− && prev_line_indent.1 < count
336+ // If there was an increase in indentation over the previous
337+ // line, update the histogram with that increase.
338+ if (prev_line_is_tabs == is_tabs || prev_line_leading_count == 0)
339+ && prev_line_leading_count < leading_count
335340 {
336341 if is_tabs {
337342 histogram[0] += 1;
338343 } else {
339− let amount = count - prev_line_indent.1;
344+ let amount = leading_count - prev_line_leading_count;
340345 if amount <= 8 {
341346 histogram[amount] += 1;
342347 }
343348 }
344349 }
345350
346− // Store data for use with the next line.
347− prev_line_indent = (is_tabs, count);
351+ // Store this line's leading whitespace info for use with
352+ // the next line.
353+ prev_line_is_tabs = is_tabs;
354+ prev_line_leading_count = leading_count;
348355 }
349356
350357 // Give more weight to tabs, because their presence is a very