howmany 3.0.0

A blazingly fast, intelligent code analysis tool with parallel processing, caching, and beautiful visualizations
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
use super::super::types::{FunctionInfo, StructureInfo, StructureType, Visibility};
use super::LanguageAnalyzer;
use crate::utils::errors::Result;

/// Haskell language complexity analyzer
pub struct HaskellAnalyzer;

impl HaskellAnalyzer {
    pub fn new() -> Self {
        Self
    }

    /// Extract function name from Haskell function definition
    fn extract_function_name(&self, line: &str) -> Option<String> {
        let trimmed = line.trim();

        // Skip comments and empty lines
        if trimmed.starts_with("--") || trimmed.is_empty() {
            return None;
        }

        // Look for function definitions: functionName :: Type -> Type
        if trimmed.contains("::") {
            let parts: Vec<&str> = trimmed.split("::").collect();
            if let Some(first_part) = parts.first() {
                let func_name = first_part.trim();
                if !func_name.is_empty()
                    && func_name
                        .chars()
                        .all(|c| c.is_alphanumeric() || c == '_' || c == '\'')
                    && func_name.chars().next().unwrap_or('A').is_lowercase()
                {
                    return Some(func_name.to_string());
                }
            }
        }

        // Look for function implementations: functionName args = body
        if trimmed.contains('=') && !trimmed.contains("::") {
            let parts: Vec<&str> = trimmed.split('=').collect();
            if let Some(first_part) = parts.first() {
                let func_part = first_part.trim();
                let words: Vec<&str> = func_part.split_whitespace().collect();
                if let Some(first_word) = words.first() {
                    if !first_word.is_empty()
                        && first_word
                            .chars()
                            .all(|c| c.is_alphanumeric() || c == '_' || c == '\'')
                        && first_word.chars().next().unwrap_or('A').is_lowercase()
                    {
                        return Some(first_word.to_string());
                    }
                }
            }
        }

        // Look for lambda expressions: \args -> body
        if trimmed.contains("\\") && trimmed.contains("->") {
            return Some("lambda".to_string());
        }

        None
    }

    /// Extract data type/module name from Haskell declaration
    fn extract_structure_name(&self, line: &str) -> Option<String> {
        let trimmed = line.trim();

        // Look for module declarations: module ModuleName where
        if let Some(start) = trimmed.find("module ") {
            let after_module = &trimmed[start + 7..];
            let parts: Vec<&str> = after_module.split_whitespace().collect();
            if let Some(first_part) = parts.first() {
                let module_name = first_part.trim();
                if !module_name.is_empty()
                    && module_name
                        .chars()
                        .all(|c| c.is_alphanumeric() || c == '_' || c == '.')
                    && module_name.chars().next().unwrap_or('a').is_uppercase()
                {
                    return Some(module_name.to_string());
                }
            }
        }

        // Look for data type declarations: data TypeName = ...
        if let Some(start) = trimmed.find("data ") {
            let after_data = &trimmed[start + 5..];
            let parts: Vec<&str> = after_data.split_whitespace().collect();
            if let Some(first_part) = parts.first() {
                let type_name = first_part.trim();
                if !type_name.is_empty()
                    && type_name
                        .chars()
                        .all(|c| c.is_alphanumeric() || c == '_' || c == '\'')
                    && type_name.chars().next().unwrap_or('a').is_uppercase()
                {
                    return Some(type_name.to_string());
                }
            }
        }

        // Look for newtype declarations: newtype TypeName = ...
        if let Some(start) = trimmed.find("newtype ") {
            let after_newtype = &trimmed[start + 8..];
            let parts: Vec<&str> = after_newtype.split_whitespace().collect();
            if let Some(first_part) = parts.first() {
                let type_name = first_part.trim();
                if !type_name.is_empty()
                    && type_name
                        .chars()
                        .all(|c| c.is_alphanumeric() || c == '_' || c == '\'')
                    && type_name.chars().next().unwrap_or('a').is_uppercase()
                {
                    return Some(type_name.to_string());
                }
            }
        }

        // Look for type aliases: type TypeName = ...
        if let Some(start) = trimmed.find("type ") {
            let after_type = &trimmed[start + 5..];
            let parts: Vec<&str> = after_type.split_whitespace().collect();
            if let Some(first_part) = parts.first() {
                let type_name = first_part.trim();
                if !type_name.is_empty()
                    && type_name
                        .chars()
                        .all(|c| c.is_alphanumeric() || c == '_' || c == '\'')
                    && type_name.chars().next().unwrap_or('a').is_uppercase()
                {
                    return Some(type_name.to_string());
                }
            }
        }

        // Look for class declarations: class ClassName where
        if let Some(start) = trimmed.find("class ") {
            let after_class = &trimmed[start + 6..];
            let parts: Vec<&str> = after_class.split_whitespace().collect();
            if let Some(first_part) = parts.first() {
                let class_name = first_part.trim();
                if !class_name.is_empty()
                    && class_name
                        .chars()
                        .all(|c| c.is_alphanumeric() || c == '_' || c == '\'')
                    && class_name.chars().next().unwrap_or('a').is_uppercase()
                {
                    return Some(class_name.to_string());
                }
            }
        }

        None
    }

    /// Count complexity keywords in Haskell code
    fn count_complexity_keywords(&self, line: &str) -> usize {
        let keywords = [
            "if",
            "then",
            "else",
            "case",
            "of",
            "let",
            "in",
            "where",
            "do",
            "guard",
            "|",
            "&&",
            "||",
            "not",
            "otherwise",
            "maybe",
            "either",
            "catch",
            "try",
            "throw",
            "error",
        ];
        keywords
            .iter()
            .map(|&keyword| {
                // Be careful with partial matches, especially for short keywords like "|"
                if keyword == "|" {
                    line.matches(" | ").count() + line.matches("| ").count()
                } else {
                    line.matches(keyword).count()
                }
            })
            .sum()
    }

    /// Calculate cyclomatic complexity for a function
    fn calculate_cyclomatic_complexity(
        &self,
        lines: &[String],
        start_line: usize,
        end_line: usize,
    ) -> usize {
        let mut complexity = 1; // Base complexity

        for line in lines.iter().take(end_line + 1).skip(start_line) {
            complexity += self.count_complexity_keywords(line);
        }

        complexity
    }

    /// Find the end of a function definition
    fn find_function_end(&self, lines: &[String], start_line: usize) -> usize {
        let mut in_function = false;
        let mut base_indentation = None;

        for (i, line) in lines.iter().enumerate().skip(start_line) {
            let trimmed = line.trim();

            if !trimmed.is_empty() {
                let current_indentation = line.len() - line.trim_start().len();

                if !in_function {
                    // First non-empty line defines the function
                    in_function = true;
                    base_indentation = Some(current_indentation);
                } else {
                    // Check if we've moved to a new top-level definition
                    if current_indentation <= base_indentation.unwrap_or(0)
                        && (trimmed.contains("::") || trimmed.contains("="))
                        && !trimmed.starts_with("--")
                    {
                        return i.saturating_sub(1);
                    }
                }
            }
        }

        lines.len().saturating_sub(1)
    }

    /// Determine visibility of a function
    fn determine_visibility(&self, _line: &str) -> Visibility {
        // Haskell functions are public by default unless not exported
        // We can't easily determine export status from a single line
        Visibility::Public
    }

    /// Determine structure type
    fn determine_structure_type(&self, line: &str) -> StructureType {
        let trimmed = line.trim();

        if trimmed.contains("module ") {
            StructureType::Module
        } else if trimmed.contains("data ") {
            StructureType::Class
        } else if trimmed.contains("type ") {
            StructureType::Struct // `newtype` contains this, and is one too
        } else if trimmed.contains("class ") {
            StructureType::Interface
        } else {
            StructureType::Class // Default
        }
    }
}

impl LanguageAnalyzer for HaskellAnalyzer {
    fn analyze_functions(&self, lines: &[String]) -> Result<Vec<FunctionInfo>> {
        let mut functions = Vec::new();

        for (i, line) in lines.iter().enumerate() {
            if let Some(func_name) = self.extract_function_name(line) {
                let end_line = self.find_function_end(lines, i);
                let complexity = self.calculate_cyclomatic_complexity(lines, i, end_line);
                let _visibility = self.determine_visibility(line);

                functions.push(FunctionInfo {
                    name: func_name,
                    line_count: end_line.saturating_sub(i).max(1),
                    cyclomatic_complexity: complexity,
                    cognitive_complexity: complexity,
                    nesting_depth: 0,
                    parameter_count: self.count_parameters(line),
                    return_path_count: 1,
                    start_line: i + 1,
                    end_line: end_line + 1,
                    is_method: false,
                    parent_class: None,
                    local_variable_count: 0,
                    has_recursion: false,
                    has_exception_handling: false,
                    visibility: Visibility::Public,
                });
            }
        }

        Ok(functions)
    }

    fn analyze_structures(&self, lines: &[String]) -> Result<Vec<StructureInfo>> {
        let mut structures = Vec::new();

        for (i, line) in lines.iter().enumerate() {
            if let Some(struct_name) = self.extract_structure_name(line) {
                let end_line = self.find_structure_end(lines, i);
                let structure_type = self.determine_structure_type(line);
                let _visibility = Visibility::Public; // Haskell structures are typically public

                structures.push(StructureInfo {
                    name: struct_name,
                    structure_type,
                    line_count: end_line.saturating_sub(i).max(1),
                    start_line: i + 1,
                    end_line: end_line + 1,
                    methods: Vec::new(),
                    properties: self.count_fields_in_structure(lines, i, end_line),
                    visibility: Visibility::Public,
                    inheritance_depth: 0,
                    interface_count: 0,
                });
            }
        }

        Ok(structures)
    }
}

impl HaskellAnalyzer {
    /// Count parameters in a function definition
    fn count_parameters(&self, line: &str) -> usize {
        // For type signatures, count arrows to estimate parameters
        if line.contains("::") {
            let parts: Vec<&str> = line.split("::").collect();
            if let Some(type_part) = parts.get(1) {
                return type_part.matches("->").count();
            }
        }

        // For function definitions, count arguments before =
        if line.contains('=') && !line.contains("::") {
            let parts: Vec<&str> = line.split('=').collect();
            if let Some(first_part) = parts.first() {
                let args: Vec<&str> = first_part.split_whitespace().collect();
                if args.len() > 1 {
                    return args.len() - 1; // Subtract 1 for the function name
                }
            }
        }

        0
    }

    /// Find the end of a structure definition
    fn find_structure_end(&self, lines: &[String], start_line: usize) -> usize {
        let mut in_structure = false;
        let mut base_indentation = None;

        for (i, line) in lines.iter().enumerate().skip(start_line) {
            let trimmed = line.trim();

            if !trimmed.is_empty() {
                let current_indentation = line.len() - line.trim_start().len();

                if !in_structure {
                    in_structure = true;
                    base_indentation = Some(current_indentation);
                } else {
                    // Check if we've moved to a new top-level definition
                    if current_indentation <= base_indentation.unwrap_or(0)
                        && (trimmed.contains("data ")
                            || trimmed.contains("newtype ")
                            || trimmed.contains("type ")
                            || trimmed.contains("class ")
                            || trimmed.contains("module "))
                        && !trimmed.starts_with("--")
                    {
                        return i.saturating_sub(1);
                    }
                }
            }
        }

        lines.len().saturating_sub(1)
    }

    /// Count fields within a structure
    fn count_fields_in_structure(
        &self,
        lines: &[String],
        start_line: usize,
        end_line: usize,
    ) -> usize {
        let mut count = 0;

        for line in lines.iter().take(end_line + 1).skip(start_line) {
            // Count constructor fields in data declarations
            if line.contains("data ") {
                // Simple heuristic: count type annotations in constructor
                count += line.matches("::").count();
            }

            // Count record fields
            if line.contains("{") && line.contains("}") {
                // Count commas in record syntax as field separators
                if let Some(start) = line.find('{') {
                    if let Some(end) = line.find('}') {
                        let record_part = &line[start + 1..end];
                        count += record_part.matches(',').count() + 1; // +1 for the last field
                    }
                }
            }
        }

        count
    }
}

impl Default for HaskellAnalyzer {
    fn default() -> Self {
        Self::new()
    }
}