forge
Clone
https://git.tahinli.com/Tahinli/forge.git
0 watching·0 forks·Rust·AGPL-3.0·9.3 MiB·0d793e7·Tahinli·6 hours ago·359 commits
| 1 | use std::collections::HashMap; |
| 2 | |
| 3 | use forge_core::{Side, Thread}; |
| 4 | use forge_git::history::{CommitDiff, DiffLimits, DiffStatus, FileDiff, LineKind}; |
| 5 | use topcoat::Result; |
| 6 | use topcoat::context::{Cx, context}; |
| 7 | use topcoat::cookie::{Cookie, Cookies, SameSite, cookies, time}; |
| 8 | use topcoat::router::request::uri; |
| 9 | use topcoat::view::{View, component, view}; |
| 10 | |
| 11 | use crate::auth::current_user; |
| 12 | use crate::i18n::Lang; |
| 13 | use crate::i18n::browse as b; |
| 14 | use crate::i18n::code::{self as g, K}; |
| 15 | use crate::i18n::pulls_review::{self as pr_g, K as PrK}; |
| 16 | use crate::state::AppState; |
| 17 | use crate::views::enc; |
| 18 | use crate::views::review_thread::{ThreadCtx, review_thread}; |
| 19 | |
| 20 | pub const DIFF_VIEW: &str = "diff_view"; |
| 21 | |
| 22 | pub const LIMITS: DiffLimits = DiffLimits { |
| 23 | max_files: MAX_FILES, |
| 24 | max_lines_per_file: 2000, |
| 25 | max_total_lines: 20_000, |
| 26 | }; |
| 27 | |
| 28 | pub const MAX_FILES: usize = 300; |
| 29 | |
| 30 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 31 | pub struct DiffLang(pub Lang); |
| 32 | |
| 33 | impl Default for DiffLang { |
| 34 | fn default() -> Self { |
| 35 | DiffLang(Lang::En) |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | #[derive(Default)] |
| 40 | pub struct LineComments { |
| 41 | pub anchors: bool, |
| 42 | pub threads: HashMap<(usize, Side, u32), Vec<Thread>>, |
| 43 | pub open_form: Option<(usize, Side, u32)>, |
| 44 | pub files_href: String, |
| 45 | pub has_pending: bool, |
| 46 | pub ctx: ThreadCtx, |
| 47 | } |
| 48 | |
| 49 | pub const DIFF_VIEW_COOKIE: &str = "forge_diff"; |
| 50 | |
| 51 | #[derive(Clone, Default)] |
| 52 | pub struct DiffOpts { |
| 53 | pub enabled: bool, |
| 54 | pub split: bool, |
| 55 | pub ignore_ws: bool, |
| 56 | pub path: String, |
| 57 | pub query: Vec<(String, String)>, |
| 58 | pub downloads: Option<(String, String)>, |
| 59 | pub only: Option<usize>, |
| 60 | } |
| 61 | |
| 62 | impl DiffOpts { |
| 63 | pub async fn from_cx(cx: &Cx, downloads: Option<(String, String)>) -> Result<Self> { |
| 64 | let request = uri(cx); |
| 65 | let mut view_param = None; |
| 66 | let mut ignore_ws = false; |
| 67 | let mut query = Vec::new(); |
| 68 | if let Some(raw) = request.query() { |
| 69 | for (key, value) in form_urlencoded::parse(raw.as_bytes()) { |
| 70 | match key.as_ref() { |
| 71 | "view" => view_param = Some(value.into_owned()), |
| 72 | "w" => ignore_ws = value == "1", |
| 73 | "comment" | "review_error" => {} |
| 74 | _ => query.push((key.into_owned(), value.into_owned())), |
| 75 | } |
| 76 | } |
| 77 | } |
| 78 | let split = match current_user(cx).await? { |
| 79 | Some((user, _)) => { |
| 80 | let state = context::<AppState>(cx); |
| 81 | let stored = state.store.user_prefs(&user.id).await?.remove(DIFF_VIEW); |
| 82 | match view_param.as_deref() { |
| 83 | Some(v @ ("split" | "unified")) => { |
| 84 | if stored.as_deref() != Some(v) { |
| 85 | state.store.set_user_pref(&user.id, DIFF_VIEW, v).await?; |
| 86 | } |
| 87 | v == "split" |
| 88 | } |
| 89 | _ => stored.as_deref() == Some("split"), |
| 90 | } |
| 91 | } |
| 92 | None => match view_param.as_deref() { |
| 93 | Some(v @ ("split" | "unified")) => { |
| 94 | let max_age = time::Duration::seconds(365 * 24 * 60 * 60); |
| 95 | cookies(cx).add( |
| 96 | Cookie::build((DIFF_VIEW_COOKIE, v.to_owned())) |
| 97 | .path("/") |
| 98 | .secure(true) |
| 99 | .same_site(SameSite::Lax) |
| 100 | .max_age(max_age) |
| 101 | .build(), |
| 102 | ); |
| 103 | v == "split" |
| 104 | } |
| 105 | _ => cookies(cx) |
| 106 | .get(DIFF_VIEW_COOKIE) |
| 107 | .is_some_and(|c| c.value() == "split"), |
| 108 | }, |
| 109 | }; |
| 110 | Ok(Self { |
| 111 | enabled: true, |
| 112 | split, |
| 113 | ignore_ws, |
| 114 | path: request.path().to_owned(), |
| 115 | query, |
| 116 | downloads, |
| 117 | only: None, |
| 118 | }) |
| 119 | } |
| 120 | |
| 121 | pub fn href(&self, split: bool, ignore_ws: bool) -> String { |
| 122 | let mut ser = form_urlencoded::Serializer::new(String::new()); |
| 123 | for (k, v) in &self.query { |
| 124 | ser.append_pair(k, v); |
| 125 | } |
| 126 | ser.append_pair("view", if split { "split" } else { "unified" }); |
| 127 | if ignore_ws { |
| 128 | ser.append_pair("w", "1"); |
| 129 | } |
| 130 | format!("{}?{}", self.path, ser.finish()) |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | struct Toolbar { |
| 135 | unified_href: String, |
| 136 | split_href: String, |
| 137 | ws_href: String, |
| 138 | split: bool, |
| 139 | ignore_ws: bool, |
| 140 | downloads: Option<(String, String)>, |
| 141 | } |
| 142 | |
| 143 | struct InlineForm { |
| 144 | action: String, |
| 145 | cancel_href: String, |
| 146 | csrf: String, |
| 147 | file_index: String, |
| 148 | side: &'static str, |
| 149 | line: String, |
| 150 | head_oid: String, |
| 151 | has_pending: bool, |
| 152 | can_review: bool, |
| 153 | suggest_line: Option<String>, |
| 154 | } |
| 155 | |
| 156 | struct RowBelow { |
| 157 | threads: Vec<Thread>, |
| 158 | form: Option<InlineForm>, |
| 159 | ctx: ThreadCtx, |
| 160 | } |
| 161 | |
| 162 | struct DiffRow { |
| 163 | class: &'static str, |
| 164 | old: String, |
| 165 | new: String, |
| 166 | code: String, |
| 167 | anchored: bool, |
| 168 | anchor: Option<String>, |
| 169 | plus_href: Option<String>, |
| 170 | below: Option<RowBelow>, |
| 171 | } |
| 172 | |
| 173 | struct FileView { |
| 174 | anchor: String, |
| 175 | label: String, |
| 176 | status: &'static str, |
| 177 | additions: u32, |
| 178 | deletions: u32, |
| 179 | binary: bool, |
| 180 | truncated: bool, |
| 181 | has_blob: bool, |
| 182 | blob_href: String, |
| 183 | rows: Vec<DiffRow>, |
| 184 | split_rows: Vec<SplitRow>, |
| 185 | } |
| 186 | |
| 187 | struct SplitSide { |
| 188 | class: &'static str, |
| 189 | num: String, |
| 190 | code: String, |
| 191 | anchor: Option<String>, |
| 192 | plus_href: Option<String>, |
| 193 | } |
| 194 | |
| 195 | impl SplitSide { |
| 196 | fn empty() -> Self { |
| 197 | Self { |
| 198 | class: "empty", |
| 199 | num: String::new(), |
| 200 | code: String::new(), |
| 201 | anchor: None, |
| 202 | plus_href: None, |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | fn from_row(row: DiffRow, class: &'static str, num: String) -> (Self, Option<RowBelow>) { |
| 207 | ( |
| 208 | Self { |
| 209 | class, |
| 210 | num, |
| 211 | code: row.code, |
| 212 | anchor: row.anchor, |
| 213 | plus_href: row.plus_href, |
| 214 | }, |
| 215 | row.below, |
| 216 | ) |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | struct SplitRow { |
| 221 | hunk: Option<String>, |
| 222 | left: SplitSide, |
| 223 | right: SplitSide, |
| 224 | below: Option<RowBelow>, |
| 225 | } |
| 226 | |
| 227 | fn merge_below(a: Option<RowBelow>, b: Option<RowBelow>) -> Option<RowBelow> { |
| 228 | match (a, b) { |
| 229 | (Some(mut a), Some(b)) => { |
| 230 | a.threads.extend(b.threads); |
| 231 | if a.form.is_none() { |
| 232 | a.form = b.form; |
| 233 | } |
| 234 | Some(a) |
| 235 | } |
| 236 | (a, None) => a, |
| 237 | (None, b) => b, |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | fn flush_pairs(out: &mut Vec<SplitRow>, dels: &mut Vec<DiffRow>, adds: &mut Vec<DiffRow>) { |
| 242 | let mut dels = std::mem::take(dels).into_iter(); |
| 243 | let mut adds = std::mem::take(adds).into_iter(); |
| 244 | loop { |
| 245 | let (d, a) = (dels.next(), adds.next()); |
| 246 | if d.is_none() && a.is_none() { |
| 247 | break; |
| 248 | } |
| 249 | let (left, left_below) = match d { |
| 250 | Some(row) => { |
| 251 | let num = row.old.clone(); |
| 252 | SplitSide::from_row(row, "del", num) |
| 253 | } |
| 254 | None => (SplitSide::empty(), None), |
| 255 | }; |
| 256 | let (right, right_below) = match a { |
| 257 | Some(row) => { |
| 258 | let num = row.new.clone(); |
| 259 | SplitSide::from_row(row, "add", num) |
| 260 | } |
| 261 | None => (SplitSide::empty(), None), |
| 262 | }; |
| 263 | out.push(SplitRow { |
| 264 | hunk: None, |
| 265 | left, |
| 266 | right, |
| 267 | below: merge_below(left_below, right_below), |
| 268 | }); |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | fn split_rows(rows: Vec<DiffRow>) -> Vec<SplitRow> { |
| 273 | let mut out = Vec::new(); |
| 274 | let mut dels: Vec<DiffRow> = Vec::new(); |
| 275 | let mut adds: Vec<DiffRow> = Vec::new(); |
| 276 | for row in rows { |
| 277 | match row.class { |
| 278 | "diff-line del" => { |
| 279 | if !adds.is_empty() { |
| 280 | flush_pairs(&mut out, &mut dels, &mut adds); |
| 281 | } |
| 282 | dels.push(row); |
| 283 | } |
| 284 | "diff-line add" => adds.push(row), |
| 285 | "diff-line hunk" => { |
| 286 | flush_pairs(&mut out, &mut dels, &mut adds); |
| 287 | out.push(SplitRow { |
| 288 | hunk: Some(row.code), |
| 289 | left: SplitSide::empty(), |
| 290 | right: SplitSide::empty(), |
| 291 | below: None, |
| 292 | }); |
| 293 | } |
| 294 | _ => { |
| 295 | flush_pairs(&mut out, &mut dels, &mut adds); |
| 296 | let left = SplitSide { |
| 297 | class: "ctx", |
| 298 | num: row.old.clone(), |
| 299 | code: row.code.clone(), |
| 300 | anchor: None, |
| 301 | plus_href: None, |
| 302 | }; |
| 303 | let new = row.new.clone(); |
| 304 | let (right, below) = SplitSide::from_row(row, "ctx dup", new); |
| 305 | out.push(SplitRow { |
| 306 | hunk: None, |
| 307 | left, |
| 308 | right, |
| 309 | below, |
| 310 | }); |
| 311 | } |
| 312 | } |
| 313 | } |
| 314 | flush_pairs(&mut out, &mut dels, &mut adds); |
| 315 | out |
| 316 | } |
| 317 | |
| 318 | impl FileView { |
| 319 | #[allow(clippy::too_many_arguments)] |
| 320 | fn new( |
| 321 | index: usize, |
| 322 | file: &FileDiff, |
| 323 | oid: &str, |
| 324 | parent: Option<&str>, |
| 325 | owner: &str, |
| 326 | repo: &str, |
| 327 | lang: Lang, |
| 328 | comments: &mut LineComments, |
| 329 | ) -> Self { |
| 330 | let label = match (&file.old_path, &file.new_path) { |
| 331 | (Some(old), Some(new)) if old != new => format!("{old} → {new}"), |
| 332 | (_, Some(new)) => new.clone(), |
| 333 | (Some(old), None) => old.clone(), |
| 334 | (None, None) => String::new(), |
| 335 | }; |
| 336 | let status = match file.status { |
| 337 | DiffStatus::Added => g::t(lang, K::StatusAdded), |
| 338 | DiffStatus::Deleted => g::t(lang, K::StatusDeleted), |
| 339 | DiffStatus::Modified => g::t(lang, K::StatusModified), |
| 340 | DiffStatus::Renamed => g::t(lang, K::StatusRenamed), |
| 341 | DiffStatus::Copied => g::t(lang, K::StatusCopied), |
| 342 | DiffStatus::TypeChanged => g::t(lang, K::StatusTypeChanged), |
| 343 | }; |
| 344 | let blob_commit = match file.status { |
| 345 | DiffStatus::Deleted => parent, |
| 346 | _ => Some(oid), |
| 347 | }; |
| 348 | let blob_path = file.new_path.as_ref().or(file.old_path.as_ref()); |
| 349 | let blob_href = match (blob_commit, blob_path) { |
| 350 | (Some(commit), Some(path)) => { |
| 351 | format!("/{owner}/{repo}/blob/{commit}/{}", enc(path)) |
| 352 | } |
| 353 | _ => String::new(), |
| 354 | }; |
| 355 | |
| 356 | let mut rows = Vec::new(); |
| 357 | for hunk in &file.hunks { |
| 358 | rows.push(DiffRow { |
| 359 | class: "diff-line hunk", |
| 360 | old: String::new(), |
| 361 | new: String::new(), |
| 362 | code: hunk.header.clone(), |
| 363 | anchored: comments.anchors, |
| 364 | anchor: None, |
| 365 | plus_href: None, |
| 366 | below: None, |
| 367 | }); |
| 368 | for line in &hunk.lines { |
| 369 | let (class, code) = match line.kind { |
| 370 | LineKind::Add => ("diff-line add", format!("+{}", line.text)), |
| 371 | LineKind::Del => ("diff-line del", format!("-{}", line.text)), |
| 372 | LineKind::Context => ("diff-line ctx", format!(" {}", line.text)), |
| 373 | LineKind::NoNewline => ("diff-line ctx", line.text.clone()), |
| 374 | }; |
| 375 | let key = match line.kind { |
| 376 | LineKind::Add | LineKind::Context => line.new_no.map(|n| (Side::New, n)), |
| 377 | LineKind::Del => line.old_no.map(|n| (Side::Old, n)), |
| 378 | LineKind::NoNewline => None, |
| 379 | }; |
| 380 | let (anchor, plus_href, below) = |
| 381 | match key { |
| 382 | Some((side, number)) => { |
| 383 | let id = format!("L-{index}-{}-{number}", side.as_str()); |
| 384 | let anchor = comments.anchors.then(|| id.clone()); |
| 385 | let plus_href = comments.anchors.then(|| { |
| 386 | format!( |
| 387 | "{}?comment={index}:{}:{number}#{id}", |
| 388 | comments.files_href, |
| 389 | side.as_str() |
| 390 | ) |
| 391 | }); |
| 392 | let target = (index, side, number); |
| 393 | let threads = comments.threads.remove(&target).unwrap_or_default(); |
| 394 | let form = (comments.anchors && comments.open_form == Some(target)) |
| 395 | .then(|| InlineForm { |
| 396 | action: format!("{}/review_comments", comments.ctx.pr_href), |
| 397 | cancel_href: comments.files_href.clone(), |
| 398 | csrf: comments.ctx.csrf.clone(), |
| 399 | file_index: index.to_string(), |
| 400 | side: side.as_str(), |
| 401 | line: number.to_string(), |
| 402 | head_oid: oid.to_owned(), |
| 403 | has_pending: comments.has_pending, |
| 404 | can_review: comments.ctx.can_review, |
| 405 | suggest_line: (side == Side::New).then(|| line.text.clone()), |
| 406 | }); |
| 407 | let below = (!threads.is_empty() || form.is_some()).then(|| RowBelow { |
| 408 | threads, |
| 409 | form, |
| 410 | ctx: comments.ctx.clone(), |
| 411 | }); |
| 412 | (anchor, plus_href, below) |
| 413 | } |
| 414 | None => (None, None, None), |
| 415 | }; |
| 416 | rows.push(DiffRow { |
| 417 | class, |
| 418 | old: line.old_no.map(|n| n.to_string()).unwrap_or_default(), |
| 419 | new: line.new_no.map(|n| n.to_string()).unwrap_or_default(), |
| 420 | code, |
| 421 | anchored: comments.anchors, |
| 422 | anchor, |
| 423 | plus_href, |
| 424 | below, |
| 425 | }); |
| 426 | } |
| 427 | } |
| 428 | |
| 429 | Self { |
| 430 | anchor: format!("file-{}", index + 1), |
| 431 | label, |
| 432 | status, |
| 433 | additions: file.additions, |
| 434 | deletions: file.deletions, |
| 435 | binary: file.is_binary, |
| 436 | truncated: file.truncated, |
| 437 | has_blob: !blob_href.is_empty(), |
| 438 | blob_href, |
| 439 | rows, |
| 440 | split_rows: Vec::new(), |
| 441 | } |
| 442 | } |
| 443 | } |
| 444 | |
| 445 | struct NavItem { |
| 446 | anchor_href: String, |
| 447 | label: String, |
| 448 | additions: u32, |
| 449 | deletions: u32, |
| 450 | } |
| 451 | |
| 452 | #[allow(clippy::too_many_arguments)] |
| 453 | #[component] |
| 454 | pub async fn diff_section( |
| 455 | cx: &Cx, |
| 456 | diff: &CommitDiff, |
| 457 | owner: &str, |
| 458 | repo: &str, |
| 459 | oid: &str, |
| 460 | parent: Option<&str>, |
| 461 | subject: &str, |
| 462 | #[default] lang: DiffLang, |
| 463 | #[default] comments: LineComments, |
| 464 | #[default] opts: DiffOpts, |
| 465 | ) -> Result<impl View> { |
| 466 | let lang = lang.0; |
| 467 | let mut comments = comments; |
| 468 | let anchored = comments.anchors; |
| 469 | let split = opts.split; |
| 470 | let mut files: Vec<FileView> = diff |
| 471 | .files |
| 472 | .iter() |
| 473 | .enumerate() |
| 474 | .filter(|(i, _)| opts.only.is_none_or(|only| only == *i)) |
| 475 | .map(|(i, file)| FileView::new(i, file, oid, parent, owner, repo, lang, &mut comments)) |
| 476 | .collect(); |
| 477 | let alone = opts.only.is_some(); |
| 478 | if split { |
| 479 | for file in &mut files { |
| 480 | file.split_rows = split_rows(std::mem::take(&mut file.rows)); |
| 481 | } |
| 482 | } |
| 483 | let toolbar = opts.enabled.then(|| Toolbar { |
| 484 | unified_href: opts.href(false, opts.ignore_ws), |
| 485 | split_href: opts.href(true, opts.ignore_ws), |
| 486 | ws_href: opts.href(opts.split, !opts.ignore_ws), |
| 487 | split: opts.split, |
| 488 | ignore_ws: opts.ignore_ws, |
| 489 | downloads: opts.downloads.clone(), |
| 490 | }); |
| 491 | let nav: Vec<NavItem> = files |
| 492 | .iter() |
| 493 | .map(|f| NavItem { |
| 494 | anchor_href: format!("#{}", f.anchor), |
| 495 | label: f.label.clone(), |
| 496 | additions: f.additions, |
| 497 | deletions: f.deletions, |
| 498 | }) |
| 499 | .collect(); |
| 500 | let total_add: u32 = files.iter().map(|f| f.additions).sum(); |
| 501 | let total_del: u32 = files.iter().map(|f| f.deletions).sum(); |
| 502 | let file_count = files.len(); |
| 503 | let no_files = files.is_empty(); |
| 504 | let files_label = g::changed_files(lang, file_count); |
| 505 | let truncated_note = g::diff_truncated(lang, MAX_FILES, subject); |
| 506 | let truncated_files = diff.truncated_files; |
| 507 | let add_label = format!("+{total_add}"); |
| 508 | let del_label = format!("-{total_del}"); |
| 509 | |
| 510 | Ok(view! { |
| 511 | <section class="diff-section"> |
| 512 | if !alone || toolbar.is_some() { |
| 513 | <div class="diff-head"> |
| 514 | if !alone { |
| 515 | <h2>(files_label.as_str()) " " <span class="diff-stats"><span class="add">(add_label.as_str())</span> " " <span class="del">(del_label.as_str())</span></span></h2> |
| 516 | } |
| 517 | if let Some(bar) = toolbar { |
| 518 | diff_toolbar(bar: bar, lang: lang) |
| 519 | } |
| 520 | </div> |
| 521 | } |
| 522 | if truncated_files { |
| 523 | <p class="state">(truncated_note.as_str())</p> |
| 524 | } |
| 525 | if no_files { |
| 526 | <p class="state">(g::t(lang, K::NoFileChanges))</p> |
| 527 | } else { |
| 528 | if !alone { |
| 529 | <ul class="diff-index"> |
| 530 | for item in nav.iter() { |
| 531 | <li> |
| 532 | <a href=(item.anchor_href.as_str())>(item.label.as_str())</a> |
| 533 | " " |
| 534 | <span class="diff-stats"><span class="add">(format!("+{}", item.additions))</span> " " <span class="del">(format!("-{}", item.deletions))</span></span> |
| 535 | </li> |
| 536 | } |
| 537 | </ul> |
| 538 | } |
| 539 | for file in files.into_iter() { |
| 540 | diff_file(file: file, lang: lang, split: split, anchored: anchored) |
| 541 | } |
| 542 | } |
| 543 | </section> |
| 544 | }) |
| 545 | } |
| 546 | |
| 547 | #[component] |
| 548 | async fn diff_toolbar(cx: &Cx, bar: Toolbar, lang: Lang) -> Result<impl View> { |
| 549 | let unified_on = if bar.split { "false" } else { "true" }; |
| 550 | let split_on = if bar.split { "true" } else { "false" }; |
| 551 | let ws_on = if bar.ignore_ws { "true" } else { "false" }; |
| 552 | Ok(view! { |
| 553 | <nav class="sub diff-toolbar" aria-label=(b::t(lang, b::K::DiffView))> |
| 554 | <a href=(bar.unified_href.as_str()) aria-pressed=(unified_on)>(b::t(lang, b::K::Unified))</a> |
| 555 | <a href=(bar.split_href.as_str()) aria-pressed=(split_on)>(b::t(lang, b::K::Split))</a> |
| 556 | <a href=(bar.ws_href.as_str()) aria-pressed=(ws_on)>(b::t(lang, b::K::IgnoreWhitespace))</a> |
| 557 | if let Some((patch, diff)) = bar.downloads { |
| 558 | <a class="act" href=(patch.as_str()) title=(b::t(lang, b::K::PatchTitle))>(b::t(lang, b::K::PatchLink))</a> |
| 559 | <a class="act" href=(diff.as_str()) title=(b::t(lang, b::K::DiffTitle))>(b::t(lang, b::K::DiffLink))</a> |
| 560 | } |
| 561 | </nav> |
| 562 | }) |
| 563 | } |
| 564 | |
| 565 | #[component] |
| 566 | async fn diff_file( |
| 567 | cx: &Cx, |
| 568 | file: FileView, |
| 569 | lang: Lang, |
| 570 | split: bool, |
| 571 | anchored: bool, |
| 572 | ) -> Result<impl View> { |
| 573 | let add_label = format!("+{}", file.additions); |
| 574 | let del_label = format!("-{}", file.deletions); |
| 575 | let rows = file.rows; |
| 576 | let pairs = file.split_rows; |
| 577 | let has_rows = !rows.is_empty() || !pairs.is_empty(); |
| 578 | let table_class = match (split, anchored) { |
| 579 | (true, true) => "diff-table diff-split anchored", |
| 580 | (true, false) => "diff-table diff-split", |
| 581 | _ => "diff-table", |
| 582 | }; |
| 583 | Ok(view! { |
| 584 | <section class="diff-file" id=(file.anchor.as_str()) data-keep-anchor=(file.label.as_str())> |
| 585 | <div class="sheet-tools diff-file-header"> |
| 586 | <strong class="diff-path">(file.label.as_str())</strong> |
| 587 | <span class="hint">(file.status)</span> |
| 588 | <span class="diff-stats"><span class="add">(add_label.as_str())</span> " " <span class="del">(del_label.as_str())</span></span> |
| 589 | </div> |
| 590 | if file.binary { |
| 591 | <p class="state diff-collapsed">(g::t(lang, K::BinaryFileChanged))</p> |
| 592 | } else { |
| 593 | if file.truncated { |
| 594 | if file.has_blob { |
| 595 | <p class="state diff-collapsed">(g::t(lang, K::DiffTooLargePrefix)) <a class="act" href=(file.blob_href.as_str())>(g::t(lang, K::ViewFile))</a></p> |
| 596 | } else { |
| 597 | <p class="state diff-collapsed">(g::t(lang, K::DiffTooLarge))</p> |
| 598 | } |
| 599 | } |
| 600 | if has_rows { |
| 601 | <div class="diff-file-body"> |
| 602 | <table class=(table_class)> |
| 603 | if split { |
| 604 | <colgroup> |
| 605 | if anchored { <col class="col-plus"> } |
| 606 | <col class="col-ln"> |
| 607 | <col class="col-code"> |
| 608 | if anchored { <col class="col-plus"> } |
| 609 | <col class="col-ln"> |
| 610 | <col class="col-code"> |
| 611 | </colgroup> |
| 612 | } |
| 613 | for row in rows.into_iter() { |
| 614 | diff_line(row: row, lang: lang) |
| 615 | } |
| 616 | for row in pairs.into_iter() { |
| 617 | diff_split_line(row: row, lang: lang, anchored: anchored) |
| 618 | } |
| 619 | </table> |
| 620 | </div> |
| 621 | } |
| 622 | } |
| 623 | </section> |
| 624 | }) |
| 625 | } |
| 626 | |
| 627 | #[component] |
| 628 | async fn diff_split_line(cx: &Cx, row: SplitRow, lang: Lang, anchored: bool) -> Result<impl View> { |
| 629 | let SplitRow { |
| 630 | hunk, |
| 631 | left, |
| 632 | right, |
| 633 | below, |
| 634 | } = row; |
| 635 | let (threads, form, ctx) = match below { |
| 636 | Some(below) => (below.threads, below.form, Some(below.ctx)), |
| 637 | None => (Vec::new(), None, None), |
| 638 | }; |
| 639 | let span = if anchored { "6" } else { "4" }; |
| 640 | let is_hunk = hunk.is_some(); |
| 641 | let hunk_text = hunk.unwrap_or_default(); |
| 642 | let left_plus = format!("diff-plus side-{}", left.class); |
| 643 | let left_ln = format!("diff-ln side-{}", left.class); |
| 644 | let left_code = format!("diff-code side-{}", left.class); |
| 645 | let right_plus = format!("diff-plus side-{}", right.class); |
| 646 | let right_ln = format!("diff-ln side-{}", right.class); |
| 647 | let right_code = format!("diff-code side-{}", right.class); |
| 648 | Ok(view! { |
| 649 | <tbody> |
| 650 | if is_hunk { |
| 651 | <tr class="diff-line hunk"> |
| 652 | <td colspan=(span) class="diff-code">(hunk_text.as_str())</td> |
| 653 | </tr> |
| 654 | } else { |
| 655 | <tr class="diff-split-row"> |
| 656 | if anchored { |
| 657 | <td class=(left_plus.as_str())> |
| 658 | if let Some(href) = left.plus_href { |
| 659 | <a href=(href.as_str()) title=(g::t(lang, K::CommentOnLine)) aria-label=(g::t(lang, K::CommentOnLine))>"+"</a> |
| 660 | } |
| 661 | </td> |
| 662 | } |
| 663 | <td class=(left_ln.as_str())>(left.num.as_str())</td> |
| 664 | <td class=(left_code.as_str()) id=(left.anchor.as_deref())>(left.code.as_str())</td> |
| 665 | if anchored { |
| 666 | <td class=(right_plus.as_str())> |
| 667 | if let Some(href) = right.plus_href { |
| 668 | <a href=(href.as_str()) title=(g::t(lang, K::CommentOnLine)) aria-label=(g::t(lang, K::CommentOnLine))>"+"</a> |
| 669 | } |
| 670 | </td> |
| 671 | } |
| 672 | <td class=(right_ln.as_str())>(right.num.as_str())</td> |
| 673 | <td class=(right_code.as_str()) id=(right.anchor.as_deref())>(right.code.as_str())</td> |
| 674 | </tr> |
| 675 | } |
| 676 | if let Some(ctx) = ctx { |
| 677 | <tr class="review-row"> |
| 678 | <td colspan=(span)> |
| 679 | <div class="review-threads"> |
| 680 | for thread in threads.into_iter() { |
| 681 | review_thread(lang: lang, thread: thread, ctx: ctx.clone(), show_hunk: false, return_to: "files") |
| 682 | } |
| 683 | if let Some(form) = form { |
| 684 | inline_comment_form(form: form, lang: lang) |
| 685 | } |
| 686 | </div> |
| 687 | </td> |
| 688 | </tr> |
| 689 | } |
| 690 | </tbody> |
| 691 | }) |
| 692 | } |
| 693 | |
| 694 | #[component] |
| 695 | async fn diff_line(cx: &Cx, row: DiffRow, lang: Lang) -> Result<impl View> { |
| 696 | let DiffRow { |
| 697 | class, |
| 698 | old, |
| 699 | new, |
| 700 | code, |
| 701 | anchored, |
| 702 | anchor, |
| 703 | plus_href, |
| 704 | below, |
| 705 | } = row; |
| 706 | let (threads, form, ctx) = match below { |
| 707 | Some(below) => (below.threads, below.form, Some(below.ctx)), |
| 708 | None => (Vec::new(), None, None), |
| 709 | }; |
| 710 | let span = if anchored { "4" } else { "3" }; |
| 711 | Ok(view! { |
| 712 | <tbody> |
| 713 | <tr class=(class) id=(anchor.as_deref())> |
| 714 | if anchored { |
| 715 | <td class="diff-plus"> |
| 716 | if let Some(href) = plus_href { |
| 717 | <a href=(href.as_str()) title=(g::t(lang, K::CommentOnLine)) aria-label=(g::t(lang, K::CommentOnLine))>"+"</a> |
| 718 | } |
| 719 | </td> |
| 720 | } |
| 721 | <td class="diff-ln">(old.as_str())</td> |
| 722 | <td class="diff-ln">(new.as_str())</td> |
| 723 | <td class="diff-code">(code.as_str())</td> |
| 724 | </tr> |
| 725 | if let Some(ctx) = ctx { |
| 726 | <tr class="review-row"> |
| 727 | <td colspan=(span)> |
| 728 | <div class="review-threads"> |
| 729 | for thread in threads.into_iter() { |
| 730 | review_thread(lang: lang, thread: thread, ctx: ctx.clone(), show_hunk: false, return_to: "files") |
| 731 | } |
| 732 | if let Some(form) = form { |
| 733 | inline_comment_form(form: form, lang: lang) |
| 734 | } |
| 735 | </div> |
| 736 | </td> |
| 737 | </tr> |
| 738 | } |
| 739 | </tbody> |
| 740 | }) |
| 741 | } |
| 742 | |
| 743 | #[component] |
| 744 | async fn inline_comment_form(cx: &Cx, form: InlineForm, lang: Lang) -> Result<impl View> { |
| 745 | let InlineForm { |
| 746 | action, |
| 747 | cancel_href, |
| 748 | csrf, |
| 749 | file_index, |
| 750 | side, |
| 751 | line, |
| 752 | head_oid, |
| 753 | has_pending, |
| 754 | can_review, |
| 755 | suggest_line, |
| 756 | } = form; |
| 757 | let review_label = if has_pending { |
| 758 | g::t(lang, K::AddReviewComment) |
| 759 | } else { |
| 760 | g::t(lang, K::StartReview) |
| 761 | }; |
| 762 | let suggest_label = pr_g::t(lang, PrK::SuggestChange); |
| 763 | Ok(view! { |
| 764 | <form method="post" action=(action.as_str()) class="review-inline-form fields"> |
| 765 | <input type="hidden" name="csrf" value=(csrf.as_str())> |
| 766 | <input type="hidden" name="file_index" value=(file_index.as_str())> |
| 767 | <input type="hidden" name="side" value=(side)> |
| 768 | <input type="hidden" name="line" value=(line.as_str())> |
| 769 | <input type="hidden" name="head_oid" value=(head_oid.as_str())> |
| 770 | <div class="field"> |
| 771 | <label for="inline-comment-body">(g::t(lang, K::CommentLabel))</label> |
| 772 | <textarea id="inline-comment-body" name="body" rows="4"></textarea> |
| 773 | </div> |
| 774 | <div class="acts"> |
| 775 | <button type="submit" class="primary" name="action" value="single">(g::t(lang, K::AddSingleComment))</button> |
| 776 | if can_review { |
| 777 | <button type="submit" class="act" name="action" value="review">(review_label)</button> |
| 778 | } |
| 779 | if let Some(text) = suggest_line { |
| 780 | <button type="button" class="act" data-suggest=(text.as_str())>(suggest_label)</button> |
| 781 | } |
| 782 | <a class="act" href=(cancel_href.as_str())>(crate::i18n::t(lang, crate::i18n::Key::Cancel))</a> |
| 783 | </div> |
| 784 | </form> |
| 785 | }) |
| 786 | } |