debtmap 0.18.0

Code complexity and technical debt analyzer
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
422
423
424
425
426
427
428
429
430
//! Dependencies page (Page 2) - Call graph, blast radius, and coupling visualization.
//!
//! This page displays:
//! - Function-level dependency metrics (upstream/downstream counts)
//! - File-level coupling metrics with visual indicators (spec 203)
//! - Coupling classification badges with semantic coloring
//! - Instability progress bars with color gradients
//! - Lists of dependents and dependencies

use super::components::{add_label_value, add_section_header};
use crate::priority::UnifiedDebtItem;
use crate::tui::results::app::ResultsApp;
use crate::tui::theme::Theme;
use ratatui::{
    Frame,
    layout::Rect,
    style::Style,
    text::{Line, Span},
    widgets::{Block, Borders, Paragraph, Wrap},
};

/// Build all lines for the dependencies page (pure function).
///
/// This is public so text_extraction can reuse it for clipboard copy.
pub fn build_page_lines(item: &UnifiedDebtItem, theme: &Theme, width: u16) -> Vec<Line<'static>> {
    let mut lines = Vec::new();

    // Function-level Dependency Metrics section
    add_section_header(&mut lines, "function dependencies", theme);
    add_label_value(
        &mut lines,
        "upstream",
        item.upstream_dependencies.to_string(),
        theme,
        width,
    );
    add_label_value(
        &mut lines,
        "downstream",
        item.downstream_dependencies.to_string(),
        theme,
        width,
    );

    let blast_radius = item.upstream_dependencies + item.downstream_dependencies;
    add_label_value(
        &mut lines,
        "blast radius",
        blast_radius.to_string(),
        theme,
        width,
    );

    // Critical path indicator (simplified - based on high dependency count)
    let is_critical = item.upstream_dependencies > 5 || item.downstream_dependencies > 10;
    add_label_value(
        &mut lines,
        "critical",
        if is_critical { "Yes" } else { "No" }.to_string(),
        theme,
        width,
    );

    // File-level Coupling Metrics section (spec 201)
    build_file_coupling_section(&mut lines, item, theme, width);

    lines
}

/// Render dependencies page showing dependency metrics and blast radius
pub fn render(
    frame: &mut Frame,
    app: &ResultsApp,
    item: &UnifiedDebtItem,
    area: Rect,
    theme: &Theme,
) {
    let lines = build_page_lines(item, theme, area.width);

    let paragraph = Paragraph::new(lines)
        .block(Block::default().borders(Borders::NONE))
        .wrap(Wrap { trim: false })
        .scroll(app.detail_scroll_offset());

    frame.render_widget(paragraph, area);
}

/// Build function-level coupling metrics section with enhanced visualization.
///
/// For regular functions, displays function-level dependency data:
/// - Coupling classification badge with color coding
/// - Afferent coupling (Ca) - functions that call this function
/// - Efferent coupling (Ce) - functions this function calls
/// - Instability progress bar with color gradient
/// - Lists of callers (dependents) and callees (dependencies)
///
/// For god objects, aggregates file-level metrics from member functions.
fn build_file_coupling_section(
    lines: &mut Vec<Line<'static>>,
    item: &UnifiedDebtItem,
    theme: &Theme,
    width: u16,
) {
    // Use function-level dependency data from the item itself
    let afferent_coupling = item.upstream_dependencies;
    let efferent_coupling = item.downstream_dependencies;

    // Only show if we have meaningful coupling data
    let total_coupling = afferent_coupling + efferent_coupling;
    if total_coupling == 0 {
        return;
    }

    // Calculate instability: Ce / (Ca + Ce)
    let instability = if total_coupling > 0 {
        efferent_coupling as f64 / total_coupling as f64
    } else {
        0.0
    };

    lines.push(Line::from(""));
    add_section_header(lines, "coupling profile", theme);

    // Classification badge with color (spec 203)
    let classification =
        derive_coupling_classification(afferent_coupling, efferent_coupling, instability);
    render_classification_badge(lines, &classification, theme, width);

    // Afferent coupling (Ca) - functions that call this function
    add_label_value(
        lines,
        "afferent (ca)",
        afferent_coupling.to_string(),
        theme,
        width,
    );

    // Efferent coupling (Ce) - functions this function calls
    add_label_value(
        lines,
        "efferent (ce)",
        efferent_coupling.to_string(),
        theme,
        width,
    );

    // Instability with progress bar (spec 203)
    render_instability_bar(lines, instability, theme, width);

    // Add context note for extreme values
    if total_coupling > 15 {
        lines.push(Line::from(vec![
            Span::styled("Warning: ", Style::default().fg(ratatui::style::Color::Red)),
            Span::styled(
                "High coupling may indicate architectural issues.",
                Style::default().fg(theme.muted),
            ),
        ]));
    } else if instability < 0.1 && afferent_coupling > 0 {
        lines.push(Line::from(vec![
            Span::styled("Note: ", Style::default().fg(theme.primary)),
            Span::styled(
                "Stable core - changes need careful review.",
                Style::default().fg(theme.muted),
            ),
        ]));
    } else if instability > 0.9 {
        lines.push(Line::from(vec![
            Span::styled("Note: ", Style::default().fg(theme.success)),
            Span::styled(
                "Unstable leaf - safe to refactor.",
                Style::default().fg(theme.muted),
            ),
        ]));
    }

    // Dependents list (who calls this function)
    render_dependency_list(
        lines,
        &item.upstream_callers,
        "dependents (who calls this)",
        theme,
        width,
    );

    // Dependencies list (what this function calls)
    render_dependency_list(
        lines,
        &item.downstream_callees,
        "dependencies (what this calls)",
        theme,
        width,
    );
}

/// Render classification badge with semantic coloring (spec 203).
///
/// Displays classification as a colored badge like `[STABLE CORE]`
/// Uses the standard label-value column layout for consistency.
fn render_classification_badge(
    lines: &mut Vec<Line<'static>>,
    classification: &str,
    theme: &Theme,
    _width: u16,
) {
    const INDENT: usize = 2;
    const LABEL_WIDTH: usize = 24;
    const GAP: usize = 4;

    let badge_text = format!("[{}]", classification.to_uppercase());
    let badge_style = theme.coupling_badge_style(classification);

    let label_with_indent = format!("{}{}", " ".repeat(INDENT), "classification");
    let padded_label = format!("{:width$}", label_with_indent, width = LABEL_WIDTH);
    let gap = " ".repeat(GAP);

    lines.push(Line::from(vec![
        Span::raw(padded_label),
        Span::raw(gap),
        Span::styled(badge_text, badge_style),
    ]));
}

/// Render instability as a progress bar with color gradient (spec 203).
///
/// Format: `  instability              0.40 ████████░░░░░░░░░░░░`
/// Uses standard label-value column layout with progress bar appended.
/// Color: Green (0.0) -> Yellow (0.5) -> Red (1.0)
fn render_instability_bar(
    lines: &mut Vec<Line<'static>>,
    instability: f64,
    theme: &Theme,
    _width: u16,
) {
    const INDENT: usize = 2;
    const LABEL_WIDTH: usize = 24;
    const GAP: usize = 4;

    let label_with_indent = format!("{}{}", " ".repeat(INDENT), "instability");
    let padded_label = format!("{:width$}", label_with_indent, width = LABEL_WIDTH);
    let gap = " ".repeat(GAP);

    // Progress bar configuration
    let bar_width = 20;
    let filled = ((instability * bar_width as f64).round() as usize).min(bar_width);
    let empty = bar_width - filled;

    let bar_color = theme.instability_color(instability);
    let filled_bar: String = "â–ˆ".repeat(filled);
    let empty_bar: String = "â–‘".repeat(empty);

    lines.push(Line::from(vec![
        Span::raw(padded_label),
        Span::raw(gap),
        Span::styled(
            format!("{:.2} ", instability),
            Style::default().fg(theme.primary),
        ),
        Span::styled(filled_bar, Style::default().fg(bar_color)),
        Span::styled(empty_bar, Style::default().fg(theme.muted)),
    ]));
}

/// Render a dependency list section (spec 203).
///
/// Displays every item. The detail view is scrollable, and callers/callees are
/// analysis context that should not be hidden behind a summary count.
fn render_dependency_list(
    lines: &mut Vec<Line<'static>>,
    items: &[String],
    title: &str,
    theme: &Theme,
    _width: u16,
) {
    // Skip if empty
    if items.is_empty() {
        return;
    }

    lines.push(Line::from(""));
    add_section_header(lines, title, theme);

    for item in items {
        // Shorten path for display (show just filename or last component)
        let display_name = shorten_path(item);
        lines.push(Line::from(vec![Span::styled(
            format!("  {} {}", "\u{2022}", display_name), // bullet point
            Style::default().fg(theme.text),
        )]));
    }
}

/// Shorten a file path for display.
///
/// If the path contains a directory separator, show only the last component.
/// Otherwise, return the path as-is.
fn shorten_path(path: &str) -> &str {
    path.rsplit('/').next().unwrap_or(path)
}

/// Derive coupling classification from metrics (same logic as CouplingClassification).
fn derive_coupling_classification(afferent: usize, efferent: usize, instability: f64) -> String {
    let total = afferent + efferent;

    if total > 15 {
        "Highly Coupled".to_string()
    } else if total <= 2 {
        "Isolated".to_string()
    } else if instability < 0.3 && afferent > efferent {
        "Stable Core".to_string()
    } else if instability > 0.7 && efferent > afferent {
        "Leaf Module".to_string()
    } else {
        "Utility Module".to_string()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::priority::{
        ActionableRecommendation, DebtType, FunctionRole, ImpactMetrics, Location, UnifiedDebtItem,
        UnifiedScore,
    };
    use std::path::PathBuf;

    fn line_text(line: &Line<'_>) -> String {
        line.spans
            .iter()
            .map(|span| span.content.as_ref())
            .collect::<Vec<_>>()
            .join("")
    }

    fn create_item_with_dependencies() -> UnifiedDebtItem {
        UnifiedDebtItem {
            location: Location {
                file: PathBuf::from("src/main.rs"),
                line: 10,
                function: "target".to_string(),
            },
            debt_type: DebtType::ComplexityHotspot {
                cyclomatic: 20,
                cognitive: 25,
            },
            unified_score: UnifiedScore {
                complexity_factor: 50.0,
                coverage_factor: 20.0,
                dependency_factor: 30.0,
                role_multiplier: 1.0,
                final_score: 50.0,
                base_score: None,
                exponential_factor: None,
                risk_boost: None,
                pre_adjustment_score: None,
                adjustment_applied: None,
                purity_factor: None,
                refactorability_factor: None,
                pattern_factor: None,
                debt_adjustment: None,
                pre_normalization_score: None,
                structural_multiplier: None,
                has_coverage_data: false,
                contextual_risk_multiplier: None,
                pre_contextual_score: None,
                debt_type_multiplier: None,
            },
            function_role: FunctionRole::Unknown,
            recommendation: ActionableRecommendation::default(),
            expected_impact: ImpactMetrics {
                coverage_improvement: 0.0,
                lines_reduction: 0,
                complexity_reduction: 0.0,
                risk_reduction: 0.0,
            },
            transitive_coverage: None,
            file_context: None,
            upstream_dependencies: 6,
            downstream_dependencies: 6,
            upstream_callers: (1..=6)
                .map(|n| format!("src/callers/caller{n}.rs:caller{n}"))
                .collect(),
            downstream_callees: (1..=6)
                .map(|n| format!("src/callees/callee{n}.rs:callee{n}"))
                .collect(),
            upstream_production_callers: vec![],
            upstream_test_callers: vec![],
            production_blast_radius: 0,
            nesting_depth: 1,
            function_length: 20,
            cyclomatic_complexity: 20,
            cognitive_complexity: 25,
            entropy_analysis: None,
            is_pure: None,
            purity_confidence: None,
            purity_level: None,
            god_object_indicators: None,
            tier: None,
            function_context: None,
            context_confidence: None,
            contextual_recommendation: None,
            pattern_analysis: None,
            context_multiplier: None,
            context_type: None,
            language_specific: None,
            detected_pattern: None,
            contextual_risk: None,
            file_line_count: None,
            responsibility_category: None,
            error_swallowing_count: None,
            error_swallowing_patterns: None,
            context_suggestion: None,
        }
    }

    #[test]
    fn dependencies_page_lists_all_callers_and_callees() {
        let item = create_item_with_dependencies();
        let theme = Theme::default();
        let text = build_page_lines(&item, &theme, 100)
            .iter()
            .map(line_text)
            .collect::<Vec<_>>()
            .join("\n");

        assert!(text.contains("caller6.rs:caller6"), "{text}");
        assert!(text.contains("callee6.rs:callee6"), "{text}");
        assert!(!text.contains("(+"), "{text}");
    }
}