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