| | @@ -16,12 +16,13 @@ use topcoat::{ |
| 16 | 16 | context::Cx, |
| 17 | 17 | icon::{icon, iconify::iconify_icon}, |
| 18 | 18 | router::{error::not_found, page, path_param}, |
| 19 | | − view::{attributes, component, view}, |
| 19 | + view::{Unescaped, attributes, component, view}, |
| 20 | 20 | }; |
| 21 | 21 | |
| 22 | 22 | use crate::{ |
| 23 | 23 | application::{Blame, BlameContent, BlameFile, BlameGroup, blame::AGE_STEPS, blame_file}, |
| 24 | 24 | domain::{ObjectId, RefName, RepoPath}, |
| 25 | + infrastructure::highlight::{Source, SourceLine, source_lines}, |
| 25 | 26 | }; |
| 26 | 27 | |
| 27 | 28 | use super::{ |
| | @@ -205,12 +206,40 @@ async fn took_too_long(blob: &str) -> Result { |
| 205 | 206 | } |
| 206 | 207 | } |
| 207 | 208 | |
| 209 | +/// One rendered line of the table. |
| 210 | +struct Row<'a> { |
| 211 | + group: &'a BlameGroup, |
| 212 | + /// The first line of a run, which is the only one that names its commit. |
| 213 | + starts_run: bool, |
| 214 | + /// Whether this row opens a run with a hairline — every run but the file's first. |
| 215 | + ruled: bool, |
| 216 | + /// 1-based, and what the `#L{n}` link points at. |
| 217 | + number: usize, |
| 218 | + code: SourceLine, |
| 219 | +} |
| 220 | + |
| 221 | +/// Where a line of a run sits in the file, 0-based. |
| 222 | +/// |
| 223 | +/// One function because the reassembled text and the rendered rows must agree about it; |
| 224 | +/// two copies of `start_line + offset - 1` is exactly how they would stop agreeing. |
| 225 | +fn line_index(group: &BlameGroup, offset: usize) -> usize { |
| 226 | + (group.start_line + offset).saturating_sub(1) |
| 227 | +} |
| 228 | + |
| 208 | 229 | /// The blame table: commit, line number, code. |
| 209 | 230 | /// |
| 210 | 231 | /// A table, and one row per line with the same type and leading as the blob's, so |
| 211 | 232 | /// scrolling from one view to the other lands on the same lines in the same places. The |
| 212 | 233 | /// commit cell is deliberately **one line tall** — anything taller would make the two |
| 213 | 234 | /// views of a file disagree about where line 400 is. |
| 235 | +/// |
| 236 | +/// The code cell goes through [`source_lines`], the same adapter and therefore the same |
| 237 | +/// classes, caps and plain fallback as the blob's. It is called **once for the whole |
| 238 | +/// file, not once per run**: `highlight.rs` carries parse state across lines — a block |
| 239 | +/// comment opens on one line and closes on another — and a blamed run is an arbitrary |
| 240 | +/// slice of the file, so highlighting a run on its own would start mid-language. |
| 241 | +/// Highlighting only ever changes what is inside the cell, so the row height is the |
| 242 | +/// blob's either way. |
| 214 | 243 | #[component] |
| 215 | 244 | async fn blame_rows( |
| 216 | 245 | handle: &str, |
| | @@ -219,39 +248,89 @@ async fn blame_rows( |
| 219 | 248 | blob: &str, |
| 220 | 249 | blame: &Blame, |
| 221 | 250 | ) -> Result { |
| 251 | + // Reassembled by line number rather than by walking the groups in order, so the |
| 252 | + // colouring cannot drift from the numbering even if a run ever arrives out of |
| 253 | + // sequence. Joined with `\n` and never a trailing one, so the line count is exactly |
| 254 | + // `blame.line_count()` — the invariant `source_lines` checks before it trusts its |
| 255 | + // own output. |
| 256 | + let mut ordered = vec![""; blame.line_count()]; |
| 257 | + for group in &blame.groups { |
| 258 | + for (offset, line) in group.lines.iter().enumerate() { |
| 259 | + if let Some(slot) = ordered.get_mut(line_index(group, offset)) { |
| 260 | + *slot = line.as_str(); |
| 261 | + } |
| 262 | + } |
| 263 | + } |
| 264 | + |
| 265 | + let Source { lines, too_large } = |
| 266 | + source_lines(path.file_name().unwrap_or_default(), &ordered.join("\n")); |
| 267 | + |
| 268 | + // Flattened to one row per line before the view rather than nested `for`s inside |
| 269 | + // it: the code cell now owns highlighted markup, and a local the view only borrows |
| 270 | + // does not outlive it. |
| 271 | + let mut rows: Vec<Row> = Vec::new(); |
| 272 | + let mut code = lines.into_iter(); |
| 273 | + for (index, group) in blame.groups.iter().enumerate() { |
| 274 | + for offset in 0..group.lines.len() { |
| 275 | + rows.push(Row { |
| 276 | + group, |
| 277 | + // The commit is shown once per run, and every run but the first opens |
| 278 | + // with a hairline. Repeating the sha down forty adjacent rows is noise; |
| 279 | + // saying it once is the whole reason blame is grouped. |
| 280 | + starts_run: offset == 0, |
| 281 | + ruled: offset == 0 && index > 0, |
| 282 | + number: line_index(group, offset) + 1, |
| 283 | + // An empty cell has no height, and a blame row must keep the blob's. |
| 284 | + code: code |
| 285 | + .next() |
| 286 | + .unwrap_or_else(|| SourceLine::Plain(" ".to_owned())), |
| 287 | + }); |
| 288 | + } |
| 289 | + } |
| 290 | + |
| 222 | 291 | view! { |
| 292 | + if too_large { |
| 293 | + <p class="border-b border-border px-4 py-1.5 font-mono text-xs text-muted-foreground"> |
| 294 | + "Too large to highlight — shown as plain text." |
| 295 | + </p> |
| 296 | + } |
| 297 | + |
| 223 | 298 | <div class="overflow-x-auto"> |
| 224 | 299 | <table class="w-full border-collapse font-mono text-xs leading-relaxed"> |
| 225 | 300 | <tbody> |
| 226 | | − for (index, group) in blame.groups.iter().enumerate() { |
| 227 | | − for (offset, line) in group.lines.iter().enumerate() { |
| 228 | | − <tr class=(if offset == 0 && index > 0 { |
| 229 | | − "border-t border-border" |
| 230 | | − } else { |
| 231 | | − "" |
| 232 | | − })> |
| 233 | | − // The age tint rides on the leftmost cell of every row |
| 234 | | − // in the run, so consecutive rows draw one unbroken |
| 235 | | − // edge. See `styles.css`. |
| 236 | | − <td class=(format!( |
| 237 | | − "w-px border-r border-border pl-2 pr-3 align-top blame-age blame-age-{}", |
| 238 | | − group.age.min(AGE_STEPS - 1), |
| 239 | | − ))> |
| 240 | | − if offset == 0 { |
| 241 | | − commit_cell(handle: handle, name: name, path: path, group: group) |
| 242 | | − } |
| 243 | | − </td> |
| 244 | | − <td class="w-px select-none border-r border-border px-3 text-right align-top text-muted-foreground"> |
| 245 | | − <a |
| 246 | | − href=(format!("{blob}#L{}", group.start_line + offset)) |
| 247 | | − class="hover:text-foreground" |
| 248 | | − >((group.start_line + offset).to_string())</a> |
| 249 | | − </td> |
| 250 | | − <td class="whitespace-pre px-4 align-top"> |
| 251 | | − (if line.is_empty() { " " } else { line.as_str() }) |
| 252 | | − </td> |
| 253 | | − </tr> |
| 254 | | − } |
| 301 | + for row in rows { |
| 302 | + <tr class=(if row.ruled { "border-t border-border" } else { "" })> |
| 303 | + // The age tint rides on the leftmost cell of every row in |
| 304 | + // the run, so consecutive rows draw one unbroken edge. See |
| 305 | + // `styles.css`. |
| 306 | + <td class=(format!( |
| 307 | + "w-px border-r border-border pl-2 pr-3 align-top blame-age blame-age-{}", |
| 308 | + row.group.age.min(AGE_STEPS - 1), |
| 309 | + ))> |
| 310 | + if row.starts_run { |
| 311 | + commit_cell( |
| 312 | + handle: handle, |
| 313 | + name: name, |
| 314 | + path: path, |
| 315 | + group: row.group, |
| 316 | + ) |
| 317 | + } |
| 318 | + </td> |
| 319 | + <td class="w-px select-none border-r border-border px-3 text-right align-top text-muted-foreground"> |
| 320 | + <a |
| 321 | + href=(format!("{blob}#L{}", row.number)) |
| 322 | + class="hover:text-foreground" |
| 323 | + >((row.number).to_string())</a> |
| 324 | + </td> |
| 325 | + <td class="whitespace-pre px-4 align-top"> |
| 326 | + match row.code { |
| 327 | + // Markup this codebase wrote: `highlight` escapes |
| 328 | + // the source itself and emits nothing but spans. |
| 329 | + SourceLine::Classed(html) => (Unescaped::new_unchecked(html)), |
| 330 | + SourceLine::Plain(text) => (text), |
| 331 | + } |
| 332 | + </td> |
| 333 | + </tr> |
| 255 | 334 | } |
| 256 | 335 | </tbody> |
| 257 | 336 | </table> |