From 066f0b03cf6782e8383b831efa45754e37b2fb14 Mon Sep 17 00:00:00 2001 From: Nathan Vegdahl Date: Mon, 1 Jan 2018 15:01:19 -0800 Subject: [PATCH] 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. --- src/editor/mod.rs | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/src/editor/mod.rs b/src/editor/mod.rs index 56a9fc3..28ff989 100644 --- a/src/editor/mod.rs +++ b/src/editor/mod.rs @@ -1,5 +1,7 @@ #![allow(dead_code)] +use std::collections::HashMap; + use buffer::Buffer; use formatter::LineFormatter; use formatter::RoundingBehavior::*; @@ -172,7 +174,7 @@ impl Editor { pub fn auto_detect_indentation_style(&mut self) { let mut tab_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 = HashMap::new(); let mut last_indent = (false, 0usize); // (was_tabs, indent_count) @@ -215,11 +217,7 @@ impl Editor { if !last_indent.0 && last_indent.1 < count { space_blocks += 1; let amount = count - last_indent.1; - if amount < 9 { - space_histogram[amount] += 1; - } else { - space_histogram[8] += 1; - } + *space_histogram.entry(amount).or_insert(0) += 1; } // Store last line info @@ -238,10 +236,10 @@ impl Editor { if space_blocks > (tab_blocks * 2) { let mut width = 0; let mut width_count = 0; - for i in 0usize..9 { - if space_histogram[i] > width_count { - width = i; - width_count = space_histogram[i]; + for (w, count) in space_histogram.iter() { + if *count > width_count { + width = *w; + width_count = *count; } }