mnml-rs 0.2.14

A NvChad-style terminal IDE in Rust — vim or standard editing, LSP, git, and an embedded HTTP client.
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
//! TODOs activity-bar panel — surfaces `TODO` / `FIXME` / `XXX` /
//! `HACK` / `REVIEW` markers found in source-code comments across
//! the workspace. (#9)
//!
//! v1 scope: workspace-wide scan on activation + rescan via
//! `todos.refresh`. One row per hit: `TAG  path:line  title`.
//! Click → jump to the file at that line. Deletion / test-tags /
//! `.mnml/notes/` markdown checkbox integration are follow-ups.

use ratatui::{
    Frame,
    layout::Rect,
    style::{Modifier, Style},
    text::{Line, Span},
    widgets::{Block, Paragraph},
};

use crate::app::App;
use crate::ui::theme;

/// One found marker. Populated by `App::todos_panel_refresh`.
#[derive(Debug, Clone)]
pub struct TodoHit {
    pub tag: &'static str,
    pub path: std::path::PathBuf,
    pub line: u32,
    pub title: String,
}

/// Marker patterns scanned in comments. Case-sensitive, matched
/// followed by non-alphanumeric so `TODOLIST` doesn't false-trip.
pub const MARKERS: &[&str] = &["TODO", "FIXME", "XXX", "HACK", "REVIEW"];

pub fn draw(frame: &mut Frame, app: &mut App, area: Rect) {
    let t = theme::cur();
    let bg = t.bg_darker;
    frame.render_widget(Block::default().style(Style::default().bg(bg)), area);
    if area.height < 2 || area.width < 8 {
        return;
    }
    app.rects.todos_panel_rows.clear();
    app.rects.todos_panel_refresh_chip = None;
    app.rects.todos_panel_filter_input = None;

    // Trigger a background rescan the first time this panel appears
    // in a session (todos_hits is empty and no scan yet).
    if !app.todos_panel_scanned_once {
        app.todos_panel_scanned_once = true;
        app.todos_panel_refresh();
    }

    // Apply the `/`-filter to the hit list — matches tag, path, or
    // title case-insensitively. Filter row is drawn between the
    // section header and the results (parity with HTTP / Agents).
    let filter_lc = app.todos_panel_filter.to_ascii_lowercase();
    let filtered: Vec<(usize, &TodoHit)> = app
        .todos_hits
        .iter()
        .enumerate()
        .filter(|(_, hit)| {
            if filter_lc.is_empty() {
                return true;
            }
            hit.tag.to_ascii_lowercase().contains(&filter_lc)
                || hit
                    .path
                    .to_string_lossy()
                    .to_ascii_lowercase()
                    .contains(&filter_lc)
                || hit.title.to_ascii_lowercase().contains(&filter_lc)
        })
        .collect();

    frame.render_widget(
        Paragraph::new(Line::from(vec![
            Span::styled(" ", Style::default().bg(bg)),
            Span::styled(
                "TODOS",
                Style::default()
                    .fg(t.comment)
                    .bg(bg)
                    .add_modifier(Modifier::BOLD),
            ),
            Span::styled(
                // Only show a count when the filter is active —
                // design-critic 2026-07-09 flagged parity with
                // Notes / Sessions / HTTP (which all show the
                // count only when filter is on).
                if filter_lc.is_empty() {
                    String::new()
                } else {
                    format!("  ({} of {})", filtered.len(), app.todos_hits.len())
                },
                Style::default()
                    .fg(t.comment)
                    .bg(bg)
                    .add_modifier(Modifier::DIM),
            ),
        ])),
        Rect {
            x: area.x,
            y: area.y,
            width: area.width,
            height: 1,
        },
    );
    // Filter row (row 1). Matches `http_panel::draw` visual — chip
    // background, magnifier glyph, `/ filter` placeholder, `▏` cursor
    // when focused.
    {
        let y_filter = area.y + 1;
        if y_filter < area.y + area.height {
            let focused = app.todos_panel_filter_focused;
            let bg_chip = t.bg2;
            let fg_chip = if app.todos_panel_filter.is_empty() && !focused {
                t.comment
            } else {
                t.fg
            };
            let display = if app.todos_panel_filter.is_empty() {
                if focused {
                    "type to filter\u{2026}".to_string()
                } else {
                    "/ filter".to_string()
                }
            } else {
                app.todos_panel_filter.clone()
            };
            let cursor = if focused { "\u{258F}" } else { " " };
            let pad = (area.width as usize).saturating_sub(3 + display.chars().count() + 1 + 1);
            let line = Line::from(vec![
                Span::styled(" ", Style::default().bg(bg)),
                Span::styled("\u{F0349} ", Style::default().fg(t.comment).bg(bg_chip)),
                Span::styled(display, Style::default().fg(fg_chip).bg(bg_chip)),
                Span::styled(cursor, Style::default().fg(t.cyan).bg(bg_chip)),
                Span::styled(" ".repeat(pad), Style::default().bg(bg_chip)),
                Span::styled(" ", Style::default().bg(bg)),
            ]);
            let row_rect = Rect {
                x: area.x,
                y: y_filter,
                width: area.width,
                height: 1,
            };
            frame.render_widget(Paragraph::new(line), row_rect);
            app.rects.todos_panel_filter_input = Some(row_rect);
        }
    }
    let mut y = area.y + 3;

    if app.todos_hits.is_empty() {
        frame.render_widget(
            Paragraph::new(Line::from(vec![
                Span::styled("  ", Style::default().bg(bg)),
                Span::styled(
                    "No markers found — click ⟳ Rescan below.",
                    Style::default().fg(t.comment).bg(bg),
                ),
            ])),
            Rect {
                x: area.x,
                y,
                width: area.width,
                height: 1,
            },
        );
        y += 1;
        frame.render_widget(
            Paragraph::new(Line::from(vec![
                Span::styled("  ", Style::default().bg(bg)),
                Span::styled(
                    "Scans for TODO / FIXME / XXX / HACK / REVIEW.",
                    Style::default()
                        .fg(t.comment)
                        .bg(bg)
                        .add_modifier(Modifier::DIM),
                ),
            ])),
            Rect {
                x: area.x,
                y,
                width: area.width,
                height: 1,
            },
        );
        y += 2;
    } else if filtered.is_empty() {
        frame.render_widget(
            Paragraph::new(Line::from(vec![
                Span::styled("  ", Style::default().bg(bg)),
                Span::styled(
                    "No matches — Esc clears",
                    Style::default().fg(t.comment).bg(bg),
                ),
            ])),
            Rect {
                x: area.x,
                y,
                width: area.width,
                height: 1,
            },
        );
        y += 2;
    } else {
        // Clamp the cursor to the filtered length so a stale
        // cursor after a filter narrows doesn't paint nothing.
        let clamped_cursor = app.todos_panel_cursor.min(filtered.len().saturating_sub(1));
        app.todos_panel_cursor = clamped_cursor;
        for (row_i, (idx, hit)) in filtered
            .iter()
            .copied()
            .take(area.height.saturating_sub(5) as usize)
            .enumerate()
        {
            if y >= area.y + area.height {
                break;
            }
            let is_focused_row = row_i == clamped_cursor;
            let row_bg = if is_focused_row { t.bg2 } else { bg };
            let rel = hit
                .path
                .strip_prefix(&app.workspace)
                .unwrap_or(&hit.path)
                .to_string_lossy()
                .into_owned();
            let tag_fg = match hit.tag {
                "TODO" => t.blue,
                "FIXME" => t.orange,
                "XXX" | "HACK" => t.red,
                "REVIEW" => t.purple,
                _ => t.comment,
            };
            let row_rect = Rect {
                x: area.x,
                y,
                width: area.width,
                height: 1,
            };
            let path_line = format!(" {rel}:{}", hit.line);
            let title: String = hit.title.chars().take(40).collect();
            frame.render_widget(
                Paragraph::new(Line::from(vec![
                    Span::styled(" ", Style::default().bg(row_bg)),
                    Span::styled(
                        hit.tag,
                        Style::default()
                            .fg(tag_fg)
                            .bg(row_bg)
                            .add_modifier(Modifier::BOLD),
                    ),
                    Span::styled(path_line, Style::default().fg(t.comment).bg(row_bg)),
                    Span::styled(" ", Style::default().bg(row_bg)),
                    Span::styled(title, Style::default().fg(t.fg).bg(row_bg)),
                ])),
                row_rect,
            );
            app.rects.todos_panel_rows.push((row_rect, idx));
            y += 1;
        }
        y += 1;
    }

    if y < area.y + area.height {
        let refresh_rect = Rect {
            x: area.x,
            y,
            width: area.width,
            height: 1,
        };
        frame.render_widget(
            Paragraph::new(Line::from(vec![
                Span::styled("  ", Style::default().bg(bg)),
                Span::styled(
                    // `⟳` eats its right sidebearing in Nerd Font +
                    // CoreText; a 2-space gap keeps it visually
                    // matched to other action rows in the app.
                    "⟳  Rescan",
                    Style::default()
                        .fg(t.cyan)
                        .bg(bg)
                        .add_modifier(Modifier::BOLD),
                ),
            ])),
            refresh_rect,
        );
        app.rects.todos_panel_refresh_chip = Some(refresh_rect);
    }
}

/// Grab the first double-quoted or single-quoted string on the
/// slice — the Playwright/Jest test title when the modifier call
/// looks like `.fixme("title", async ({ page }) => …)`.
fn extract_first_quoted(s: &str) -> Option<String> {
    let mut chars = s.char_indices().peekable();
    while let Some((i, c)) = chars.next() {
        if c == '"' || c == '\'' {
            let quote = c;
            let start = i + c.len_utf8();
            for (j, cc) in chars.by_ref() {
                if cc == quote {
                    return Some(s[start..j].chars().take(120).collect());
                }
            }
            return None;
        }
    }
    None
}

/// Playwright/Jest test-modifier markers picked up in `.spec.ts`,
/// `.test.ts`, `.spec.js`, `.test.js`. These are call-site tokens
/// (`test.fixme(...)`, `test.fail(...)`, `test.skip(...)`) that
/// tag a test as pending / expected-to-fail / disabled — user
/// feedback: they belong in the TODOs surface even though they're
/// not in a comment.
const TEST_MODIFIER_MARKERS: &[&str] = &["fixme", "fail", "skip"];

fn is_playwright_test_file(path: &std::path::Path) -> bool {
    let name = path
        .file_name()
        .and_then(|s| s.to_str())
        .unwrap_or_default();
    name.ends_with(".spec.ts")
        || name.ends_with(".test.ts")
        || name.ends_with(".spec.js")
        || name.ends_with(".test.js")
}

/// Scan a file for marker patterns in comments. Cheap enough per
/// file for a workspace-wide scan (~200 typical), but skips large
/// files, binaries, and generated dirs.
pub fn scan_file(path: &std::path::Path) -> Vec<TodoHit> {
    let Ok(meta) = std::fs::metadata(path) else {
        return Vec::new();
    };
    if meta.len() > 1024 * 1024 {
        return Vec::new(); // skip huge files
    }
    let Ok(content) = std::fs::read_to_string(path) else {
        return Vec::new(); // non-UTF-8 → binary → skip
    };
    let mut out = Vec::new();
    let scan_test_modifiers = is_playwright_test_file(path);
    for (i, line) in content.lines().enumerate() {
        // Playwright/Jest test modifiers first — `.fixme(` / `.fail(`
        // / `.skip(` are call-site tokens, not comment markers, so
        // they get their own detection path per line. FIXME wins
        // (higher-severity mapping) if both match.
        if scan_test_modifiers {
            let mut matched = false;
            for &modifier in TEST_MODIFIER_MARKERS {
                let needle = format!(".{modifier}(");
                if let Some(pos) = line.find(&needle) {
                    let tag: &'static str = match modifier {
                        "fixme" => "FIXME",
                        "fail" => "XXX",
                        "skip" => "REVIEW",
                        _ => continue,
                    };
                    let after = pos + needle.len();
                    // Grab the test title if the call is
                    // `.fixme("title", …)` — first quoted string on
                    // the same line.
                    let title = extract_first_quoted(&line[after..])
                        .unwrap_or_else(|| format!(".{modifier}(...)"));
                    out.push(TodoHit {
                        tag,
                        path: path.to_path_buf(),
                        line: (i + 1) as u32,
                        title,
                    });
                    matched = true;
                    break;
                }
            }
            if matched {
                continue;
            }
        }
        for &tag in MARKERS {
            if let Some(pos) = line.find(tag) {
                // Rough "is this in a comment?" heuristic: at least
                // one comment char (`//`, `#`, `/*`, `--`, `<!--`)
                // appears BEFORE the marker on the same line.
                let prefix = &line[..pos];
                let looks_like_comment = prefix.contains("//")
                    || prefix.contains('#')
                    || prefix.contains("/*")
                    || prefix.contains("--")
                    || prefix.contains("<!--");
                if !looks_like_comment {
                    continue;
                }
                // Confirm word-boundary on the right so `TODOLIST`
                // doesn't match `TODO`.
                let after = line[pos + tag.len()..].chars().next();
                if let Some(c) = after
                    && (c.is_alphanumeric() || c == '_')
                {
                    continue;
                }
                let title: String = line[pos + tag.len()..]
                    .trim_start_matches([':', '(', ')', ' '])
                    .trim()
                    .chars()
                    .take(120)
                    .collect();
                out.push(TodoHit {
                    tag,
                    path: path.to_path_buf(),
                    line: (i + 1) as u32,
                    title,
                });
                break; // one marker per line
            }
        }
    }
    out
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn playwright_scanner_picks_up_fixme_and_fail_and_skip() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("example.spec.ts");
        std::fs::write(
            &path,
            r#"import { test } from '@playwright/test';

test.fixme('renders survey card', async ({ page }) => {
  await page.goto('/');
});

test.fail('editor accepts nested lists', async ({ page }) => {
  await page.goto('/');
});

test.skip('legacy filter', async ({ page }) => { });
"#,
        )
        .unwrap();
        let hits = scan_file(&path);
        assert_eq!(hits.len(), 3);
        assert_eq!(hits[0].tag, "FIXME");
        assert_eq!(hits[0].title, "renders survey card");
        assert_eq!(hits[1].tag, "XXX");
        assert_eq!(hits[1].title, "editor accepts nested lists");
        assert_eq!(hits[2].tag, "REVIEW");
        assert_eq!(hits[2].title, "legacy filter");
    }

    #[test]
    fn playwright_scanner_ignores_regular_files() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("index.ts");
        std::fs::write(&path, "// TODO: hook this up\nconst x = 1;\n").unwrap();
        let hits = scan_file(&path);
        assert_eq!(hits.len(), 1);
        assert_eq!(hits[0].tag, "TODO");
    }

    #[test]
    fn extract_first_quoted_handles_single_and_double_quotes() {
        assert_eq!(
            extract_first_quoted(r#"("hello", async () => {})"#),
            Some("hello".to_string())
        );
        assert_eq!(
            extract_first_quoted(r#"('hi there', () => {})"#),
            Some("hi there".to_string())
        );
        assert_eq!(extract_first_quoted("(no quotes here)"), None);
    }
}