windjammer 0.48.0

A simple language inspired by Go, Ruby, and Elixir that transpiles to Rust - 80% of Rust's power with 20% of the complexity
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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
/// Error TUI: Interactive terminal UI for navigating and fixing errors
///
/// This module provides a beautiful terminal UI using ratatui for
/// navigating through compilation errors and applying fixes interactively.
use crate::error_mapper::{DiagnosticLevel, WindjammerDiagnostic};
use crossterm::{
    event::{self, DisableMouseCapture, EnableMouseCapture, Event, KeyCode, KeyEventKind},
    execute,
    terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
};
use ratatui::{
    backend::CrosstermBackend,
    layout::{Constraint, Direction, Layout, Rect},
    style::{Color, Modifier, Style},
    text::{Line, Span},
    widgets::{Block, Borders, List, ListItem, ListState, Paragraph, Wrap},
    Frame, Terminal,
};
use std::io;

/// TUI application state
pub struct ErrorTui {
    /// List of diagnostics to display
    diagnostics: Vec<WindjammerDiagnostic>,
    /// Currently selected error index
    selected: usize,
    /// List state for the error list
    list_state: ListState,
    /// Whether to show help
    show_help: bool,
}

impl ErrorTui {
    /// Create a new TUI application
    pub fn new(diagnostics: Vec<WindjammerDiagnostic>) -> Self {
        let mut list_state = ListState::default();
        if !diagnostics.is_empty() {
            list_state.select(Some(0));
        }

        Self {
            diagnostics,
            selected: 0,
            list_state,
            show_help: false,
        }
    }

    /// Run the TUI application
    pub fn run(&mut self) -> io::Result<Option<TuiAction>> {
        // Setup terminal
        enable_raw_mode()?;
        let mut stdout = io::stdout();
        execute!(stdout, EnterAlternateScreen, EnableMouseCapture)?;
        let backend = CrosstermBackend::new(stdout);
        let mut terminal = Terminal::new(backend)?;

        // Run the app
        let result = self.run_app(&mut terminal);

        // Restore terminal
        disable_raw_mode()?;
        execute!(
            terminal.backend_mut(),
            LeaveAlternateScreen,
            DisableMouseCapture
        )?;
        terminal.show_cursor()?;

        result
    }

    /// Main application loop
    fn run_app(
        &mut self,
        terminal: &mut Terminal<CrosstermBackend<io::Stdout>>,
    ) -> io::Result<Option<TuiAction>> {
        loop {
            terminal.draw(|f| self.ui(f))?;

            if let Event::Key(key) = event::read()? {
                if key.kind == KeyEventKind::Press {
                    match key.code {
                        KeyCode::Char('q') | KeyCode::Esc => return Ok(None),
                        KeyCode::Char('?') | KeyCode::F(1) => self.show_help = !self.show_help,
                        KeyCode::Down | KeyCode::Char('j') => self.next(),
                        KeyCode::Up | KeyCode::Char('k') => self.previous(),
                        KeyCode::Enter | KeyCode::Char(' ') if !self.diagnostics.is_empty() => {
                            return Ok(Some(TuiAction::ViewError(self.selected)));
                        }
                        KeyCode::Char('f') if !self.diagnostics.is_empty() => {
                            let diagnostic = &self.diagnostics[self.selected];
                            if diagnostic.is_fixable() {
                                return Ok(Some(TuiAction::FixError(self.selected)));
                            }
                        }
                        KeyCode::Char('e') if !self.diagnostics.is_empty() => {
                            let diagnostic = &self.diagnostics[self.selected];
                            if let Some(code) = &diagnostic.code {
                                return Ok(Some(TuiAction::ExplainError(code.clone())));
                            }
                        }
                        KeyCode::Char('a') => {
                            return Ok(Some(TuiAction::FixAll));
                        }
                        _ => {}
                    }
                }
            }
        }
    }

    /// Move to next error
    fn next(&mut self) {
        if self.diagnostics.is_empty() {
            return;
        }
        self.selected = (self.selected + 1) % self.diagnostics.len();
        self.list_state.select(Some(self.selected));
    }

    /// Move to previous error
    fn previous(&mut self) {
        if self.diagnostics.is_empty() {
            return;
        }
        if self.selected > 0 {
            self.selected -= 1;
        } else {
            self.selected = self.diagnostics.len() - 1;
        }
        self.list_state.select(Some(self.selected));
    }

    /// Render the UI
    fn ui(&mut self, f: &mut Frame) {
        let chunks = Layout::default()
            .direction(Direction::Vertical)
            .constraints([
                Constraint::Length(3), // Title
                Constraint::Min(10),   // Main content
                Constraint::Length(3), // Status bar
            ])
            .split(f.area());

        // Title
        self.render_title(f, chunks[0]);

        if self.show_help {
            // Show help screen
            self.render_help(f, chunks[1]);
        } else {
            // Split main area into error list and detail
            let main_chunks = Layout::default()
                .direction(Direction::Horizontal)
                .constraints([Constraint::Percentage(40), Constraint::Percentage(60)])
                .split(chunks[1]);

            // Error list
            self.render_error_list(f, main_chunks[0]);

            // Error detail
            self.render_error_detail(f, main_chunks[1]);
        }

        // Status bar
        self.render_status_bar(f, chunks[2]);
    }

    /// Render title bar
    fn render_title(&self, f: &mut Frame, area: Rect) {
        let title = Paragraph::new("Windjammer Error Navigator")
            .style(
                Style::default()
                    .fg(Color::Cyan)
                    .add_modifier(Modifier::BOLD),
            )
            .block(Block::default().borders(Borders::ALL));
        f.render_widget(title, area);
    }

    /// Render error list
    fn render_error_list(&mut self, f: &mut Frame, area: Rect) {
        let items: Vec<ListItem> = self
            .diagnostics
            .iter()
            .map(|diag| {
                let (icon, color) = match diag.level {
                    DiagnosticLevel::Error => ("", Color::Red),
                    DiagnosticLevel::Warning => ("", Color::Yellow),
                    DiagnosticLevel::Note => ("", Color::Blue),
                    DiagnosticLevel::Help => ("💡", Color::Green),
                };

                let code_str = if let Some(code) = &diag.code {
                    format!("[{}] ", code)
                } else {
                    String::new()
                };

                let fixable = if diag.is_fixable() { " [F]" } else { "" };

                let content = format!(
                    "{} {}{} - {}:{}{}",
                    icon,
                    code_str,
                    diag.message.chars().take(40).collect::<String>(),
                    diag.location
                        .file
                        .file_name()
                        .unwrap_or_default()
                        .to_string_lossy(),
                    diag.location.line,
                    fixable
                );

                ListItem::new(content).style(Style::default().fg(color))
            })
            .collect();

        let list = List::new(items)
            .block(
                Block::default()
                    .title(format!(
                        "Errors ({}/{})",
                        self.selected + 1,
                        self.diagnostics.len()
                    ))
                    .borders(Borders::ALL),
            )
            .highlight_style(
                Style::default()
                    .bg(Color::DarkGray)
                    .add_modifier(Modifier::BOLD),
            )
            .highlight_symbol(">> ");

        f.render_stateful_widget(list, area, &mut self.list_state);
    }

    /// Render error detail
    fn render_error_detail(&self, f: &mut Frame, area: Rect) {
        if self.diagnostics.is_empty() {
            let text = Paragraph::new("No errors to display!")
                .style(Style::default().fg(Color::Green))
                .block(Block::default().title("Detail").borders(Borders::ALL))
                .wrap(Wrap { trim: true });
            f.render_widget(text, area);
            return;
        }

        let diagnostic = &self.diagnostics[self.selected];

        let mut lines = vec![];

        // Error message
        let (level_str, level_color) = match diagnostic.level {
            DiagnosticLevel::Error => ("ERROR", Color::Red),
            DiagnosticLevel::Warning => ("WARNING", Color::Yellow),
            DiagnosticLevel::Note => ("NOTE", Color::Blue),
            DiagnosticLevel::Help => ("HELP", Color::Green),
        };

        lines.push(Line::from(vec![
            Span::styled(
                level_str,
                Style::default()
                    .fg(level_color)
                    .add_modifier(Modifier::BOLD),
            ),
            Span::raw(": "),
            Span::raw(&diagnostic.message),
        ]));

        if let Some(code) = &diagnostic.code {
            lines.push(Line::from(vec![
                Span::styled("Code: ", Style::default().fg(Color::Cyan)),
                Span::raw(code),
            ]));
        }

        lines.push(Line::from(""));

        // Location
        lines.push(Line::from(vec![
            Span::styled("Location: ", Style::default().fg(Color::Cyan)),
            Span::raw(format!(
                "{}:{}:{}",
                diagnostic.location.file.display(),
                diagnostic.location.line,
                diagnostic.location.column
            )),
        ]));

        lines.push(Line::from(""));

        // Help messages
        if !diagnostic.help.is_empty() {
            lines.push(Line::from(Span::styled(
                "Help:",
                Style::default()
                    .fg(Color::Green)
                    .add_modifier(Modifier::BOLD),
            )));
            for help in &diagnostic.help {
                lines.push(Line::from(format!("{}", help)));
            }
            lines.push(Line::from(""));
        }

        // Notes
        if !diagnostic.notes.is_empty() {
            lines.push(Line::from(Span::styled(
                "Notes:",
                Style::default()
                    .fg(Color::Blue)
                    .add_modifier(Modifier::BOLD),
            )));
            for note in &diagnostic.notes {
                lines.push(Line::from(format!("{}", note)));
            }
            lines.push(Line::from(""));
        }

        // Fixable status
        if diagnostic.is_fixable() {
            lines.push(Line::from(vec![
                Span::styled("", Style::default().fg(Color::Green)),
                Span::styled(
                    "This error can be fixed automatically!",
                    Style::default().fg(Color::Green),
                ),
            ]));
            lines.push(Line::from(Span::styled(
                "Press 'f' to apply fix",
                Style::default().fg(Color::Yellow),
            )));
        }

        let text = Paragraph::new(lines)
            .block(Block::default().title("Detail").borders(Borders::ALL))
            .wrap(Wrap { trim: true });

        f.render_widget(text, area);
    }

    /// Render help screen
    fn render_help(&self, f: &mut Frame, area: Rect) {
        let help_text = vec![
            Line::from(Span::styled(
                "Keyboard Shortcuts",
                Style::default()
                    .fg(Color::Cyan)
                    .add_modifier(Modifier::BOLD),
            )),
            Line::from(""),
            Line::from(vec![
                Span::styled("↑/k", Style::default().fg(Color::Yellow)),
                Span::raw("       - Previous error"),
            ]),
            Line::from(vec![
                Span::styled("↓/j", Style::default().fg(Color::Yellow)),
                Span::raw("       - Next error"),
            ]),
            Line::from(vec![
                Span::styled("Enter/Space", Style::default().fg(Color::Yellow)),
                Span::raw(" - View error details"),
            ]),
            Line::from(vec![
                Span::styled("f", Style::default().fg(Color::Yellow)),
                Span::raw("         - Fix current error (if fixable)"),
            ]),
            Line::from(vec![
                Span::styled("a", Style::default().fg(Color::Yellow)),
                Span::raw("         - Fix all fixable errors"),
            ]),
            Line::from(vec![
                Span::styled("e", Style::default().fg(Color::Yellow)),
                Span::raw("         - Explain error code"),
            ]),
            Line::from(vec![
                Span::styled("?/F1", Style::default().fg(Color::Yellow)),
                Span::raw("      - Toggle this help"),
            ]),
            Line::from(vec![
                Span::styled("q/Esc", Style::default().fg(Color::Yellow)),
                Span::raw("     - Quit"),
            ]),
            Line::from(""),
            Line::from(Span::styled(
                "Legend",
                Style::default()
                    .fg(Color::Cyan)
                    .add_modifier(Modifier::BOLD),
            )),
            Line::from(""),
            Line::from(vec![
                Span::styled("", Style::default().fg(Color::Red)),
                Span::raw("- Error"),
            ]),
            Line::from(vec![
                Span::styled("", Style::default().fg(Color::Yellow)),
                Span::raw("- Warning"),
            ]),
            Line::from(vec![
                Span::styled("", Style::default().fg(Color::Blue)),
                Span::raw("- Note"),
            ]),
            Line::from(vec![
                Span::styled("💡 ", Style::default().fg(Color::Green)),
                Span::raw("- Help"),
            ]),
            Line::from(vec![
                Span::styled("[F]", Style::default().fg(Color::Green)),
                Span::raw(" - Fixable"),
            ]),
        ];

        let help = Paragraph::new(help_text)
            .block(Block::default().title("Help").borders(Borders::ALL))
            .wrap(Wrap { trim: true });

        f.render_widget(help, area);
    }

    /// Render status bar
    fn render_status_bar(&self, f: &mut Frame, area: Rect) {
        let status = if self.show_help {
            "Press ? or F1 to close help"
        } else {
            "Press ? for help | ↑↓ to navigate | f to fix | a to fix all | e to explain | q to quit"
        };

        let status_bar = Paragraph::new(status)
            .style(Style::default().fg(Color::White).bg(Color::DarkGray))
            .block(Block::default());

        f.render_widget(status_bar, area);
    }
}

/// Action returned by the TUI
#[derive(Debug, Clone)]
pub enum TuiAction {
    /// View error details
    ViewError(usize),
    /// Fix a specific error
    FixError(usize),
    /// Fix all fixable errors
    FixAll,
    /// Explain an error code
    ExplainError(String),
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::source_map::Location;
    use std::path::PathBuf;

    fn create_test_diagnostic() -> WindjammerDiagnostic {
        WindjammerDiagnostic {
            message: "Test error".to_string(),
            level: DiagnosticLevel::Error,
            location: Location {
                file: PathBuf::from("test.wj"),
                line: 1,
                column: 1,
            },
            spans: vec![],
            code: Some("WJ0001".to_string()),
            help: vec!["This is a test".to_string()],
            notes: vec![],
        }
    }

    #[test]
    fn test_tui_creation() {
        let diagnostics = vec![create_test_diagnostic()];
        let tui = ErrorTui::new(diagnostics);
        assert_eq!(tui.selected, 0);
        assert_eq!(tui.diagnostics.len(), 1);
    }

    #[test]
    fn test_navigation() {
        let diagnostics = vec![
            create_test_diagnostic(),
            create_test_diagnostic(),
            create_test_diagnostic(),
        ];
        let mut tui = ErrorTui::new(diagnostics);

        assert_eq!(tui.selected, 0);
        tui.next();
        assert_eq!(tui.selected, 1);
        tui.next();
        assert_eq!(tui.selected, 2);
        tui.next();
        assert_eq!(tui.selected, 0); // Wraps around

        tui.previous();
        assert_eq!(tui.selected, 2); // Wraps around
        tui.previous();
        assert_eq!(tui.selected, 1);
    }
}