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
38 | LangId::Tsx
39 | LangId::JavaScript
40 | LangId::Vue
41 | LangId::Json
42 | LangId::Scala => IndentStyle::Spaces(2),
43 LangId::Rust => IndentStyle::Spaces(4),
44 LangId::Go => IndentStyle::Tabs,
45 LangId::C | LangId::Cpp | LangId::Zig | LangId::CSharp | LangId::Bash => {
46 IndentStyle::Spaces(4)
47 }
48 LangId::Solidity => IndentStyle::Spaces(4),
49 LangId::Html => IndentStyle::Spaces(2),
50 LangId::Markdown => IndentStyle::Spaces(4),
51 }
52 }
53}
54
55pub fn detect_indent(source: &str, lang: LangId) -> IndentStyle {
64 let mut tab_count: u32 = 0;
65 let mut space_count: u32 = 0;
66 let mut indent_widths: [u32; 9] = [0; 9]; for line in source.lines() {
69 if line.is_empty() {
70 continue;
71 }
72 let first = line.as_bytes()[0];
73 if first == b'\t' {
74 tab_count += 1;
75 } else if first == b' ' {
76 space_count += 1;
77 let leading = line.len() - line.trim_start_matches(' ').len();
79 if leading > 0 && leading <= 8 {
80 indent_widths[leading] += 1;
81 }
82 }
83 }
84
85 let total = tab_count + space_count;
86 if total == 0 {
87 return IndentStyle::default_for(lang);
88 }
89
90 if tab_count > total / 2 {
92 return IndentStyle::Tabs;
93 }
94
95 if space_count > total / 2 {
97 let width = determine_space_width(&indent_widths);
101 return IndentStyle::Spaces(width);
102 }
103
104 IndentStyle::default_for(lang)
106}
107
108fn determine_space_width(widths: &[u32; 9]) -> u8 {
114 let smallest = (1..=8usize).find(|&i| widths[i] > 0);
116 let smallest = match smallest {
117 Some(s) => s,
118 None => return 4,
119 };
120
121 let all_multiples = (1..=8).all(|i| widths[i] == 0 || i % smallest == 0);
123
124 if all_multiples && smallest >= 2 {
125 return smallest as u8;
126 }
127
128 for &candidate in &[4u8, 2, 8] {
130 let c = candidate as usize;
131 let mut matching: u32 = 0;
132 let mut non_matching: u32 = 0;
133 for i in 1..=8 {
134 if widths[i] > 0 {
135 if i % c == 0 {
136 matching += widths[i];
137 } else {
138 non_matching += widths[i];
139 }
140 }
141 }
142 if matching > 0 && non_matching == 0 {
143 return candidate;
144 }
145 }
146
147 smallest as u8
148}
149
150#[cfg(test)]
151mod tests {
152 use super::*;
153
154 #[test]
155 fn detect_indent_tabs() {
156 let source = "fn main() {\n\tlet x = 1;\n\tlet y = 2;\n}\n";
157 assert_eq!(detect_indent(source, LangId::Rust), IndentStyle::Tabs);
158 }
159
160 #[test]
161 fn detect_indent_two_spaces() {
162 let source = "class Foo {\n bar() {}\n baz() {}\n}\n";
163 assert_eq!(
164 detect_indent(source, LangId::TypeScript),
165 IndentStyle::Spaces(2)
166 );
167 }
168
169 #[test]
170 fn detect_indent_four_spaces() {
171 let source =
172 "class Foo:\n def bar(self):\n pass\n def baz(self):\n pass\n";
173 assert_eq!(
174 detect_indent(source, LangId::Python),
175 IndentStyle::Spaces(4)
176 );
177 }
178
179 #[test]
180 fn detect_indent_empty_source_uses_default() {
181 assert_eq!(detect_indent("", LangId::Python), IndentStyle::Spaces(4));
182 assert_eq!(
183 detect_indent("", LangId::TypeScript),
184 IndentStyle::Spaces(2)
185 );
186 assert_eq!(detect_indent("", LangId::Go), IndentStyle::Tabs);
187 }
188
189 #[test]
190 fn detect_indent_no_indented_lines_uses_default() {
191 let source = "x = 1\ny = 2\n";
192 assert_eq!(
193 detect_indent(source, LangId::Python),
194 IndentStyle::Spaces(4)
195 );
196 }
197
198 #[test]
199 fn indent_style_as_str() {
200 assert_eq!(IndentStyle::Tabs.as_str(), "\t");
201 assert_eq!(IndentStyle::Spaces(2).as_str(), " ");
202 assert_eq!(IndentStyle::Spaces(4).as_str(), " ");
203 }
204
205 #[test]
206 fn detect_indent_four_spaces_with_nested() {
207 let source = "impl Foo {\n fn bar() {\n let x = 1;\n }\n}\n";
209 assert_eq!(detect_indent(source, LangId::Rust), IndentStyle::Spaces(4));
210 }
211}