arbor-gui 2.0.1

Arbor GUI - Impact-first code analysis interface
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
// Full file skipped by rustfmt manually via block-level attributes to avoid unstable inner attributes
//! Main application state and UI logic

use arbor_graph::ArborGraph;
use arbor_watcher::{index_directory, IndexOptions};
use eframe::egui;
use std::path::PathBuf;

/// Analysis result for display
#[rustfmt::skip]
#[derive(Default)]
struct AnalysisResult {
    target_name: String,
    target_file: String,
    role: String,
    direct_callers: Vec<String>,
    indirect_callers: Vec<String>,
    downstream: Vec<String>,
    total_affected: usize,
    confidence: String,
}

/// Main application state
#[rustfmt::skip]
pub struct ArborApp {
    /// Current working directory
    cwd: PathBuf,

    /// Symbol input field
    symbol_input: String,

    /// Indexed graph (lazy loaded)
    graph: Option<ArborGraph>,

    /// Current analysis result
    result: Option<AnalysisResult>,

    /// Status message
    status: String,

    /// Is analysis in progress
    #[allow(dead_code)]
    loading: bool,

    /// Dark mode toggle
    dark_mode: bool,

    /// Search history
    search_history: Vec<String>,

    /// Show call tree (collapsible)
    #[allow(dead_code)]
    show_call_tree: bool,

    /// Show dependencies (collapsible)
    #[allow(dead_code)]
    show_dependencies: bool,

    /// Show file path (spoiler mode - click to reveal)
    show_file_path: bool,
}

#[rustfmt::skip]
impl ArborApp {
    pub fn new(_cc: &eframe::CreationContext<'_>) -> Self {
        Self {
            cwd: std::env::current_dir().unwrap_or_default(),
            symbol_input: String::new(),
            graph: None,
            result: None,
            status: "Ready. Enter a symbol name to analyze.".to_string(),
            loading: false,
            dark_mode: true,
            search_history: Vec::new(),
            show_call_tree: true,
            show_dependencies: true,
            show_file_path: false, // Hidden by default (spoiler mode)
        }
    }

    fn analyze(&mut self) {
        if self.symbol_input.trim().is_empty() {
            self.status = "Please enter a symbol name.".to_string();
            return;
        }

        // Index if not already done
        if self.graph.is_none() {
            self.status = "Indexing codebase...".to_string();
            match index_directory(&self.cwd, IndexOptions::default()) {
                Ok(result) => {
                    self.graph = Some(result.graph);
                    self.status = format!("Indexed {} nodes.", result.nodes_extracted);
                }
                Err(e) => {
                    self.status = format!("Indexing failed: {}", e);
                    return;
                }
            }
        }

        // Analyze the symbol
        if let Some(graph) = &self.graph {
            let target = self.symbol_input.trim();

            // Find the node
            let node_idx = graph.get_index(target).or_else(|| {
                graph
                    .find_by_name(target)
                    .first()
                    .and_then(|n| graph.get_index(&n.id))
            });

            match node_idx {
                Some(idx) => {
                    let node = graph.get(idx).unwrap();
                    let analysis = graph.analyze_impact(idx, 5);

                    let has_upstream = !analysis.upstream.is_empty();
                    let has_downstream = !analysis.downstream.is_empty();

                    let role = match (has_upstream, has_downstream) {
                        (false, false) => "Isolated",
                        (false, true) => "Entry Point",
                        (true, false) => "Utility",
                        (true, true) => "Core Logic",
                    };

                    let direct: Vec<_> = analysis
                        .all_affected()
                        .into_iter()
                        .filter(|n| n.severity == arbor_graph::ImpactSeverity::Direct)
                        .map(|n| format!("{} ({})", n.node_info.name, n.node_info.kind))
                        .collect();

                    let indirect: Vec<_> = analysis
                        .all_affected()
                        .into_iter()
                        .filter(|n| n.severity != arbor_graph::ImpactSeverity::Direct)
                        .map(|n| format!("{} ({} hops)", n.node_info.name, n.hop_distance))
                        .collect();

                    let downstream: Vec<_> = analysis
                        .downstream
                        .iter()
                        .take(10)
                        .map(|n| format!("{} ({})", n.node_info.name, n.entry_edge))
                        .collect();

                    self.result = Some(AnalysisResult {
                        target_name: node.name.clone(),
                        target_file: node.file.clone(),
                        role: role.to_string(),
                        direct_callers: direct,
                        indirect_callers: indirect,
                        downstream,
                        total_affected: analysis.total_affected,
                        confidence: if analysis.total_affected == 0 {
                            "Low (no edges found)".to_string()
                        } else {
                            "High".to_string()
                        },
                    });

                    self.status = format!("Analyzed '{}'", target);

                    // Add to search history
                    let query = target.to_string();
                    if !self.search_history.contains(&query) {
                        self.search_history.insert(0, query);
                        if self.search_history.len() > 10 {
                            self.search_history.pop();
                        }
                    }
                }
                None => {
                    self.result = None;
                    self.status = format!(
                        "Symbol '{}' not found. Try: arbor status --files to see indexed files.",
                        target
                    );
                }
            }
        }
    }

    #[allow(dead_code)]
    fn copy_as_markdown(&self) -> String {
        if let Some(r) = &self.result {
            let mut md = format!("## Impact Analysis: {}\n\n", r.target_name);
            md += &format!("**File:** `{}`\n", r.target_file);
            md += &format!("**Role:** {}\n", r.role);
            md += &format!("**Confidence:** {}\n\n", r.confidence);

            if !r.direct_callers.is_empty() {
                md += "### Direct Callers (will break immediately)\n";
                for c in &r.direct_callers {
                    md += &format!("- {}\n", c);
                }
                md += "\n";
            }

            if !r.indirect_callers.is_empty() {
                md += "### Indirect Callers (may break)\n";
                for c in r.indirect_callers.iter().take(5) {
                    md += &format!("- {}\n", c);
                }
                md += "\n";
            }

            md += &format!("**Total Affected:** {} nodes\n", r.total_affected);
            md
        } else {
            String::new()
        }
    }
}

#[rustfmt::skip]
impl eframe::App for ArborApp {
    #[rustfmt::skip]
    fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
        // Apply theme
        if self.dark_mode {
            ctx.set_visuals(egui::Visuals::dark());
        } else {
            ctx.set_visuals(egui::Visuals::light());
        }

        egui::CentralPanel::default().show(ctx, |ui| {
            // Header
            ui.horizontal(|ui| {
                // ui.heading("🌲 Arbor");
                ui.image(egui::include_image!("../assets/arbor-logo.svg"));
                ui.heading("Arbor");
                ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| {
                    if ui.button(if self.dark_mode { "" } else { "🌙" }).clicked() {
                        self.dark_mode = !self.dark_mode;
                    }
                });
            });

            ui.separator();

            // Input section
            ui.horizontal(|ui| {
                ui.label("Symbol:");
                let response = ui.text_edit_singleline(&mut self.symbol_input);
                if response.lost_focus() && ui.input(|i| i.key_pressed(egui::Key::Enter)) {
                    self.analyze();
                }
                if ui.button("🔍 Analyze").clicked() {
                    self.analyze();
                }
            });

            // Search history
            let mut clicked_query: Option<String> = None;
            if !self.search_history.is_empty() {
                ui.horizontal(|ui| {
                    ui.label(egui::RichText::new("History:").small().weak());
                    for query in self.search_history.iter().take(5) {
                        if ui.small_button(query).clicked() {
                            clicked_query = Some(query.clone());
                        }
                    }
                });
            }
            if let Some(query) = clicked_query {
                self.symbol_input = query;
                self.analyze();
            }

            ui.label(egui::RichText::new(&self.status).small().weak());

            ui.separator();

            // Results section - extract values to avoid borrow issues
            let result_data = self.result.as_ref().map(|r| {
                (r.target_name.clone(), r.target_file.clone(), r.role.clone(), 
                 r.confidence.clone(), r.direct_callers.clone(), r.indirect_callers.clone(),
                 r.downstream.clone(), r.total_affected)
            });
            
            let mut toggle_file_path = false;
            let mut toggle_hide_path = false;

            if let Some((target_name, target_file, role, confidence, direct_callers, indirect_callers, downstream, total_affected)) = result_data {
                egui::ScrollArea::vertical().show(ui, |ui| {
                    ui.heading(&target_name);
                    
                    // File path with Discord-style spoiler
                    ui.horizontal(|ui| {
                        ui.label("File:");
                        if self.show_file_path {
                            ui.label(egui::RichText::new(&target_file).monospace());
                            if ui.small_button("🙈 Hide").clicked() {
                                toggle_hide_path = true;
                            }
                        } else {
                            // Spoiler box - click to reveal
                            let spoiler_text = "████████████████";
                            let button = egui::Button::new(
                                egui::RichText::new(spoiler_text)
                                    .background_color(egui::Color32::DARK_GRAY)
                                    .color(egui::Color32::DARK_GRAY),
                            )
                            .frame(false);

                            if ui
                                .add(button)
                                .on_hover_text("Click to reveal file path")
                                .clicked()
                            {
                                toggle_file_path = true;
                            }
                        }
                    });

                    ui.horizontal(|ui| {
                        ui.label("Role:");
                        ui.label(egui::RichText::new(&role).strong());
                    });
                    ui.horizontal(|ui| {
                        ui.label("Confidence:");
                        ui.label(&confidence);
                    });
                    // Confidence explainer
                    ui.label(egui::RichText::new("Based on static call graph and resolved symbols.").small().weak());

                    ui.add_space(10.0);

                    // Direct callers
                    if !direct_callers.is_empty() {
                        ui.label(egui::RichText::new("⚠️ Will break immediately:").strong().color(egui::Color32::RED));
                        for c in &direct_callers {
                            ui.label(format!("{}", c));
                        }
                    }

                    ui.add_space(5.0);

                    // Indirect callers
                    if !indirect_callers.is_empty() {
                        ui.label(egui::RichText::new("May break indirectly:").strong().color(egui::Color32::YELLOW));
                        for c in indirect_callers.iter().take(5) {
                            ui.label(format!("{}", c));
                        }
                        if indirect_callers.len() > 5 {
                            ui.label(format!("  ... and {} more", indirect_callers.len() - 5));
                        }
                    }

                    ui.add_space(5.0);

                    // Downstream dependencies with clearer labels
                    if !downstream.is_empty() {
                        ui.label(egui::RichText::new("Dependencies:").strong());
                        ui.label(egui::RichText::new("This function calls these lower-level helpers.").small().weak());
                        for d in &downstream {
                            ui.label(format!("  → Calls {}", d));
                        }
                    }

                    ui.add_space(10.0);

                    ui.label(format!("Total affected: {} nodes", total_affected));

                    ui.add_space(10.0);

                    // Copy button
                    if ui.button("📋 Copy as Markdown").clicked() {
                        if let Ok(mut clipboard) = arboard::Clipboard::new() {
                            let md = format!(
                                "## Impact Analysis: {}\n\n**File:** `{}`\n**Role:** {}\n**Confidence:** {}\n**Total Affected:** {} nodes",
                                target_name, target_file, role, confidence, total_affected
                            );
                            let _ = clipboard.set_text(md);
                        }
                    }
                });
                
                // Apply spoiler toggles after closure
                if toggle_file_path {
                    self.show_file_path = true;
                }
                if toggle_hide_path {
                    self.show_file_path = false;
                }
            } else {
                ui.centered_and_justified(|ui| {
                    ui.label("Enter a function or class name above to analyze its impact.");
                });
            }
            
            // Version watermark (bottom right)
            ui.with_layout(egui::Layout::bottom_up(egui::Align::RIGHT), |ui| {
                ui.add_space(4.0);
                ui.label(
                    egui::RichText::new(format!("Arbor v{}", env!("CARGO_PKG_VERSION")))
                        .small()
                        .weak(),
                );
            });
        });
    }
}