1use crate::parser::LangId;
8
9#[derive(Debug, Clone, Copy, PartialEq, Eq)]
11pub enum IndentStyle {
12 Tabs,
13 Spaces(u8),
14}
15
16impl IndentStyle {
17 pub fn as_str(&self) -> &'static str {
19 match self {
20 IndentStyle::Tabs => "\t",
21 IndentStyle::Spaces(2) => " ",
22 IndentStyle::Spaces(4) => " ",
23 IndentStyle::Spaces(8) => " ",
24 IndentStyle::Spaces(n) => {
25 let s: String = " ".repeat(*n as usize);
28 Box::leak(s.into_boxed_str())
29 }
30 }
31 }
32
33 pub fn default_for(lang: LangId) -> Self {
35 match lang {
36 LangId::Python => IndentStyle::Spaces(4),
37 LangId::TypeScript | LangId::Tsx | LangId::JavaScript => IndentStyle::Spaces(2),
38 LangId::Rust => IndentStyle::Spaces(4),
39 LangId::Go => IndentStyle::Tabs,
40 LangId::C | LangId::Cpp | LangId::Zig | LangId::CSharp => IndentStyle::Spaces(4),
41 LangId::Html => IndentStyle::Spaces(2),
42 LangId::Markdown => IndentStyle::Spaces(4),
43 }
44 }
45}
46
47pub fn detect_indent(source: &str, lang: LangId) -> IndentStyle {
56 let mut tab_count: u32 = 0;
57 let mut space_count: u32 = 0;
58 let mut indent_widths: [u32; 9] = [0; 9]; for line in source.lines() {
61 if line.is_empty() {
62 continue;
63 }
64 let first = line.as_bytes()[0];
65 if first == b'\t' {
66 tab_count += 1;
67 } else if first == b' ' {
68 space_count += 1;
69 let leading = line.len() - line.trim_start_matches(' ').len();
71 if leading > 0 && leading <= 8 {
72 indent_widths[leading] += 1;
73 }
74 }
75 }
76
77 let total = tab_count + space_count;
78 if total == 0 {
79 return IndentStyle::default_for(lang);
80 }
81
82 if tab_count > total / 2 {
84 return IndentStyle::Tabs;
85 }
86
87 if space_count > total / 2 {
89 let width = determine_space_width(&indent_widths);
93 return IndentStyle::Spaces(width);
94 }
95
96 IndentStyle::default_for(lang)
98}
99
100fn determine_space_width(widths: &[u32; 9]) -> u8 {
106 let smallest = (1..=8usize).find(|&i| widths[i] > 0);
108 let smallest = match smallest {
109 Some(s) => s,
110 None => return 4,
111 };
112
113 let all_multiples = (1..=8).all(|i| widths[i] == 0 || i % smallest == 0);
115
116 if all_multiples && smallest >= 2 {
117 return smallest as u8;
118 }
119
120 for &candidate in &[4u8, 2, 8] {
122 let c = candidate as usize;
123 let mut matching: u32 = 0;
124 let mut non_matching: u32 = 0;
125 for i in 1..=8 {
126 if widths[i] > 0 {
127 if i % c == 0 {
128 matching += widths[i];
129 } else {
130 non_matching += widths[i];
131 }
132 }
133 }
134 if matching > 0 && non_matching == 0 {
135 return candidate;
136 }
137 }
138
139 smallest as u8
140}
141
142#[cfg(test)]
143mod tests {
144 use super::*;
145
146 #[test]
147 fn detect_indent_tabs() {
148 let source = "fn main() {\n\tlet x = 1;\n\tlet y = 2;\n}\n";
149 assert_eq!(detect_indent(source, LangId::Rust), IndentStyle::Tabs);
150 }
151
152 #[test]
153 fn detect_indent_two_spaces() {
154 let source = "class Foo {\n bar() {}\n baz() {}\n}\n";
155 assert_eq!(
156 detect_indent(source, LangId::TypeScript),
157 IndentStyle::Spaces(2)
158 );
159 }
160
161 #[test]
162 fn detect_indent_four_spaces() {
163 let source =
164 "class Foo:\n def bar(self):\n pass\n def baz(self):\n pass\n";
165 assert_eq!(
166 detect_indent(source, LangId::Python),
167 IndentStyle::Spaces(4)
168 );
169 }
170
171 #[test]
172 fn detect_indent_empty_source_uses_default() {
173 assert_eq!(detect_indent("", LangId::Python), IndentStyle::Spaces(4));
174 assert_eq!(
175 detect_indent("", LangId::TypeScript),
176 IndentStyle::Spaces(2)
177 );
178 assert_eq!(detect_indent("", LangId::Go), IndentStyle::Tabs);
179 }
180
181 #[test]
182 fn detect_indent_no_indented_lines_uses_default() {
183 let source = "x = 1\ny = 2\n";
184 assert_eq!(
185 detect_indent(source, LangId::Python),
186 IndentStyle::Spaces(4)
187 );
188 }
189
190 #[test]
191 fn indent_style_as_str() {
192 assert_eq!(IndentStyle::Tabs.as_str(), "\t");
193 assert_eq!(IndentStyle::Spaces(2).as_str(), " ");
194 assert_eq!(IndentStyle::Spaces(4).as_str(), " ");
195 }
196
197 #[test]
198 fn detect_indent_four_spaces_with_nested() {
199 let source = "impl Foo {\n fn bar() {\n let x = 1;\n }\n}\n";
201 assert_eq!(detect_indent(source, LangId::Rust), IndentStyle::Spaces(4));
202 }
203}