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 | 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
47/// Detect the indentation style of a source file.
48///
49/// Examines indented lines (those starting with whitespace) and determines
50/// whether tabs or spaces dominate. For spaces, determines the most common
51/// indent width by looking at the smallest indent unit.
52///
53/// Returns detected style if >50% of indented lines agree, otherwise falls
54/// back to the language default.
55pub 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]; // index 1..8
59
60    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            // Count leading spaces
70            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    // Tabs win if >50% of indented lines use tabs
83    if tab_count > total / 2 {
84        return IndentStyle::Tabs;
85    }
86
87    // Spaces win if >50% of indented lines use spaces
88    if space_count > total / 2 {
89        // Determine the most likely indent unit width.
90        // The unit is the GCD of observed indent widths, or equivalently,
91        // the smallest width that has significant usage.
92        let width = determine_space_width(&indent_widths);
93        return IndentStyle::Spaces(width);
94    }
95
96    // Mixed / no clear winner — fall back
97    IndentStyle::default_for(lang)
98}
99
100/// Determine the most likely space indent width from observed leading-space counts.
101///
102/// Strategy: find the smallest observed indent width that forms a consistent
103/// pattern (all other widths are multiples of it). Prefer the smallest actual
104/// indent seen, not just the GCD.
105fn determine_space_width(widths: &[u32; 9]) -> u8 {
106    // Find the smallest observed indent width
107    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    // Check if all observed widths are multiples of this smallest
114    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    // If smallest is 1 or doesn't divide evenly, try common widths
121    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        // Lines indented at 4 and 8 should detect 4-space indent
200        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}