on master help sign in
Cosmetic changes and better comments for the indent auto-detect code.
ecb39daNathan Vegdahl
helix-view/src/document.rs+22−15
@@ -284,8 +284,11 @@ impl Document {
284 // Index 0 is for tabs, the rest are 1-8 spaces.
284 // Index 0 is for tabs, the rest are 1-8 spaces.
285 let histogram: [usize; 9] = {
285 let histogram: [usize; 9] = {
286 let mut histogram = [0; 9];
286 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;
288
289
290+ // Loop through the lines, checking for and recording indentation
291+ // increases as we go.
289 'outer: for line in self.text.lines().take(1000) {
292 'outer: for line in self.text.lines().take(1000) {
290 let mut c_iter = line.chars();
293 let mut c_iter = line.chars();
291
294
@@ -295,21 +298,22 @@ impl Document {
295 Some(' ') => false,
298 Some(' ') => false,
296
299
297 // Ignore blank lines.
300 // Ignore blank lines.
298− Some(c) if char_is_linebreak(c) => continue 'outer,
301+ Some(c) if char_is_linebreak(c) => continue,
299
302
300 _ => {
303 _ => {
301− prev_line_indent = (false, 0usize);
304+ prev_line_is_tabs = false;
305+ prev_line_leading_count = 0;
302− continue 'outer;
306+ continue;
303 }
307 }
304 };
308 };
305
309
306− // Count the total leading tab/space characters.
310+ // Count the line's total leading tab/space characters.
307− let mut count = 1;
311+ let mut leading_count = 1;
308 let mut count_is_done = false;
312 let mut count_is_done = false;
309 for c in c_iter {
313 for c in c_iter {
310 match c {
314 match c {
311− '\t' if is_tabs && !count_is_done => count += 1,
315+ '\t' if is_tabs && !count_is_done => leading_count += 1,
312− ' ' if !is_tabs && !count_is_done => count += 1,
316+ ' ' if !is_tabs && !count_is_done => leading_count += 1,
313
317
314 // We stop counting if we hit whitespace that doesn't
318 // We stop counting if we hit whitespace that doesn't
315 // qualify as indent or doesn't match the leading
319 // qualify as indent or doesn't match the leading
@@ -324,27 +328,30 @@ impl Document {
324 }
328 }
325
329
326 // Bound the worst-case execution time for weird text files.
330 // Bound the worst-case execution time for weird text files.
327− if count > 256 {
331+ if leading_count > 256 {
328 continue 'outer;
332 continue 'outer;
329 }
333 }
330 }
334 }
331
335
332− // Update stats.
336+ // If there was an increase in indentation over the previous
333− if (prev_line_indent.0 == is_tabs || prev_line_indent.1 == 0)
337+ // line, update the histogram with that increase.
334− && prev_line_indent.1 < count
338+ if (prev_line_is_tabs == is_tabs || prev_line_leading_count == 0)
339+ && prev_line_leading_count < leading_count
335 {
340 {
336 if is_tabs {
341 if is_tabs {
337 histogram[0] += 1;
342 histogram[0] += 1;
338 } else {
343 } else {
339− let amount = count - prev_line_indent.1;
344+ let amount = leading_count - prev_line_leading_count;
340 if amount <= 8 {
345 if amount <= 8 {
341 histogram[amount] += 1;
346 histogram[amount] += 1;
342 }
347 }
343 }
348 }
344 }
349 }
345
350
351+ // Store this line's leading whitespace info for use with
346− // Store data for use with the next line.
352+ // the next line.
347− prev_line_indent = (is_tabs, count);
353+ prev_line_is_tabs = is_tabs;
354+ prev_line_leading_count = leading_count;
348 }
355 }
349
356
350 // Give more weight to tabs, because their presence is a very
357 // Give more weight to tabs, because their presence is a very