Indentation detection can now detect arbitrary space counts.

It was using a fixed-size array before, but now is using a
HashMap.  This is technically slower, but it's such a negligible
part of the runtime to begin with that it doesn't matter.
This commit is contained in:
Nathan Vegdahl 2018-01-01 15:01:19 -08:00
parent 020827806b
commit 066f0b03cf

View File

@ -1,5 +1,7 @@
#![allow(dead_code)] #![allow(dead_code)]
use std::collections::HashMap;
use buffer::Buffer; use buffer::Buffer;
use formatter::LineFormatter; use formatter::LineFormatter;
use formatter::RoundingBehavior::*; use formatter::RoundingBehavior::*;
@ -172,7 +174,7 @@ impl<T: LineFormatter> Editor<T> {
pub fn auto_detect_indentation_style(&mut self) { pub fn auto_detect_indentation_style(&mut self) {
let mut tab_blocks: usize = 0; let mut tab_blocks: usize = 0;
let mut space_blocks: usize = 0; let mut space_blocks: usize = 0;
let mut space_histogram: [usize; 9] = [0, 0, 0, 0, 0, 0, 0, 0, 0]; let mut space_histogram: HashMap<usize, usize> = HashMap::new();
let mut last_indent = (false, 0usize); // (was_tabs, indent_count) let mut last_indent = (false, 0usize); // (was_tabs, indent_count)
@ -215,11 +217,7 @@ impl<T: LineFormatter> Editor<T> {
if !last_indent.0 && last_indent.1 < count { if !last_indent.0 && last_indent.1 < count {
space_blocks += 1; space_blocks += 1;
let amount = count - last_indent.1; let amount = count - last_indent.1;
if amount < 9 { *space_histogram.entry(amount).or_insert(0) += 1;
space_histogram[amount] += 1;
} else {
space_histogram[8] += 1;
}
} }
// Store last line info // Store last line info
@ -238,10 +236,10 @@ impl<T: LineFormatter> Editor<T> {
if space_blocks > (tab_blocks * 2) { if space_blocks > (tab_blocks * 2) {
let mut width = 0; let mut width = 0;
let mut width_count = 0; let mut width_count = 0;
for i in 0usize..9 { for (w, count) in space_histogram.iter() {
if space_histogram[i] > width_count { if *count > width_count {
width = i; width = *w;
width_count = space_histogram[i]; width_count = *count;
} }
} }