brain: Hunk splitting
ea8ec59cf78efe3466a616f6869459a306088116
1 parent
21cddbe1
crates/brain/src/session/commit.rs
+29 -0
| 1 | 1 | //! Per-commit review state for the session. |
|
| 2 | 2 | ||
| 3 | 3 | use std::collections::{BTreeMap, BTreeSet}; |
|
| 4 | + | use std::mem; |
|
| 4 | 5 | ||
| 5 | 6 | use rustik_diff::{DiffDocument, DiffOptions, DiffViewer, Encode as _, FileDiff, FileId, HunkId}; |
|
| 6 | 7 | use rustik_repo::PatchId; |
|
| 7 | 8 | ||
| 8 | 9 | use crate::backend::{Brain, Commit}; |
| 59 | 60 | document: DiffDocument, |
|
| 60 | 61 | options: DiffOptions, |
|
| 61 | 62 | brain: &Brain, |
|
| 62 | 63 | store: &ReviewComments, |
|
| 63 | 64 | ) -> Result<Self, Error> { |
|
| 65 | + | let document = Self::split_review_hunks(document); |
|
| 64 | 66 | let viewer = DiffViewer::new(document, options); |
|
| 65 | 67 | let files = viewer.document().files.clone(); |
|
| 66 | 68 | let heading = format!("{} {}", commit.id.short(), commit.summary); |
|
| 67 | 69 | let mut hunk_patches = BTreeMap::new(); |
|
| 68 | 70 | let mut patch_ids = BTreeMap::new(); |
| 103 | 105 | loaded.refresh_decorations(); |
|
| 104 | 106 | ||
| 105 | 107 | Ok(loaded) |
|
| 106 | 108 | } |
|
| 107 | 109 | ||
| 110 | + | /// Splits multi-change hunks into independently reviewable hunks. |
|
| 111 | + | /// |
|
| 112 | + | /// The split runs before patch ids, comments, and viewer state are built, |
|
| 113 | + | /// so split pieces have stable hunk identities across reloads. |
|
| 114 | + | fn split_review_hunks(mut document: DiffDocument) -> DiffDocument { |
|
| 115 | + | let mut next = 0; |
|
| 116 | + | for file in &mut document.files { |
|
| 117 | + | let mut hunks = Vec::with_capacity(file.hunks.len()); |
|
| 118 | + | for hunk in mem::take(&mut file.hunks) { |
|
| 119 | + | if let Some(split) = hunk.split() { |
|
| 120 | + | for mut hunk in split { |
|
| 121 | + | hunk.id = HunkId::new(next); |
|
| 122 | + | next += 1; |
|
| 123 | + | hunks.push(hunk); |
|
| 124 | + | } |
|
| 125 | + | } else { |
|
| 126 | + | let mut hunk = hunk; |
|
| 127 | + | hunk.id = HunkId::new(next); |
|
| 128 | + | next += 1; |
|
| 129 | + | hunks.push(hunk); |
|
| 130 | + | } |
|
| 131 | + | } |
|
| 132 | + | file.hunks = hunks; |
|
| 133 | + | } |
|
| 134 | + | document |
|
| 135 | + | } |
|
| 136 | + | ||
| 108 | 137 | /// Returns whether every original hunk of `file` has been accepted. |
|
| 109 | 138 | fn accepts_file(&self, file: &FileDiff) -> bool { |
|
| 110 | 139 | self.review |
|
| 111 | 140 | .accepts_all(file.hunks.iter().map(|hunk| hunk.id)) |
|
| 112 | 141 | } |
a.rs
+0 -0
b.rs
+0 -0
a.rs
+0 -0