Skip to main content

aft/
indent.rs

1//! Shared indentation detection utility (D042).
2//!
3//! Analyzes source file content to determine the indentation style (tabs vs
4//! spaces, width) used. Falls back to language-specific defaults when the
5//! file has insufficient indented lines or mixed signals.
6
7use crate::parser::LangId;
8
9/// Detected indentation style.
10#[derive(Debug, Clone, Copy, PartialEq, Eq)]
11pub enum IndentStyle {
12    Tabs,
13    Spaces(u8),
14}
15
16impl IndentStyle {
17    /// Returns the whitespace string for one level of this indent.
18    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                // For uncommon widths, leak a static string. In practice
26                // this only fires for exotic indent widths (1, 3, 5, 6, 7).
27                let s: String = " ".repeat(*n as usize);
28                Box::leak(s.into_boxed_str())
29            }
30        }
31    }
32
33    /// Language-specific default when detection has low confidence.
34    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::Yaml => IndentStyle::Spaces(2),
46            LangId::Rust => IndentStyle::Spaces(4),
47            LangId::Go => IndentStyle::Tabs,
48            LangId::C | LangId::Cpp | LangId::Zig | LangId::CSharp | LangId::Bash => {
49                IndentStyle::Spaces(4)
50            }
51            LangId::Solidity
52            | LangId::Java
53            | LangId::Kotlin
54            | LangId::Swift
55            | LangId::Php
56            | LangId::Perl => IndentStyle::Spaces(4),
57            LangId::Html => IndentStyle::Spaces(2),
58            LangId::Markdown => IndentStyle::Spaces(4),
59        }
60    }
61}
62
63/// Detect the indentation style of a source file.
64///
65/// Examines indented lines (those starting with whitespace) and determines
66/// whether tabs or spaces dominate. For spaces, determines the most common
67/// indent width by looking at the smallest indent unit.
68///
69/// Returns detected style if >50% of indented lines agree, otherwise falls
70/// back to the language default.
71pub fn detect_indent(source: &str, lang: LangId) -> IndentStyle {
72    let mut tab_count: u32 = 0;
73    let mut space_count: u32 = 0;
74    let mut indent_widths: [u32; 9] = [0; 9]; // index 1..8
75
76    for line in source.lines() {
77        if line.is_empty() {
78            continue;
79        }
80        let first = line.as_bytes()[0];
81        if first == b'\t' {
82            tab_count += 1;
83        } else if first == b' ' {
84            space_count += 1;
85            // Count leading spaces
86            let leading = line.len() - line.trim_start_matches(' ').len();
87            if leading > 0 && leading <= 8 {
88                indent_widths[leading] += 1;
89            }
90        }
91    }
92
93    let total = tab_count + space_count;
94    if total == 0 {
95        return IndentStyle::default_for(lang);
96    }
97
98    // Tabs win if >50% of indented lines use tabs
99    if tab_count > total / 2 {
100        return IndentStyle::Tabs;
101    }
102
103    // Spaces win if >50% of indented lines use spaces
104    if space_count > total / 2 {
105        // Determine the most likely indent unit width.
106        // The unit is the GCD of observed indent widths, or equivalently,
107        // the smallest width that has significant usage.
108        let width = determine_space_width(&indent_widths);
109        return IndentStyle::Spaces(width);
110    }
111
112    // Mixed / no clear winner — fall back
113    IndentStyle::default_for(lang)
114}
115
116/// Determine the most likely space indent width from observed leading-space counts.
117///
118/// Strategy: find the smallest observed indent width that forms a consistent
119/// pattern (all other widths are multiples of it). Prefer the smallest actual
120/// indent seen, not just the GCD.
121fn determine_space_width(widths: &[u32; 9]) -> u8 {
122    // Find the smallest observed indent width
123    let smallest = (1..=8usize).find(|&i| widths[i] > 0);
124    let smallest = match smallest {
125        Some(s) => s,
126        None => return 4,
127    };
128
129    // Check if all observed widths are multiples of this smallest
130    let all_multiples = (1..=8).all(|i| widths[i] == 0 || i % smallest == 0);
131
132    if all_multiples && smallest >= 2 {
133        return smallest as u8;
134    }
135
136    // If smallest is 1 or doesn't divide evenly, try common widths
137    for &candidate in &[4u8, 2, 8] {
138        let c = candidate as usize;
139        let mut matching: u32 = 0;
140        let mut non_matching: u32 = 0;
141        for i in 1..=8 {
142            if widths[i] > 0 {
143                if i % c == 0 {
144                    matching += widths[i];
145                } else {
146                    non_matching += widths[i];
147                }
148            }
149        }
150        if matching > 0 && non_matching == 0 {
151            return candidate;
152        }
153    }
154
155    smallest as u8
156}
157
158#[cfg(test)]
159mod tests {
160    use super::*;
161
162    #[test]
163    fn detect_indent_tabs() {
164        let source = "fn main() {\n\tlet x = 1;\n\tlet y = 2;\n}\n";
165        assert_eq!(detect_indent(source, LangId::Rust), IndentStyle::Tabs);
166    }
167
168    #[test]
169    fn detect_indent_two_spaces() {
170        let source = "class Foo {\n  bar() {}\n  baz() {}\n}\n";
171        assert_eq!(
172            detect_indent(source, LangId::TypeScript),
173            IndentStyle::Spaces(2)
174        );
175    }
176
177    #[test]
178    fn detect_indent_four_spaces() {
179        let source =
180            "class Foo:\n    def bar(self):\n        pass\n    def baz(self):\n        pass\n";
181        assert_eq!(
182            detect_indent(source, LangId::Python),
183            IndentStyle::Spaces(4)
184        );
185    }
186
187    #[test]
188    fn detect_indent_empty_source_uses_default() {
189        assert_eq!(detect_indent("", LangId::Python), IndentStyle::Spaces(4));
190        assert_eq!(
191            detect_indent("", LangId::TypeScript),
192            IndentStyle::Spaces(2)
193        );
194        assert_eq!(detect_indent("", LangId::Go), IndentStyle::Tabs);
195    }
196
197    #[test]
198    fn detect_indent_no_indented_lines_uses_default() {
199        let source = "x = 1\ny = 2\n";
200        assert_eq!(
201            detect_indent(source, LangId::Python),
202            IndentStyle::Spaces(4)
203        );
204    }
205
206    #[test]
207    fn indent_style_as_str() {
208        assert_eq!(IndentStyle::Tabs.as_str(), "\t");
209        assert_eq!(IndentStyle::Spaces(2).as_str(), "  ");
210        assert_eq!(IndentStyle::Spaces(4).as_str(), "    ");
211    }
212
213    #[test]
214    fn detect_indent_four_spaces_with_nested() {
215        // Lines indented at 4 and 8 should detect 4-space indent
216        let source = "impl Foo {\n    fn bar() {\n        let x = 1;\n    }\n}\n";
217        assert_eq!(detect_indent(source, LangId::Rust), IndentStyle::Spaces(4));
218    }
219}