@@ -19,6 +19,10 @@ pub enum IndentStyle {
1919 Spaces(u8),
2020}
2121
22+// 16 spaces
23+const INDENTS: &str = " ";
24+const MAX_INDENT: u8 = 16;
25+
2226impl IndentStyle {
2327 /// Creates an `IndentStyle` from an indentation string.
2428 ///
@@ -27,10 +31,10 @@ impl IndentStyle {
2731 #[inline]
2832 pub fn from_str(indent: &str) -> Self {
2933 // XXX: do we care about validating the input more than this? Probably not...?
30− debug_assert!(!indent.is_empty() && indent.len() <= 8);
34+ debug_assert!(!indent.is_empty() && indent.len() <= MAX_INDENT as usize);
3135
3236 if indent.starts_with(' ') {
33− IndentStyle::Spaces(indent.len() as u8)
37+ IndentStyle::Spaces(indent.len().clamp(1, MAX_INDENT as usize) as u8)
3438 } else {
3539 IndentStyle::Tabs
3640 }
@@ -40,20 +44,13 @@ impl IndentStyle {
4044 pub fn as_str(&self) -> &'static str {
4145 match *self {
4246 IndentStyle::Tabs => "\t",
43− IndentStyle::Spaces(1) => " ",
44− IndentStyle::Spaces(2) => " ",
45− IndentStyle::Spaces(3) => " ",
46− IndentStyle::Spaces(4) => " ",
47− IndentStyle::Spaces(5) => " ",
48− IndentStyle::Spaces(6) => " ",
49− IndentStyle::Spaces(7) => " ",
50− IndentStyle::Spaces(8) => " ",
51−
52− // Unsupported indentation style. This should never happen,
53− // but just in case fall back to two spaces.
5447 IndentStyle::Spaces(n) => {
55− debug_assert!(n > 0 && n <= 8); // Always triggers. `debug_panic!()` wanted.
56− " "
48+ // Unsupported indentation style. This should never happen,
49+ debug_assert!(n > 0 && n <= MAX_INDENT);
50+
51+ // Either way, clamp to the nearest supported value
52+ let closest_n = n.clamp(1, MAX_INDENT) as usize;
53+ &INDENTS[0..closest_n]
5754 }
5855 }
5956 }
@@ -75,9 +72,9 @@ pub fn auto_detect_indent_style(document_text: &Rope) -> Option<IndentStyle> {
7572 // Build a histogram of the indentation *increases* between
7673 // subsequent lines, ignoring lines that are all whitespace.
7774 //
78− // Index 0 is for tabs, the rest are 1-8 spaces.
79− let histogram: [usize; 9] = {
80− let mut histogram = [0; 9];
75+ // Index 0 is for tabs, the rest are 1-MAX_INDENT spaces.
76+ let histogram: [usize; MAX_INDENT as usize + 1] = {
77+ let mut histogram = [0; MAX_INDENT as usize + 1];
8178 let mut prev_line_is_tabs = false;
8279 let mut prev_line_leading_count = 0usize;
8380
@@ -136,7 +133,7 @@ pub fn auto_detect_indent_style(document_text: &Rope) -> Option<IndentStyle> {
136133 histogram[0] += 1;
137134 } else {
138135 let amount = leading_count - prev_line_leading_count;
139− if amount <= 8 {
136+ if amount <= MAX_INDENT as usize {
140137 histogram[amount] += 1;
141138 }
142139 }
@@ -797,4 +794,20 @@ mod test {
797794 3
798795 );
799796 }
797+
798+ #[test]
799+ fn test_large_indent_level() {
800+ let tab_width = 16;
801+ let indent_width = 16;
802+ let line = Rope::from(" fn new"); // 16 spaces
803+ assert_eq!(
804+ indent_level_for_line(line.slice(..), tab_width, indent_width),
805+ 1
806+ );
807+ let line = Rope::from(" fn new"); // 32 spaces
808+ assert_eq!(
809+ indent_level_for_line(line.slice(..), tab_width, indent_width),
810+ 2
811+ );
812+ }
800813}