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
43 | LangId::Ruby
44 | LangId::Lua
45 | LangId::Scss
46 | LangId::Yaml => IndentStyle::Spaces(2),
47 LangId::Rust => IndentStyle::Spaces(4),
48 LangId::Go => IndentStyle::Tabs,
49 LangId::C | LangId::Cpp | LangId::Zig | LangId::CSharp | LangId::Bash => {
50 IndentStyle::Spaces(4)
51 }
52 LangId::Solidity
53 | LangId::Java
54 | LangId::Kotlin
55 | LangId::Swift
56 | LangId::Php
57 | LangId::Perl => IndentStyle::Spaces(4),
58 LangId::Html => IndentStyle::Spaces(2),
59 LangId::Markdown => IndentStyle::Spaces(4),
60 }
61 }
62}
63
64pub fn detect_indent(source: &str, lang: LangId) -> IndentStyle {
73 let mut tab_count: u32 = 0;
74 let mut space_count: u32 = 0;
75 let mut indent_widths: [u32; 9] = [0; 9]; for line in source.lines() {
78 if line.is_empty() {
79 continue;
80 }
81 let first = line.as_bytes()[0];
82 if first == b'\t' {
83 tab_count += 1;
84 } else if first == b' ' {
85 space_count += 1;
86 let leading = line.len() - line.trim_start_matches(' ').len();
88 if leading > 0 && leading <= 8 {
89 indent_widths[leading] += 1;
90 }
91 }
92 }
93
94 let total = tab_count + space_count;
95 if total == 0 {
96 return IndentStyle::default_for(lang);
97 }
98
99 if tab_count > total / 2 {
101 return IndentStyle::Tabs;
102 }
103
104 if space_count > total / 2 {
106 let width = determine_space_width(&indent_widths);
110 return IndentStyle::Spaces(width);
111 }
112
113 IndentStyle::default_for(lang)
115}
116
117fn determine_space_width(widths: &[u32; 9]) -> u8 {
123 let smallest = (1..=8usize).find(|&i| widths[i] > 0);
125 let smallest = match smallest {
126 Some(s) => s,
127 None => return 4,
128 };
129
130 let all_multiples = (1..=8).all(|i| widths[i] == 0 || i % smallest == 0);
132
133 if all_multiples && smallest >= 2 {
134 return smallest as u8;
135 }
136
137 for &candidate in &[4u8, 2, 8] {
139 let c = candidate as usize;
140 let mut matching: u32 = 0;
141 let mut non_matching: u32 = 0;
142 for i in 1..=8 {
143 if widths[i] > 0 {
144 if i % c == 0 {
145 matching += widths[i];
146 } else {
147 non_matching += widths[i];
148 }
149 }
150 }
151 if matching > 0 && non_matching == 0 {
152 return candidate;
153 }
154 }
155
156 smallest as u8
157}
158
159#[cfg(test)]
160mod tests {
161 use super::*;
162
163 #[test]
164 fn detect_indent_tabs() {
165 let source = "fn main() {\n\tlet x = 1;\n\tlet y = 2;\n}\n";
166 assert_eq!(detect_indent(source, LangId::Rust), IndentStyle::Tabs);
167 }
168
169 #[test]
170 fn detect_indent_two_spaces() {
171 let source = "class Foo {\n bar() {}\n baz() {}\n}\n";
172 assert_eq!(
173 detect_indent(source, LangId::TypeScript),
174 IndentStyle::Spaces(2)
175 );
176 }
177
178 #[test]
179 fn detect_indent_four_spaces() {
180 let source =
181 "class Foo:\n def bar(self):\n pass\n def baz(self):\n pass\n";
182 assert_eq!(
183 detect_indent(source, LangId::Python),
184 IndentStyle::Spaces(4)
185 );
186 }
187
188 #[test]
189 fn detect_indent_empty_source_uses_default() {
190 assert_eq!(detect_indent("", LangId::Python), IndentStyle::Spaces(4));
191 assert_eq!(
192 detect_indent("", LangId::TypeScript),
193 IndentStyle::Spaces(2)
194 );
195 assert_eq!(detect_indent("", LangId::Go), IndentStyle::Tabs);
196 }
197
198 #[test]
199 fn detect_indent_no_indented_lines_uses_default() {
200 let source = "x = 1\ny = 2\n";
201 assert_eq!(
202 detect_indent(source, LangId::Python),
203 IndentStyle::Spaces(4)
204 );
205 }
206
207 #[test]
208 fn indent_style_as_str() {
209 assert_eq!(IndentStyle::Tabs.as_str(), "\t");
210 assert_eq!(IndentStyle::Spaces(2).as_str(), " ");
211 assert_eq!(IndentStyle::Spaces(4).as_str(), " ");
212 }
213
214 #[test]
215 fn detect_indent_four_spaces_with_nested() {
216 let source = "impl Foo {\n fn bar() {\n let x = 1;\n }\n}\n";
218 assert_eq!(detect_indent(source, LangId::Rust), IndentStyle::Spaces(4));
219 }
220}