dv-review 0.77.2

A terminal diff viewer for reviewing GitHub PRs: Helix keys, rust-analyzer, inline review comments, and Claude Code reviews
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
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
//! GitHub review actions: comments, replies, suggestions, edits, deletes, reviews, resolving,
//! viewed marks, links, and the pending/threads pickers.
use super::*;

impl App {
    /// The pending review to add to, starting one on the PR head if there isn't one yet (with
    /// your review text so far, as GitHub can't add any later to one started without).
    pub(super) fn ensure_review(&mut self) -> Result<String> {
        if let Some(r) = &self.review {
            return Ok(r.clone());
        }
        let pr = self.pr.as_ref().context("no PR")?;
        let id = gh::start_review(&pr.node_id, &pr.head_oid, &self.summary_draft)?;
        self.review = Some(id.clone());
        Ok(id)
    }

    pub(super) fn run_job(&mut self, job: Job) {
        // What changes GitHub works on the PR as it is now, not as cached (not what only reads
        // it, nor git's own work).
        if !matches!(job, Job::Preview(_) | Job::Reactions(_) | Job::Labels | Job::Retarget(_) | Job::Rebase | Job::Pull | Job::Push(_)) {
            self.ensure_fresh();
        }
        match job {
            Job::Pull => {
                let res = self.pull();
                self.finish(res);
            }
            Job::Push(force) => {
                let res = self.push(force);
                if res.is_ok() {
                    self.github_changed();
                }
                self.finish(res); // the PR's head moved
            }
            // Reruns: GitHub sets them going in a moment, and CI doesn't change the PR's ETag:
            // dv looks again by itself, shortly.
            Job::Rerun => {
                let head = self.pr.as_ref().map(|p| p.head_oid.clone()).unwrap_or_default();
                self.msg = match gh::rerun_failed(&head) {
                    Ok(said) => {
                        // What a fetch under way brings back is from before (failing still);
                        // the watch fetches again by itself, shortly (through `watch_github`).
                        self.github_changed();
                        self.refresh_at = Some(std::time::Instant::now() + std::time::Duration::from_secs(10));
                        said
                    }
                    Err(e) => format!("{e:#}"),
                };
            }
            Job::Rebase => self.start_rebase(),
            Job::Retarget(base) => self.retarget(base),
            Job::Labels => self.labels_menu(),
            Job::Reactions(node) => self.react_menu(&node),
            Job::Preview(url) => match gh::issue_preview(&url) {
                Ok((title, md)) => (self.msg.clear(), self.popup = Some((title, self.hover_lines(&md), 0))).1,
                Err(e) => self.msg = format!("{e:#}"),
            },
            Job::Refresh => self.finish(Ok("refreshed")),
            Job::Resolve(t, on) => self.finish(gh::set_resolved(&t, on).map(|_| if on { "thread resolved" } else { "thread unresolved" })),
            Job::Discard(r) => {
                let res = gh::delete_review(&r);
                if res.is_ok() {
                    self.review = None;
                }
                self.finish(res.map(|_| "pending review discarded"));
            }
            Job::DeleteComment(c) => self.finish(gh::delete_comment(&c).map(|_| "comment deleted")),
            Job::SetField(field, name, on) => {
                let n = self.pr.as_ref().map_or(0, |p| p.number);
                let res = gh::set_field(n, field, &name, on);
                self.msg = match &res {
                    Err(e) => format!("{e:#}"),
                    Ok(()) => match (field, on) {
                        (gh::Field::Label, true) => format!("label {name} added"),
                        (gh::Field::Label, false) => format!("label {name} removed"),
                        (gh::Field::Assignee, true) => format!("@{name} assigned"),
                        (gh::Field::Assignee, false) => format!("@{name} unassigned"),
                        (gh::Field::Reviewer, true) => format!("review requested from @{name}"),
                        (gh::Field::Reviewer, false) => format!("review request to @{name} withdrawn"),
                    },
                };
                // The picker again, as you left it (what's on it as now); the PR's facts, at
                // GitHub's next look (a moment: no reload of the diff for it).
                if res.is_ok()
                    && let Some(f) = &mut self.fields
                {
                    let list = match field {
                        gh::Field::Label => &mut f.0,
                        gh::Field::Assignee => &mut f.1,
                        gh::Field::Reviewer => &mut f.2,
                    };
                    list.iter_mut().filter(|x| x.0 == name).for_each(|x| x.1 = on);
                    self.github_changed();
                    self.refresh_at = Some(std::time::Instant::now());
                }
                if let Some((query, sel)) = self.fields_at.take() {
                    self.fields_menu(query, sel);
                }
            }
            Job::React(node, what, add) => self.finish(gh::react(&node, what, add).map(|_| if add { "reacted" } else { "reaction taken back" })),
            Job::Post(p) => {
                let res = self.post(p);
                self.finish(res);
            }
            Job::Viewed(path, on) => {
                let pr_id = self.pr.as_ref().map(|p| p.node_id.clone()).unwrap_or_default();
                let res = gh::set_viewed(&pr_id, &path, on);
                if res.is_ok() && !on {
                    self.collapsed.remove(&path);
                }
                self.finish(res.map(|_| if on { "marked as viewed" } else { "unmarked as viewed" }));
                // To the file's header, or when marked, on to the next file (like on GitHub).
                if let Some(fi) = self.files.iter().position(|f| f.path == path)
                    && let Some(own) = self.rows.iter().position(|r| r.file == fi && r.line.kind == Kind::File)
                {
                    let later = self.rows[own + 1..].iter().position(|r| r.line.kind == Kind::File).map(|i| own + 1 + i);
                    self.select = false;
                    self.goto(if on { later.unwrap_or(own) } else { own }, 0);
                }
            }
        }
    }

    /// After a job that changed something: the diff reloaded (the working tree, or the PR's
    /// head, may have changed), and what came of it said.
    fn finish(&mut self, res: Result<impl Into<String>>) {
        self.reloaded(res, true);
    }

    /// The diff reloaded (`threads`: GitHub's too), and what came of `res` said: its error, or
    /// the reload's, or what it did.
    pub(super) fn reloaded(&mut self, res: Result<impl Into<String>>, threads: bool) {
        let reload = self.reload_all(threads);
        self.msg = match res {
            Ok(m) => with_reload(m.into(), reload),
            Err(e) => format!("{e:#}"),
        };
    }

    /// Queue `p` to send after the next frame, which shows what's being sent.
    pub(super) fn post_later(&mut self, p: Post) -> Result<String> {
        self.start(Job::Post(p));
        Ok(std::mem::take(&mut self.msg))
    }

    /// `job`, after the next frame, which says what it's doing.
    pub(super) fn start(&mut self, job: Job) {
        self.msg = job.doing();
        self.job = Some(job);
    }

    /// Send `p`; if that fails, its text is saved to a file so it isn't lost.
    pub(super) fn post(&mut self, p: Post) -> Result<&'static str> {
        let pr = self.pr.clone().context("no PR")?;
        match p {
            Post::Thread { anchor, review, body, claude } => {
                let res = match review {
                    true => self.ensure_review().and_then(|r| gh::add_thread(&r, &anchor, &body)),
                    false => gh::comment(&pr, &anchor, &body),
                };
                keep_on_error(res, &body)?;
                // It's on GitHub now; drop Claude's copy so it doesn't show twice.
                if let (Some(k), Some(p)) = (claude, &self.claude.path) {
                    crate::claude::dismiss(p, &k)?;
                }
                Ok(posted_msg(review))
            }
            Post::Reply { thread, comment, review, body } => {
                let res = match review {
                    true => self.ensure_review().and_then(|r| gh::add_reply(&r, &thread, &body)),
                    false => gh::reply(&pr, comment, &body),
                };
                keep_on_error(res, &body).map(|_| posted_msg(review))
            }
            Post::Converse(body) => {
                keep_on_error(gh::conversation_comment(&pr, &body), &body).map(|_| "comment posted to the conversation")
            }
            Post::Summary(body) => {
                // A new review starts with it; one started without text can't take any yet, so
                // it stays here (S submits it with the review).
                let fresh = self.review.is_none();
                let res = self.ensure_review().and_then(|r| if fresh { Ok(()) } else { gh::set_review_body(&r, &body) });
                match res {
                    Err(e) if format!("{e:#}").contains("missing body") => Ok(
                        "in your review summary till you quit (GitHub won't hold it yet): S submits it",
                    ),
                    res => keep_on_error(res, &body).map(|_| "in your review summary: S submits it (editing it first)"),
                }
            }
            Post::Edit { comment, body } => keep_on_error(gh::edit_comment(&comment, &body), &body).map(|_| "comment updated"),
            Post::Submit(event, body) => {
                // Closing comments; with nothing to say (and no pending comments) it only closes.
                let close = event == CLOSE;
                let event = if close { "COMMENT" } else { event };
                if !close || self.review.is_some() || !body.trim().is_empty() {
                    let res = match &self.review {
                        Some(r) => gh::submit_review(r, event, &body),
                        None => gh::review_now(&pr.node_id, &pr.head_oid, event, &body),
                    };
                    keep_on_error(res, &body)?;
                    self.review = None;
                }
                if !close {
                    return Ok("review submitted");
                }
                gh::close(pr.number, None).context("the review went in, but closing the PR failed")?;
                Ok("PR closed")
            }
        }
    }

    /// Viewed files fold, like on GitHub: on first load, and when newly marked; not with
    /// comments you haven't seen on them. Files you unfold yourself stay unfolded across
    /// refreshes.
    pub(super) fn apply_viewed(&mut self, viewed: HashMap<String, String>) {
        for (path, state) in &viewed {
            if state == "VIEWED" && self.viewed.get(path).is_none_or(|old| old != "VIEWED") && !self.new_on(path) {
                self.collapsed.insert(path.clone());
            }
        }
        self.viewed = viewed;
    }

    /// `Space v`: toggle GitHub's "Viewed" mark for the file under the cursor.
    pub(super) fn toggle_viewed(&mut self) {
        let Some(path) = self.file_path().map(str::to_owned) else {
            return self.msg = "not in a file".into();
        };
        if self.pr.is_none() {
            return self.msg = "no PR for this branch".into();
        }
        if self.files.iter().any(|f| f.path == path && f.meta == "untracked") {
            return self.msg = format!("{path} is untracked: not on GitHub, so it can't be marked viewed there");
        }
        let on = self.viewed.get(&path).is_none_or(|s| s != "VIEWED");
        self.start(Job::Viewed(path, on));
    }

    pub(super) fn pending_count(&self) -> usize {
        self.threads.iter().flat_map(|t| &t.comments).filter(|c| c.pending).count()
    }

    pub(super) fn review_menu(&mut self) {
        if self.pr.is_none() && self.replacement.is_some() {
            return self.replacement_menu();
        }
        if self.pr.is_none() {
            self.msg = "no PR for this branch".into();
            return;
        }
        let item = |label: &str, target| Item::new(label, target);
        let mut items = vec![
            item("Comment", Target::Do(Action::Submit("COMMENT"))),
            item("Approve", Target::Do(Action::Submit("APPROVE"))),
            item("Request changes", Target::Do(Action::Submit("REQUEST_CHANGES"))),
            item("Close the PR (with a comment, optional)", Target::Do(Action::Submit(CLOSE))),
        ];
        if self.review.is_some() {
            let n = self.pending_count();
            items.push(item(&format!("Discard pending review ({n} comment(s))"), Target::Discard));
        }
        // A replacement, published: closing what it replaces is still to do.
        if let Some(r) = &self.replacement {
            let open: Vec<String> = r.replaces.iter().filter(|o| !r.closed.contains(&o.number)).map(|o| format!("#{}", o.number)).collect();
            if !open.is_empty() {
                items.push(item(&format!("Close {} (what it replaces) with the closing note…", open.join(", ")), Target::Do(Action::CloseOriginals)));
            }
        }
        // What's still open, to see before you say you're done (approving most of all).
        items.extend(self.submit_notes().iter().map(|n| item(n, Target::Warn)));
        self.picker = Some(Picker::new(" submit review ", items));
    }

    /// What's still open in the review: threads waiting on someone, pending comments whose
    /// code has changed, and a checkout that isn't the PR's head.
    pub(super) fn submit_notes(&self) -> Vec<String> {
        let count = |f: &dyn Fn(&Thread) -> bool| self.threads.iter().filter(|t| f(t)).count();
        let open = count(&|t| t.claude.is_some() && !t.stale());
        let answered = count(&|t| t.answered());
        let github = count(&|t| t.claude.is_none() && !t.stale() && !t.comments.iter().all(|c| c.pending));
        let stale = count(&|t| t.outdated && t.comments.iter().any(|c| c.pending));
        let head = diff::git(&["rev-parse", "HEAD"]).unwrap_or_default();
        let behind = self.pr.as_ref().is_some_and(|p| !p.head_oid.is_empty() && p.head_oid != head.trim());
        [
            (open > 0, format!("{open} open comment(s) in Claude's review ({answered} answered, your turn)")),
            (github > 0, format!("{github} unresolved thread(s) on GitHub")),
            (stale > 0, format!("{stale} of your pending comments are on code that has changed since")),
            (behind, "your checkout isn't the PR's head: what you reviewed isn't what you'd approve".into()),
        ]
        .into_iter()
        .filter_map(|(on, s)| on.then_some(s))
        .collect()
    }

    /// `Space d`: delete the comment under the cursor, after a confirmation picker.
    pub(super) fn start_delete(&mut self) {
        if let Some((_, k)) = self.claude_at_cursor() {
            return self.edit_claude(&k, crate::claude::dismiss, "dismissed Claude's comment");
        }
        let Some(c) = self.rows[self.head.row].thread.and_then(|t| self.comment_under_cursor(t)) else {
            self.msg = "move onto a comment to delete it".into();
            return;
        };
        self.confirm_delete(c.clone());
    }

    /// The "delete this comment?" picker, "No" preselected.
    pub(super) fn confirm_delete(&mut self, c: gh::Comment) {
        if !c.can_delete {
            self.msg = format!("you can't delete @{}'s comment", c.author);
            return;
        }
        let preview = match self.seen_body(&c) {
            Some(body) => format!("\"{}\"", body.lines().next().unwrap_or("").chars().take(40).collect::<String>()),
            None => format!("({})", hidden_words(&c.hidden)),
        };
        let items = vec![
            Item::new("No, keep it", Target::Nothing),
            Item::new(format!("Yes, delete @{}'s comment: {preview}", c.author), Target::Run(Job::DeleteComment(c.node_id.clone()))),
        ];
        self.picker = Some(Picker::new(" delete comment? ", items));
    }

    pub(super) fn start_edit(&mut self) {
        if self.claude_at_cursor().is_some() {
            return self.msg = "c copies Claude's comment into your review, to edit there; r replies to Claude".into();
        }
        let Some(c) = self.rows[self.head.row].thread.and_then(|t| self.comment_under_cursor(t)) else {
            self.msg = "move onto a comment to edit it".into();
            return;
        };
        match c.can_edit {
            true if self.seen_body(c).is_none() => self.msg = HIDDEN_FIRST.into(),
            true => {
                let (comment, body, author) = (c.node_id.clone(), c.body.clone(), c.author.clone());
                self.action = Some(Action::Edit { comment, body, author });
            }
            false => self.msg = format!("you can't edit @{}'s comment", c.author),
        }
    }

    pub(super) fn toggle_resolved(&mut self) {
        if let Some((t, k)) = self.claude_at_cursor() {
            let th = &self.threads[t];
            // Your note for Claude: resolved or reopened.
            if !th.by_claude() {
                return match th.resolved {
                    true => self.edit_claude(&k, |p, k| crate::claude::set_resolved(p, k, false), "unresolved (your note for Claude)"),
                    false => self.edit_claude(&k, |p, k| crate::claude::set_resolved(p, k, true), "resolved (your note for Claude)"),
                };
            }
            // Claude's comment is what's kept (and posted): Space r resolves the talk under it,
            // not it (Space d deletes it). One Claude marked done (its change made): reopened.
            return match (th.resolved, th.talk_resolved, th.comments.len() > 1) {
                (true, ..) => self.edit_claude(&k, |p, k| crate::claude::set_resolved(p, k, false), "reopened (Claude's comment)"),
                (_, true, _) => self.edit_claude(&k, |p, k| crate::claude::set_talk_resolved(p, k, false), "the conversation under Claude's comment reopened"),
                (_, false, true) => self.edit_claude(&k, |p, k| crate::claude::set_talk_resolved(p, k, true), "the conversation under Claude's comment resolved (the comment stays)"),
                (_, false, false) => self.msg = "Claude's comment has no conversation to resolve (Space d deletes the comment)".into(),
            };
        }
        match self.rows[self.head.row].thread.map(|t| &self.threads[t]) {
            Some(t) => {
                let job = Job::Resolve(t.id.clone(), !t.resolved);
                self.start(job);
            }
            None => self.msg = "move onto a comment thread".into(),
        }
    }

    /// Claude's review file, replacing its threads (GitHub's stay).
    pub(super) fn load_claude(&mut self) {
        let had_new = self.new_paths();
        // (Its time before it's read: a write landing meanwhile is read next time round.)
        self.claude.mtime = self.claude.path.as_ref().and_then(|p| std::fs::metadata(p).and_then(|m| m.modified()).ok());
        let loaded = self.claude.path.as_ref().map(|p| crate::claude::load_with_ids(p));
        // Unreadable: what was read of it last stays, not "Claude hasn't reviewed this".
        if let Some(Err(e)) = &loaded {
            let why = format!("{e:#}");
            self.msg = format!("Claude's review file can't be read (shown as it last was): {why}");
            self.claude.broken = Some(why);
            // (Kept, over a diff that may have changed since: following their code.)
            if self.whole_pr() {
                crate::claude::reanchor(&mut self.threads, &self.files);
            }
            return;
        }
        self.claude.broken = None;
        // (`dv list`'s ranking: its word on this PR, or this replacement.)
        // (Its PR's item, then a replacement's own: closing what it replaces, say.)
        let keys: Vec<String> = self.pr.iter().map(|p| format!("pr-{}", p.number)).chain(self.replacement.iter().map(|r| format!("rep-{}", r.id))).collect();
        let ranked = crate::todo::load_of(crate::todo::Of::Prs);
        self.ranked_next = ranked.and_then(|r| keys.iter().find_map(|k| r.items.iter().find(|i| &i.0 == k && !i.1.trim().is_empty())).map(|i| (i.1.clone(), r.at)));
        self.threads.retain(|t| t.claude.is_none());
        self.claude.summary.clear();
        self.claude.head.clear();
        self.claude.description.clear();
        self.claude.closing.clear();
        if let Some(Ok(Some(mut r))) = loaded {
            // Comments with their code recorded follow it; others can only trust their lines.
            let head = diff::git(&["rev-parse", "HEAD"]).unwrap_or_default();
            let moved = !r.head.is_empty() && !head.trim().starts_with(&r.head) && !r.head.starts_with(head.trim());
            if moved && r.threads.iter().any(|t| t.hunk.is_empty()) {
                self.msg = format!(
                    "Claude reviewed {}; HEAD has moved since, so its line numbers may be off (`dv review-check` re-anchors)",
                    &r.head[..r.head.len().min(8)]
                );
            }
            // Not against HEAD: files without uncommitted changes aren't in that diff, and
            // their comments would look outdated (and could be dismissed as such).
            if self.whole_pr() {
                crate::claude::reanchor(&mut r.threads, &self.files);
            }
            (self.claude.summary, self.claude.head, self.claude.description, self.claude.closing) = (r.summary, r.head, r.description, r.closing);
            self.threads.extend(r.threads);
            // (Tab's shown talks: while resolved only, so one resolved again, by you or Claude,
            // folds.)
            let resolved: HashSet<u64> = (0..self.threads.len()).filter(|&t| self.threads[t].talk_resolved).filter_map(|t| self.root_id(t)).collect();
            self.talk_shown.retain(|id| resolved.contains(id));
            self.unfold_new(&had_new);
        }
    }

    /// Reload Claude's review when its file changes (Claude writing it, or answering replies).
    pub(super) fn watch_claude(&mut self) {
        let Some(p) = &self.claude.path else { return };
        let mtime = std::fs::metadata(p).and_then(|m| m.modified()).ok();
        if mtime != self.claude.mtime {
            let keep = self.cursor_key();
            self.msg.clear(); // a warning from loading takes its place
            self.load_claude();
            self.rebuild(keep);
            if self.msg.is_empty() {
                let n = self.threads.iter().filter(|t| t.by_claude()).count();
                self.msg = format!("Claude's review updated: {n} comment(s)");
            }
        }
    }

    /// Run a change to Claude's review file on comment `i`, then show the result.
    pub(super) fn edit_claude(&mut self, key: &str, f: fn(&std::path::Path, &str) -> Result<()>, done: &str) {
        let Some(p) = self.claude.path.clone() else { return };
        let (keep, res) = (self.cursor_key(), f(&p, key));
        self.load_claude();
        self.rebuild(keep);
        self.msg = match res {
            Ok(()) => done.into(),
            Err(e) => format!("{e:#}"),
        };
    }

    /// The thread under the cursor and its id in Claude's review file, if it's from there.
    pub(super) fn claude_at_cursor(&self) -> Option<(usize, String)> {
        let t = self.rows[self.head.row].thread?;
        Some((t, self.threads[t].claude.clone()?))
    }

    /// `c`/`C` on Claude's comment: copy it (edited in `$EDITOR`) into your pending review, or
    /// post it now. Claude's copy is dismissed once it's on GitHub.
    pub(super) fn start_promote(&mut self, t: usize, key: String, review: bool) {
        let th = &self.threads[t];
        if !th.by_claude() {
            return self.msg = "this is your comment for Claude (private): r replies, Space d removes it".into();
        }
        if th.outdated {
            return self.msg = "this comment is outdated (its code changed): ask Claude to update it, or Space d".into();
        }
        if !th.conversation.is_empty() {
            return self.msg = "Claude's answer here is for you, on no lines: C on the comment above quotes it in a new conversation comment".into();
        }
        let anchor = Anchor { path: th.path.clone(), line: th.line.unwrap_or(0), left: th.left, start: th.start_line.map(|s| (s, th.left)) };
        // Claude's comment, wherever in its thread the cursor is: the talk under it is about it,
        // not what's posted.
        let mut body = th.comments.first().map(|c| c.body.clone()).unwrap_or_default();
        // On GitHub the severity leads the text, as reviewers write it: "**nit:** …".
        if !th.severity.is_empty() && !body.starts_with("**") {
            body = format!("**{}:** {body}", th.severity);
        }
        match self.check_synced().and_then(|_| self.on_github(anchor)) {
            Ok(anchor) => self.action = Some(Action::Comment { anchor, review, prefill: body, claude: Some(key) }),
            Err(e) => self.msg = format!("{e:#}"),
        }
    }

    /// The GitHub comment (or review) under the cursor, to react to: its node id. A review
    /// thread's comment, or one in the PR's conversation (its heading or text).
    pub(super) fn reactable_at_cursor(&self) -> Option<String> {
        let row = &self.rows[self.head.row];
        if let Some(t) = row.thread {
            let c = self.comment_under_cursor(t).filter(|c| self.threads[t].claude.is_none() && !c.pending)?;
            return Some(c.node_id.clone()).filter(|n| !n.is_empty());
        }
        self.conversation_node()
    }

    /// The PR conversation's comment (or review) under the cursor, or the description (the PR
    /// itself): its node id.
    pub(super) fn conversation_node(&self) -> Option<String> {
        let row = &self.rows[self.head.row];
        let i = row.info.filter(|_| row.file == INFO && row.thread.is_none() && matches!(row.main, None | Some(DESCRIPTION)))?;
        let head = match self.info.get(i)? {
            l if l.heading => l,
            l if l.md => self.info.get(i.checked_sub(1)?).filter(|h| h.heading)?,
            _ => return None,
        };
        Some(head.node.clone()).filter(|n| !n.is_empty())
    }

    /// `Space =`: react to the comment under the cursor, or take your reaction back: what's
    /// on it now is asked of GitHub first (a job, so a frame says so), so yours show.
    pub(super) fn start_react(&mut self) {
        match self.reactable_at_cursor() {
            Some(node) => self.start(Job::Reactions(node)),
            None => self.msg = "move onto a GitHub comment (or the description) to react to it".into(),
        }
    }

    /// `Space l`: the PR's labels, assignees and requested reviewers, and what it could have
    /// (✓: on it); Enter adds or removes one. Type to find one (`review`, `@` for people).
    pub(super) fn labels_menu(&mut self) {
        self.msg.clear();
        let Some(n) = self.pr.as_ref().map(|p| p.number) else { return };
        match gh::pr_fields(n) {
            Ok(x) => self.fields = Some(x),
            Err(e) => return self.msg = format!("{e:#}"),
        };
        self.fields_menu(String::new(), 0);
    }

    /// The labels picker, of what GitHub said (`fields`, kept up as you change it): what was
    /// typed, and where you were, as given. It stays as you pick (one after another).
    pub(super) fn fields_menu(&mut self, query: String, sel: usize) {
        let Some((labels, people, reviewers)) = &self.fields else { return };
        // (Under their sections: Reviewers, Labels, Assignees.)
        let item = |field: gh::Field, name: &str, on: bool| {
            let at = if matches!(field, gh::Field::Label) { "" } else { "@" };
            let tick = if on { "✓ " } else { "  " };
            Item::new(format!("{tick}{at}{name}"), Target::Run(Job::SetField(field, name.to_owned(), !on)))
        };
        let items: Vec<Item> = [(gh::Field::Reviewer, &reviewers), (gh::Field::Label, &labels), (gh::Field::Assignee, &people)]
            .into_iter()
            .flat_map(|(f, xs)| xs.iter().map(move |(x, on)| item(f, x, *on)))
            .collect();
        if items.is_empty() {
            return self.msg = "no labels in this repo, nor anyone to assign or ask".into();
        }
        let p = Picker::new(" reviewers, labels, assignees (✓ on it: Enter adds or takes it off) ", items);
        self.picker = Some(Picker { query, sel, ..p });
    }

    pub(super) fn react_menu(&mut self, node: &str) {
        self.msg.clear();
        let now = match gh::reactions_of(node) {
            Ok(r) => r,
            Err(e) => return self.msg = format!("{e:#}"),
        };
        let items = gh::REACTIONS
            .iter()
            .map(|&(what, emoji)| {
                let (n, mine) = now.iter().find(|r| r.0 == what).map_or((0, false), |r| (r.1, r.2));
                let count = if n > 0 { format!(" {n}") } else { String::new() };
                let label = match mine {
                    true => format!("{emoji}{count}  ✓ yours: Enter takes it back"),
                    false => format!("{emoji}{count}"),
                };
                Item::new(label, Target::Run(Job::React(node.to_owned(), what, !mine)))
            })
            .collect();
        self.picker = Some(Picker::new(" react ", items));
    }

    /// GitHub anchor for the selected code lines (comment rows inside the selection are ignored).
    pub(super) fn selection_anchor(&self) -> Result<Anchor> {
        let (s, e) = self.ordered();
        let code: Vec<usize> = (s.row..=e.row)
            .filter(|&r| self.rows[r].line.kind.is_code())
            .collect();
        let (Some(&a), Some(&b)) = (code.first(), code.last()) else { bail!("select code lines to comment on") };
        if self.rows[a].file != self.rows[b].file {
            bail!("a comment can't span files");
        }
        let side = |r: usize| {
            let l = &self.rows[r].line;
            if l.kind == Kind::Del { (l.old, true) } else { (l.new, false) }
        };
        let ((Some(line), left), (Some(sl), sleft)) = (side(b), side(a)) else { bail!("no line number") };
        Ok(Anchor {
            path: self.files[self.rows[b].file].path.clone(),
            line,
            left,
            start: (a != b).then_some((sl, sleft)),
        })
    }

    /// `c`/`C`: reply when on a thread, else comment on the selected lines. In the PR section
    /// `C` quotes into a new conversation comment; `c` has nothing to queue there (yet).
    pub(super) fn comment_or_reply(&mut self, review: bool) {
        // GitHub allows one pending review per PR and refuses single line comments beside it.
        let row = &self.rows[self.head.row];
        if row.info == Some(SUMMARY) {
            return self.promote_summary(review);
        }
        if review && row.info == Some(DRAFT) {
            return self.msg = "this is your pending review's text: S submits it, editing it first".into();
        }
        // PR text (not a thread there: those are on files no longer in the diff).
        let pr_text = row.file == INFO && row.thread.is_none();
        if !review && self.review.is_some() && !pr_text {
            let n = self.pending_count();
            return self.msg = format!("you have a pending review ({n}): c adds to it, S submits it, then C posts");
        }
        if let Some((t, k)) = self.claude_at_cursor() {
            return self.start_promote(t, k, review);
        }
        match (pr_text, self.rows[self.head.row].thread) {
            // (Not quoting what's hidden on GitHub, unseen.)
            (true, _) if review => self.quote_into_summary(),
            (true, _) => self.start_conversation_comment(),
            // `r` replies (in the review); `C` here replies right away, as `C` posts what `c` queues.
            (false, Some(_)) if review => self.msg = "r replies to this thread (c comments on selected code lines)".into(),
            (false, Some(_)) => self.start_reply(false),
            (false, None) => self.start_comment(review),
        }
    }

    /// Line comments are posted against the PR head; local edits would shift the lines.
    pub(super) fn check_synced(&self) -> Result<()> {
        let pr = self.pr.as_ref().context("no PR for this branch")?;
        if self.before_head() {
            bail!("comments go on the PR's head: show commits up to the last one (or all) to comment");
        }
        if diff::git(&["rev-parse", "HEAD"])?.trim() != pr.head_oid {
            bail!("HEAD isn't the PR head (pull, or `gh pr checkout {}`)", pr.number);
        }
        if !diff::git(&["status", "--porcelain", "--untracked-files=no"])?.trim().is_empty() {
            bail!("working tree has local changes; stash them to comment");
        }
        Ok(())
    }

    /// GitHub only takes comments on files the PR has: not on one that's untracked here.
    pub(super) fn on_github(&self, a: Anchor) -> Result<Anchor> {
        match self.files.iter().any(|f| f.path == a.path && f.meta == "untracked") {
            true => bail!("{} is untracked: GitHub hasn't got it (commit and push it: Space g), or `a` asks Claude", a.path),
            false => Ok(a),
        }
    }

    pub(super) fn start_comment(&mut self, review: bool) {
        match self.check_synced().and_then(|_| self.selection_anchor()).and_then(|a| self.on_github(a)) {
            Ok(anchor) => self.action = Some(Action::Comment { anchor, review, prefill: String::new(), claude: None }),
            Err(e) => self.msg = format!("{e:#}"),
        }
    }

    /// `s`: a GitHub suggestion for the selected new-side lines, pre-filled with their code.
    pub(super) fn start_suggest(&mut self) {
        match self.check_synced().and_then(|_| self.suggestion()).and_then(|(a, p)| Ok((self.on_github(a)?, p))) {
            Ok((anchor, prefill)) => self.action = Some(Action::Comment { anchor, review: true, prefill, claude: None }),
            Err(e) => self.msg = format!("{e:#}"),
        }
    }

    pub(super) fn suggestion(&self) -> Result<(Anchor, String)> {
        let (s, e) = self.ordered();
        let rows: Vec<&Row> =
            self.rows[s.row..=e.row].iter().filter(|r| r.line.kind.is_code()).collect();
        let (Some(first), Some(last)) = (rows.first(), rows.last()) else { bail!("select the lines to suggest a change for") };
        if rows.iter().any(|r| r.line.kind == Kind::Del) {
            bail!("suggestions replace lines in the new version; leave removed lines out of the selection");
        }
        if first.file != last.file {
            bail!("a suggestion can't span files");
        }
        let (Some(a), Some(b)) = (first.line.new, last.line.new) else { bail!("no line number") };
        let code: Vec<&str> = rows.iter().map(|r| r.line.text.as_str()).collect();
        let code = code.join("\n");
        // A fence longer than any backtick run in the code, so the block can't end early.
        let mut fence = String::from("```");
        while code.contains(&fence) {
            fence.push('`');
        }
        let anchor =
            Anchor { path: self.files[first.file].path.clone(), line: b, left: false, start: (a != b).then_some((a, false)) };
        Ok((anchor, format!("\n{fence}suggestion\n{code}\n{fence}\n")))
    }

    /// `C` in the PR section: a new PR conversation comment quoting the comment (or description)
    /// under the cursor, like GitHub's "Quote reply"; on other PR info lines, an empty one.
    pub(super) fn start_conversation_comment(&mut self) {
        if self.pr.is_none() {
            return self.msg = "no PR for this branch".into();
        }
        let row = &self.rows[self.head.row];
        let i = row.info.filter(|&i| i < self.info.len());
        // (Hidden on GitHub, unseen: not quoted, and the editor says why.)
        let quote = match i.map(|i| self.seen_info(i)) {
            Some(None) => format!("{HINT} not quoted: it's hidden on GitHub (Tab on it shows it) -->\n"),
            i => i.flatten().map_or(String::new(), |l| quote(&l.text)),
        };
        self.action = Some(Action::Converse { quote, as_is: false });
    }

    /// `c` in the PR section: the comment (or description) under the cursor, quoted at the end
    /// of your pending review's text.
    pub(super) fn quote_into_summary(&mut self) {
        let i = self.rows[self.head.row].info.filter(|&i| i < self.info.len());
        match i.map(|i| self.seen_info(i)) {
            Some(None) => self.msg = HIDDEN_FIRST.into(),
            Some(Some(l)) if l.md && !l.text.trim().is_empty() && self.pr.is_some() => {
                let q = quote(&l.text);
                self.add_to_summary(q.trim_end());
            }
            _ => self.msg = "c here: on a comment or the description, quotes it in your review summary".into(),
        }
    }

    /// Appends `text` to your pending review's text, on GitHub (starting the review if need be).
    pub(super) fn add_to_summary(&mut self, text: &str) {
        // (Submitting goes with what's in the editor: added here, it'd be lost.)
        if matches!(self.writing, Some((Action::Submit(_), _))) {
            return self.msg = "you're writing what goes with your review in the editor's pane: add it there".into();
        }
        let draft = self.summary_draft.trim_end();
        self.summary_draft = if draft.is_empty() { text.to_owned() } else { format!("{draft}\n\n{text}") };
        self.msg = self.post_later(Post::Summary(self.summary_draft.clone())).unwrap_or_else(|e| format!("{e:#}"));
    }

    /// `c` / `C` on Claude's review (its summary), as on its comments: yours to use, not to
    /// quote. `c` puts it in your review summary (`S` starts from it); `C` posts it as your PR
    /// comment, after the editor.
    pub(super) fn promote_summary(&mut self, review: bool) {
        if self.pr.is_none() {
            return self.msg = "no PR for this branch".into();
        }
        let text = self.claude.summary.trim().to_owned();
        match review {
            true => self.add_to_summary(&text),
            false => self.action = Some(Action::Converse { quote: format!("{text}\n"), as_is: true }),
        }
    }

    pub(super) fn start_reply(&mut self, review: bool) {
        // On Claude's review summary: go on in the thread replying to it, or start one.
        if self.rows[self.head.row].info == Some(SUMMARY) && self.rows[self.head.row].thread.is_none() {
            if self.claude.path.is_none() {
                return self.msg = "no review file here (not in a git repo?)".into();
            }
            return self.action = Some(match self.threads.iter().find(|t| t.conversation == ON_SUMMARY).and_then(|t| t.claude.clone()) {
                Some(k) => Action::ClaudeReply(k, "", String::new()),
                None => Action::AskConversation { url: ON_SUMMARY.into(), prefill: String::new(), want: "" },
            });
        }
        // Several of Claude's threads selected: one reply to them all.
        if let Some(keys) = self.selected_claude_threads() {
            self.select = false;
            return self.pick_want(Action::ClaudeReplyAll(keys, ""), None);
        }
        // On lines, say what you want of Claude; under a conversation comment it's a question.
        if let Some((t, k)) = self.claude_at_cursor() {
            return match self.threads[t].conversation.is_empty() {
                true => self.pick_want(Action::ClaudeReply(k, "", String::new()), Some(t)),
                false => self.action = Some(Action::ClaudeReply(k, "", String::new())),
            };
        }
        let t = self.rows[self.head.row].thread;
        match (t, t.and_then(|t| self.root_id(t))) {
            (Some(t), Some(comment)) if self.pr.is_some() => {
                let thread = self.threads[t].id.clone();
                let quote = self.comment_under_cursor(t).and_then(|c| self.seen_body(c)).map_or(String::new(), quote);
                self.action = Some(Action::Reply { thread, comment, review, quote });
            }
            _ => self.msg = "move onto a comment thread to reply".into(),
        }
    }

}

/// What `c`, `Space e` and the like say on a comment hidden on GitHub, not shown here.
const HIDDEN_FIRST: &str = "it's hidden on GitHub: Tab on it shows it first";

impl App {
}

/// `S` → "Close the PR": a comment review (if there's anything to say), then closing.
pub(super) const CLOSE: &str = "CLOSE";