| b69ab31 | | | 1 | /** |
| b69ab31 | | | 2 | * Copyright (c) Meta Platforms, Inc. and affiliates. |
| b69ab31 | | | 3 | * |
| b69ab31 | | | 4 | * This source code is licensed under the MIT license found in the |
| b69ab31 | | | 5 | * LICENSE file in the root directory of this source tree. |
| b69ab31 | | | 6 | */ |
| b69ab31 | | | 7 | |
| b69ab31 | | | 8 | import type {ReactNode} from 'react'; |
| b69ab31 | | | 9 | import type {Hunk, ParsedDiff} from 'shared/patch/types'; |
| b69ab31 | | | 10 | import type {Result} from '../../types'; |
| b69ab31 | | | 11 | import type {TokenizedDiffHunk, TokenizedHunk} from './syntaxHighlightingTypes'; |
| b69ab31 | | | 12 | import type {Context, OneIndexedLineNumber} from './types'; |
| b69ab31 | | | 13 | |
| b69ab31 | | | 14 | import {diffChars} from 'diff'; |
| b69ab31 | | | 15 | import {ErrorNotice} from 'isl-components/ErrorNotice'; |
| b69ab31 | | | 16 | import {Icon} from 'isl-components/Icon'; |
| b69ab31 | | | 17 | import React, {useCallback, useEffect, useState} from 'react'; |
| b69ab31 | | | 18 | import {comparisonStringKey} from 'shared/Comparison'; |
| b69ab31 | | | 19 | import organizeLinesIntoGroups from 'shared/SplitDiffView/organizeLinesIntoGroups'; |
| b69ab31 | | | 20 | import { |
| b69ab31 | | | 21 | applyTokenizationToLine, |
| b69ab31 | | | 22 | createTokenizedIntralineDiff, |
| b69ab31 | | | 23 | } from 'shared/createTokenizedIntralineDiff'; |
| b69ab31 | | | 24 | import SplitDiffRow, {BlankLineNumber} from './SplitDiffRow'; |
| b69ab31 | | | 25 | import {useTableColumnSelection} from './copyFromSelectedColumn'; |
| b69ab31 | | | 26 | import {useTokenizedContents, useTokenizedHunks} from './syntaxHighlighting'; |
| b69ab31 | | | 27 | |
| b69ab31 | | | 28 | const MAX_INPUT_LENGTH_FOR_INTRALINE_DIFF = 300; |
| b69ab31 | | | 29 | |
| b69ab31 | | | 30 | export type SplitDiffTableProps = { |
| b69ab31 | | | 31 | ctx: Context; |
| b69ab31 | | | 32 | path: string; |
| b69ab31 | | | 33 | patch: ParsedDiff; |
| b69ab31 | | | 34 | }; |
| b69ab31 | | | 35 | |
| b69ab31 | | | 36 | export const SplitDiffTable = React.memo( |
| b69ab31 | | | 37 | ({ctx, path, patch}: SplitDiffTableProps): React.ReactElement => { |
| b69ab31 | | | 38 | const [deletedFileExpanded, setDeletedFileExpanded] = useState<boolean>(false); |
| b69ab31 | | | 39 | const [expandedSeparators, setExpandedSeparators] = useState<Readonly<Set<string>>>( |
| b69ab31 | | | 40 | () => new Set(), |
| b69ab31 | | | 41 | ); |
| b69ab31 | | | 42 | const onExpand = useCallback( |
| b69ab31 | | | 43 | (key: string) => { |
| b69ab31 | | | 44 | const amendedSet = new Set(expandedSeparators); |
| b69ab31 | | | 45 | amendedSet.add(key); |
| b69ab31 | | | 46 | setExpandedSeparators(amendedSet); |
| b69ab31 | | | 47 | }, |
| b69ab31 | | | 48 | [expandedSeparators, setExpandedSeparators], |
| b69ab31 | | | 49 | ); |
| b69ab31 | | | 50 | |
| b69ab31 | | | 51 | const t = ctx.t ?? (s => s); |
| b69ab31 | | | 52 | const tokenization = useTokenizedHunks(patch.newFileName ?? '', patch.hunks, ctx.useThemeHook); |
| b69ab31 | | | 53 | |
| b69ab31 | | | 54 | const {className: tableSelectionClassName, ...tableSelectionProps} = useTableColumnSelection(); |
| b69ab31 | | | 55 | |
| b69ab31 | | | 56 | const isDeleted = patch.newFileName === '/dev/null'; |
| b69ab31 | | | 57 | const isAdded = patch.type === 'Added'; |
| b69ab31 | | | 58 | |
| b69ab31 | | | 59 | const unified = ctx.display === 'unified'; |
| b69ab31 | | | 60 | const displayLineNumbers = ctx.displayLineNumbers ?? true; |
| b69ab31 | | | 61 | |
| b69ab31 | | | 62 | const {hunks} = patch; |
| b69ab31 | | | 63 | const lastHunkIndex = hunks.length - 1; |
| b69ab31 | | | 64 | const rows: React.ReactElement[] = []; |
| b69ab31 | | | 65 | if (!isDeleted || deletedFileExpanded) { |
| b69ab31 | | | 66 | hunks.forEach((hunk, index) => { |
| b69ab31 | | | 67 | // Show a separator before the first hunk if the file starts with a |
| b69ab31 | | | 68 | // section of unmodified lines that is hidden by default. |
| b69ab31 | | | 69 | if (index === 0 && (hunk.oldStart !== 1 || hunk.newStart !== 1)) { |
| b69ab31 | | | 70 | // TODO: test empty file that went from 644 to 755? |
| b69ab31 | | | 71 | const key = 's0'; |
| b69ab31 | | | 72 | if (expandedSeparators.has(key)) { |
| b69ab31 | | | 73 | rows.push( |
| b69ab31 | | | 74 | <ExpandingSeparator |
| b69ab31 | | | 75 | key={key} |
| b69ab31 | | | 76 | ctx={ctx} |
| b69ab31 | | | 77 | path={path} |
| b69ab31 | | | 78 | start={1} |
| b69ab31 | | | 79 | numLines={hunk.oldStart - 1} |
| b69ab31 | | | 80 | beforeLineStart={1} |
| b69ab31 | | | 81 | afterLineStart={1} |
| b69ab31 | | | 82 | />, |
| b69ab31 | | | 83 | ); |
| b69ab31 | | | 84 | } else if (ctx.fetchAdditionalLines != null) { |
| b69ab31 | | | 85 | const numLines = Math.max(hunk.oldStart, hunk.newStart) - 1; |
| b69ab31 | | | 86 | rows.push( |
| b69ab31 | | | 87 | <HunkSeparator key={key} numLines={numLines} onExpand={() => onExpand(key)} t={t} />, |
| b69ab31 | | | 88 | ); |
| b69ab31 | | | 89 | } |
| b69ab31 | | | 90 | } |
| b69ab31 | | | 91 | |
| b69ab31 | | | 92 | addRowsForHunk( |
| b69ab31 | | | 93 | unified, |
| b69ab31 | | | 94 | hunk, |
| b69ab31 | | | 95 | path, |
| b69ab31 | | | 96 | rows, |
| b69ab31 | | | 97 | tokenization?.[index], |
| b69ab31 | | | 98 | ctx.openFileToLine, |
| b69ab31 | | | 99 | displayLineNumbers, |
| b69ab31 | | | 100 | ); |
| b69ab31 | | | 101 | |
| b69ab31 | | | 102 | const isLast = index === lastHunkIndex; |
| b69ab31 | | | 103 | const nextHunk = hunks[index + 1]; |
| b69ab31 | | | 104 | const key = `s${hunk.oldStart}`; |
| b69ab31 | | | 105 | const canExpand = !isLast || !(isAdded || isDeleted || isHunkProbablyAtEndOfFile(hunk)); // added and deleted files are already expanded |
| b69ab31 | | | 106 | if (canExpand) { |
| b69ab31 | | | 107 | if (expandedSeparators.has(key)) { |
| b69ab31 | | | 108 | const start = hunk.oldStart + hunk.oldLines; |
| b69ab31 | | | 109 | const MAX_LINES_FETCH = 10000; // We don't know the total number of lines, so for the last hunk we just request a lot of lines. |
| b69ab31 | | | 110 | const numLines = isLast ? MAX_LINES_FETCH : nextHunk.oldStart - start; |
| b69ab31 | | | 111 | rows.push( |
| b69ab31 | | | 112 | <ExpandingSeparator |
| b69ab31 | | | 113 | key={key} |
| b69ab31 | | | 114 | ctx={ctx} |
| b69ab31 | | | 115 | start={start} |
| b69ab31 | | | 116 | numLines={numLines} |
| b69ab31 | | | 117 | path={path} |
| b69ab31 | | | 118 | beforeLineStart={hunk.oldStart + hunk.oldLines} |
| b69ab31 | | | 119 | afterLineStart={hunk.newStart + hunk.newLines} |
| b69ab31 | | | 120 | />, |
| b69ab31 | | | 121 | ); |
| b69ab31 | | | 122 | } else if (ctx.fetchAdditionalLines != null) { |
| b69ab31 | | | 123 | const numLines = isLast ? null : nextHunk.oldStart - hunk.oldLines - hunk.oldStart; |
| b69ab31 | | | 124 | rows.push( |
| b69ab31 | | | 125 | <HunkSeparator key={key} numLines={numLines} onExpand={() => onExpand(key)} t={t} />, |
| b69ab31 | | | 126 | ); |
| b69ab31 | | | 127 | } |
| b69ab31 | | | 128 | } |
| b69ab31 | | | 129 | }); |
| b69ab31 | | | 130 | } else { |
| b69ab31 | | | 131 | rows.push( |
| b69ab31 | | | 132 | <SeparatorRow> |
| b69ab31 | | | 133 | <InlineRowButton |
| b69ab31 | | | 134 | key={'show-deleted'} |
| b69ab31 | | | 135 | label={t('Show deleted file')} |
| b69ab31 | | | 136 | onClick={() => setDeletedFileExpanded(true)} |
| b69ab31 | | | 137 | /> |
| b69ab31 | | | 138 | </SeparatorRow>, |
| b69ab31 | | | 139 | ); |
| b69ab31 | | | 140 | } |
| b69ab31 | | | 141 | |
| b69ab31 | | | 142 | if (unified) { |
| b69ab31 | | | 143 | return ( |
| b69ab31 | | | 144 | <table |
| b69ab31 | | | 145 | className={ |
| b69ab31 | | | 146 | 'split-diff-view-hunk-table display-unified ' + (tableSelectionClassName ?? '') |
| b69ab31 | | | 147 | } |
| b69ab31 | | | 148 | {...tableSelectionProps}> |
| b69ab31 | | | 149 | <colgroup> |
| b69ab31 | | | 150 | {displayLineNumbers && <col width={50} />} |
| b69ab31 | | | 151 | {displayLineNumbers && <col width={50} />} |
| b69ab31 | | | 152 | <col width={'100%'} /> |
| b69ab31 | | | 153 | </colgroup> |
| b69ab31 | | | 154 | <tbody>{rows}</tbody> |
| b69ab31 | | | 155 | </table> |
| b69ab31 | | | 156 | ); |
| b69ab31 | | | 157 | } |
| b69ab31 | | | 158 | return ( |
| b69ab31 | | | 159 | <table |
| b69ab31 | | | 160 | className={'split-diff-view-hunk-table display-split ' + (tableSelectionClassName ?? '')} |
| b69ab31 | | | 161 | {...tableSelectionProps}> |
| b69ab31 | | | 162 | <colgroup> |
| b69ab31 | | | 163 | {displayLineNumbers && <col width={50} />} |
| b69ab31 | | | 164 | <col width={'50%'} /> |
| b69ab31 | | | 165 | {displayLineNumbers && <col width={50} />} |
| b69ab31 | | | 166 | <col width={'50%'} /> |
| b69ab31 | | | 167 | </colgroup> |
| b69ab31 | | | 168 | <tbody>{rows}</tbody> |
| b69ab31 | | | 169 | </table> |
| b69ab31 | | | 170 | ); |
| b69ab31 | | | 171 | }, |
| b69ab31 | | | 172 | ); |
| b69ab31 | | | 173 | |
| b69ab31 | | | 174 | /** |
| b69ab31 | | | 175 | * If the last hunk of a file doesn't have as many context lines as it should, |
| b69ab31 | | | 176 | * it's because it's at the end of the file. This is a clue we can skip showing |
| b69ab31 | | | 177 | * the expander. |
| b69ab31 | | | 178 | * This util should only be called on the last hunk in the file. |
| b69ab31 | | | 179 | */ |
| b69ab31 | | | 180 | function isHunkProbablyAtEndOfFile(hunk: Hunk): boolean { |
| b69ab31 | | | 181 | // we could conceivably check if the initial context length matches the end length, but that's not true in short files. |
| b69ab31 | | | 182 | const CONTEXT_LENGTH = 4; |
| b69ab31 | | | 183 | return !hunk.lines.slice(-CONTEXT_LENGTH).every(line => line.startsWith(' ')); |
| b69ab31 | | | 184 | } |
| b69ab31 | | | 185 | |
| b69ab31 | | | 186 | /** |
| b69ab31 | | | 187 | * Adds new rows to the supplied `rows` array. |
| b69ab31 | | | 188 | */ |
| b69ab31 | | | 189 | function addRowsForHunk( |
| b69ab31 | | | 190 | unified: boolean, |
| b69ab31 | | | 191 | hunk: Hunk, |
| b69ab31 | | | 192 | path: string, |
| b69ab31 | | | 193 | rows: React.ReactElement[], |
| b69ab31 | | | 194 | tokenization: TokenizedDiffHunk | undefined, |
| b69ab31 | | | 195 | openFileToLine?: (line: OneIndexedLineNumber) => unknown, |
| b69ab31 | | | 196 | displayLineNumbers: boolean = true, |
| b69ab31 | | | 197 | ): void { |
| b69ab31 | | | 198 | const {oldStart, newStart, lines} = hunk; |
| b69ab31 | | | 199 | const groups = organizeLinesIntoGroups(lines); |
| b69ab31 | | | 200 | let beforeLineNumber = oldStart; |
| b69ab31 | | | 201 | let afterLineNumber = newStart; |
| b69ab31 | | | 202 | |
| b69ab31 | | | 203 | let beforeTokenizedIndex = 0; |
| b69ab31 | | | 204 | let afterTokenizedIndex = 0; |
| b69ab31 | | | 205 | |
| b69ab31 | | | 206 | groups.forEach(group => { |
| b69ab31 | | | 207 | const {common, removed, added} = group; |
| b69ab31 | | | 208 | addUnmodifiedRows( |
| b69ab31 | | | 209 | unified, |
| b69ab31 | | | 210 | common, |
| b69ab31 | | | 211 | path, |
| b69ab31 | | | 212 | 'common', |
| b69ab31 | | | 213 | beforeLineNumber, |
| b69ab31 | | | 214 | afterLineNumber, |
| b69ab31 | | | 215 | rows, |
| b69ab31 | | | 216 | tokenization?.[0].slice(beforeTokenizedIndex), |
| b69ab31 | | | 217 | tokenization?.[1].slice(afterTokenizedIndex), |
| b69ab31 | | | 218 | openFileToLine, |
| b69ab31 | | | 219 | displayLineNumbers, |
| b69ab31 | | | 220 | ); |
| b69ab31 | | | 221 | beforeLineNumber += common.length; |
| b69ab31 | | | 222 | afterLineNumber += common.length; |
| b69ab31 | | | 223 | beforeTokenizedIndex += common.length; |
| b69ab31 | | | 224 | afterTokenizedIndex += common.length; |
| b69ab31 | | | 225 | |
| b69ab31 | | | 226 | // split content, or before lines when unified |
| b69ab31 | | | 227 | const linesA = []; |
| b69ab31 | | | 228 | // after lines when unified, or empty when using "split" |
| b69ab31 | | | 229 | const linesB = []; |
| b69ab31 | | | 230 | |
| b69ab31 | | | 231 | const maxIndex = Math.max(removed.length, added.length); |
| b69ab31 | | | 232 | for (let index = 0; index < maxIndex; ++index) { |
| b69ab31 | | | 233 | const removedLine = removed[index]; |
| b69ab31 | | | 234 | const addedLine = added[index]; |
| b69ab31 | | | 235 | if (removedLine != null && addedLine != null) { |
| b69ab31 | | | 236 | let beforeAndAfter; |
| b69ab31 | | | 237 | |
| b69ab31 | | | 238 | if (tokenization != null) { |
| b69ab31 | | | 239 | beforeAndAfter = createTokenizedIntralineDiff( |
| b69ab31 | | | 240 | removedLine, |
| b69ab31 | | | 241 | tokenization[0][beforeTokenizedIndex], |
| b69ab31 | | | 242 | addedLine, |
| b69ab31 | | | 243 | tokenization[1][afterTokenizedIndex], |
| b69ab31 | | | 244 | ); |
| b69ab31 | | | 245 | } else { |
| b69ab31 | | | 246 | beforeAndAfter = createIntralineDiff(removedLine, addedLine); |
| b69ab31 | | | 247 | } |
| b69ab31 | | | 248 | |
| b69ab31 | | | 249 | const [before, after] = beforeAndAfter; |
| b69ab31 | | | 250 | const [beforeLine, beforeChange, afterLine, afterChange] = SplitDiffRow({ |
| b69ab31 | | | 251 | beforeLineNumber, |
| b69ab31 | | | 252 | before, |
| b69ab31 | | | 253 | afterLineNumber, |
| b69ab31 | | | 254 | after, |
| b69ab31 | | | 255 | rowType: 'modify', |
| b69ab31 | | | 256 | path, |
| b69ab31 | | | 257 | unified, |
| b69ab31 | | | 258 | openFileToLine, |
| b69ab31 | | | 259 | }); |
| b69ab31 | | | 260 | |
| b69ab31 | | | 261 | if (unified) { |
| b69ab31 | | | 262 | linesA.push( |
| b69ab31 | | | 263 | <tr key={`${beforeLineNumber}/${afterLineNumber}:b`}> |
| b69ab31 | | | 264 | {displayLineNumbers && beforeLine} |
| b69ab31 | | | 265 | {displayLineNumbers && <BlankLineNumber before />} |
| b69ab31 | | | 266 | {beforeChange} |
| b69ab31 | | | 267 | </tr>, |
| b69ab31 | | | 268 | ); |
| b69ab31 | | | 269 | linesB.push( |
| b69ab31 | | | 270 | <tr key={`${beforeLineNumber}/${afterLineNumber}:a`}> |
| b69ab31 | | | 271 | {displayLineNumbers && <BlankLineNumber after />} |
| b69ab31 | | | 272 | {displayLineNumbers && afterLine} |
| b69ab31 | | | 273 | {afterChange} |
| b69ab31 | | | 274 | </tr>, |
| b69ab31 | | | 275 | ); |
| b69ab31 | | | 276 | } else { |
| b69ab31 | | | 277 | linesA.push( |
| b69ab31 | | | 278 | <tr key={`${beforeLineNumber}/${afterLineNumber}`}> |
| b69ab31 | | | 279 | {displayLineNumbers && beforeLine} |
| b69ab31 | | | 280 | {beforeChange} |
| b69ab31 | | | 281 | {displayLineNumbers && afterLine} |
| b69ab31 | | | 282 | {afterChange} |
| b69ab31 | | | 283 | </tr>, |
| b69ab31 | | | 284 | ); |
| b69ab31 | | | 285 | } |
| b69ab31 | | | 286 | ++beforeLineNumber; |
| b69ab31 | | | 287 | ++afterLineNumber; |
| b69ab31 | | | 288 | ++beforeTokenizedIndex; |
| b69ab31 | | | 289 | ++afterTokenizedIndex; |
| b69ab31 | | | 290 | } else if (removedLine != null) { |
| b69ab31 | | | 291 | const [beforeLine, beforeChange, afterLine, afterChange] = SplitDiffRow({ |
| b69ab31 | | | 292 | beforeLineNumber, |
| b69ab31 | | | 293 | before: |
| b69ab31 | | | 294 | tokenization?.[0] == null |
| b69ab31 | | | 295 | ? removedLine |
| b69ab31 | | | 296 | : applyTokenizationToLine(removedLine, tokenization[0][beforeTokenizedIndex]), |
| b69ab31 | | | 297 | afterLineNumber: null, |
| b69ab31 | | | 298 | after: null, |
| b69ab31 | | | 299 | rowType: 'remove', |
| b69ab31 | | | 300 | path, |
| b69ab31 | | | 301 | unified, |
| b69ab31 | | | 302 | openFileToLine, |
| b69ab31 | | | 303 | }); |
| b69ab31 | | | 304 | |
| b69ab31 | | | 305 | if (unified) { |
| b69ab31 | | | 306 | linesA.push( |
| b69ab31 | | | 307 | <tr key={`${beforeLineNumber}/`}> |
| b69ab31 | | | 308 | {displayLineNumbers && beforeLine} |
| b69ab31 | | | 309 | {displayLineNumbers && <BlankLineNumber before />} |
| b69ab31 | | | 310 | {beforeChange} |
| b69ab31 | | | 311 | </tr>, |
| b69ab31 | | | 312 | ); |
| b69ab31 | | | 313 | } else { |
| b69ab31 | | | 314 | linesA.push( |
| b69ab31 | | | 315 | <tr key={`${beforeLineNumber}/`}> |
| b69ab31 | | | 316 | {displayLineNumbers && beforeLine} |
| b69ab31 | | | 317 | {beforeChange} |
| b69ab31 | | | 318 | {displayLineNumbers && afterLine} |
| b69ab31 | | | 319 | {afterChange} |
| b69ab31 | | | 320 | </tr>, |
| b69ab31 | | | 321 | ); |
| b69ab31 | | | 322 | } |
| b69ab31 | | | 323 | ++beforeLineNumber; |
| b69ab31 | | | 324 | ++beforeTokenizedIndex; |
| b69ab31 | | | 325 | } else { |
| b69ab31 | | | 326 | const [beforeLine, beforeChange, afterLine, afterChange] = SplitDiffRow({ |
| b69ab31 | | | 327 | beforeLineNumber: null, |
| b69ab31 | | | 328 | before: null, |
| b69ab31 | | | 329 | afterLineNumber, |
| b69ab31 | | | 330 | after: |
| b69ab31 | | | 331 | tokenization?.[1] == null |
| b69ab31 | | | 332 | ? addedLine |
| b69ab31 | | | 333 | : applyTokenizationToLine(addedLine, tokenization[1][afterTokenizedIndex]), |
| b69ab31 | | | 334 | rowType: 'add', |
| b69ab31 | | | 335 | path, |
| b69ab31 | | | 336 | unified, |
| b69ab31 | | | 337 | openFileToLine, |
| b69ab31 | | | 338 | }); |
| b69ab31 | | | 339 | |
| b69ab31 | | | 340 | if (unified) { |
| b69ab31 | | | 341 | linesB.push( |
| b69ab31 | | | 342 | <tr key={`/${afterLineNumber}`}> |
| b69ab31 | | | 343 | {displayLineNumbers && <BlankLineNumber after />} |
| b69ab31 | | | 344 | {displayLineNumbers && afterLine} |
| b69ab31 | | | 345 | {afterChange} |
| b69ab31 | | | 346 | </tr>, |
| b69ab31 | | | 347 | ); |
| b69ab31 | | | 348 | } else { |
| b69ab31 | | | 349 | linesA.push( |
| b69ab31 | | | 350 | <tr key={`/${afterLineNumber}`}> |
| b69ab31 | | | 351 | {displayLineNumbers && beforeLine} |
| b69ab31 | | | 352 | {beforeChange} |
| b69ab31 | | | 353 | {displayLineNumbers && afterLine} |
| b69ab31 | | | 354 | {afterChange} |
| b69ab31 | | | 355 | </tr>, |
| b69ab31 | | | 356 | ); |
| b69ab31 | | | 357 | } |
| b69ab31 | | | 358 | ++afterLineNumber; |
| b69ab31 | | | 359 | ++afterTokenizedIndex; |
| b69ab31 | | | 360 | } |
| b69ab31 | | | 361 | } |
| b69ab31 | | | 362 | |
| b69ab31 | | | 363 | rows.push(...linesA, ...linesB); |
| b69ab31 | | | 364 | }); |
| b69ab31 | | | 365 | } |
| b69ab31 | | | 366 | |
| b69ab31 | | | 367 | function InlineRowButton({onClick, label}: {onClick: () => unknown; label: ReactNode}) { |
| b69ab31 | | | 368 | return ( |
| b69ab31 | | | 369 | // TODO: tabindex or make this a button for accessibility |
| b69ab31 | | | 370 | <div className="split-diff-view-inline-row-button" onClick={onClick}> |
| b69ab31 | | | 371 | <Icon icon="unfold" /> |
| b69ab31 | | | 372 | <span className="inline-row-button-label">{label}</span> |
| b69ab31 | | | 373 | <Icon icon="unfold" /> |
| b69ab31 | | | 374 | </div> |
| b69ab31 | | | 375 | ); |
| b69ab31 | | | 376 | } |
| b69ab31 | | | 377 | |
| b69ab31 | | | 378 | /** |
| b69ab31 | | | 379 | * Adds new rows to the supplied `rows` array. |
| b69ab31 | | | 380 | */ |
| b69ab31 | | | 381 | function addUnmodifiedRows( |
| b69ab31 | | | 382 | unified: boolean, |
| b69ab31 | | | 383 | lines: string[], |
| b69ab31 | | | 384 | path: string, |
| b69ab31 | | | 385 | rowType: 'common' | 'expanded', |
| b69ab31 | | | 386 | initialBeforeLineNumber: number, |
| b69ab31 | | | 387 | initialAfterLineNumber: number, |
| b69ab31 | | | 388 | rows: React.ReactElement[], |
| b69ab31 | | | 389 | tokenizationBefore?: TokenizedHunk | undefined, |
| b69ab31 | | | 390 | tokenizationAfter?: TokenizedHunk | undefined, |
| b69ab31 | | | 391 | openFileToLine?: (line: OneIndexedLineNumber) => unknown, |
| b69ab31 | | | 392 | displayLineNumbers: boolean = true, |
| b69ab31 | | | 393 | ): void { |
| b69ab31 | | | 394 | let beforeLineNumber = initialBeforeLineNumber; |
| b69ab31 | | | 395 | let afterLineNumber = initialAfterLineNumber; |
| b69ab31 | | | 396 | lines.forEach((lineContent, i) => { |
| b69ab31 | | | 397 | const [beforeLine, beforeChange, afterLine, afterChange] = SplitDiffRow({ |
| b69ab31 | | | 398 | beforeLineNumber, |
| b69ab31 | | | 399 | before: |
| b69ab31 | | | 400 | tokenizationBefore?.[i] == null |
| b69ab31 | | | 401 | ? lineContent |
| b69ab31 | | | 402 | : applyTokenizationToLine(lineContent, tokenizationBefore[i]), |
| b69ab31 | | | 403 | afterLineNumber, |
| b69ab31 | | | 404 | after: |
| b69ab31 | | | 405 | tokenizationAfter?.[i] == null |
| b69ab31 | | | 406 | ? lineContent |
| b69ab31 | | | 407 | : applyTokenizationToLine(lineContent, tokenizationAfter[i]), |
| b69ab31 | | | 408 | rowType, |
| b69ab31 | | | 409 | path, |
| b69ab31 | | | 410 | unified, |
| b69ab31 | | | 411 | openFileToLine, |
| b69ab31 | | | 412 | }); |
| b69ab31 | | | 413 | if (unified) { |
| b69ab31 | | | 414 | rows.push( |
| b69ab31 | | | 415 | <tr key={`${beforeLineNumber}/${afterLineNumber}`}> |
| b69ab31 | | | 416 | {displayLineNumbers && beforeLine} |
| b69ab31 | | | 417 | {displayLineNumbers && afterLine} |
| b69ab31 | | | 418 | {beforeChange} |
| b69ab31 | | | 419 | </tr>, |
| b69ab31 | | | 420 | ); |
| b69ab31 | | | 421 | } else { |
| b69ab31 | | | 422 | rows.push( |
| b69ab31 | | | 423 | <tr key={`${beforeLineNumber}/${afterLineNumber}`}> |
| b69ab31 | | | 424 | {displayLineNumbers && beforeLine} |
| b69ab31 | | | 425 | {beforeChange} |
| b69ab31 | | | 426 | {displayLineNumbers && afterLine} |
| b69ab31 | | | 427 | {afterChange} |
| b69ab31 | | | 428 | </tr>, |
| b69ab31 | | | 429 | ); |
| b69ab31 | | | 430 | } |
| b69ab31 | | | 431 | ++beforeLineNumber; |
| b69ab31 | | | 432 | ++afterLineNumber; |
| b69ab31 | | | 433 | }); |
| b69ab31 | | | 434 | } |
| b69ab31 | | | 435 | |
| b69ab31 | | | 436 | function createIntralineDiff( |
| b69ab31 | | | 437 | before: string, |
| b69ab31 | | | 438 | after: string, |
| b69ab31 | | | 439 | ): [React.ReactFragment, React.ReactFragment] { |
| b69ab31 | | | 440 | // For lines longer than this, diffChars() can get very expensive to compute |
| b69ab31 | | | 441 | // and is likely of little value to the user. |
| b69ab31 | | | 442 | if (before.length + after.length > MAX_INPUT_LENGTH_FOR_INTRALINE_DIFF) { |
| b69ab31 | | | 443 | return [before, after]; |
| b69ab31 | | | 444 | } |
| b69ab31 | | | 445 | |
| b69ab31 | | | 446 | const changes = diffChars(before, after); |
| b69ab31 | | | 447 | const beforeElements: React.ReactNode[] = []; |
| b69ab31 | | | 448 | const afterElements: React.ReactNode[] = []; |
| b69ab31 | | | 449 | changes.forEach((change, index) => { |
| b69ab31 | | | 450 | const {added, removed, value} = change; |
| b69ab31 | | | 451 | if (added) { |
| b69ab31 | | | 452 | afterElements.push( |
| b69ab31 | | | 453 | <span key={index} className="patch-add-word"> |
| b69ab31 | | | 454 | {value} |
| b69ab31 | | | 455 | </span>, |
| b69ab31 | | | 456 | ); |
| b69ab31 | | | 457 | } else if (removed) { |
| b69ab31 | | | 458 | beforeElements.push( |
| b69ab31 | | | 459 | <span key={index} className="patch-remove-word"> |
| b69ab31 | | | 460 | {value} |
| b69ab31 | | | 461 | </span>, |
| b69ab31 | | | 462 | ); |
| b69ab31 | | | 463 | } else { |
| b69ab31 | | | 464 | beforeElements.push(value); |
| b69ab31 | | | 465 | afterElements.push(value); |
| b69ab31 | | | 466 | } |
| b69ab31 | | | 467 | }); |
| b69ab31 | | | 468 | |
| b69ab31 | | | 469 | return [beforeElements, afterElements]; |
| b69ab31 | | | 470 | } |
| b69ab31 | | | 471 | |
| b69ab31 | | | 472 | /** |
| b69ab31 | | | 473 | * Visual element to delimit the discontinuity in a SplitDiffView. |
| b69ab31 | | | 474 | */ |
| b69ab31 | | | 475 | function HunkSeparator({ |
| b69ab31 | | | 476 | numLines, |
| b69ab31 | | | 477 | onExpand, |
| b69ab31 | | | 478 | t, |
| b69ab31 | | | 479 | }: { |
| b69ab31 | | | 480 | numLines: number | null; |
| b69ab31 | | | 481 | onExpand: () => unknown; |
| b69ab31 | | | 482 | t: (s: string) => string; |
| b69ab31 | | | 483 | }): React.ReactElement | null { |
| b69ab31 | | | 484 | if (numLines === 0) { |
| b69ab31 | | | 485 | return null; |
| b69ab31 | | | 486 | } |
| b69ab31 | | | 487 | // TODO: Ensure numLines is never below a certain threshold: it takes up more |
| b69ab31 | | | 488 | // space to display the separator than it does to display the text (though |
| b69ab31 | | | 489 | // admittedly fetching the collapsed text is an async operation). |
| b69ab31 | | | 490 | const label = |
| b69ab31 | | | 491 | numLines == null |
| b69ab31 | | | 492 | ? // to expand the remaining lines at the end of the file, we don't know the size ahead of time, |
| b69ab31 | | | 493 | // just omit the amount to be expanded |
| b69ab31 | | | 494 | t('Expand lines') |
| b69ab31 | | | 495 | : numLines === 1 |
| b69ab31 | | | 496 | ? t('Expand 1 line') |
| b69ab31 | | | 497 | : t(`Expand ${numLines} lines`); |
| b69ab31 | | | 498 | return ( |
| b69ab31 | | | 499 | <SeparatorRow> |
| b69ab31 | | | 500 | <InlineRowButton label={label} onClick={onExpand} /> |
| b69ab31 | | | 501 | </SeparatorRow> |
| b69ab31 | | | 502 | ); |
| b69ab31 | | | 503 | } |
| b69ab31 | | | 504 | |
| b69ab31 | | | 505 | /** |
| b69ab31 | | | 506 | * This replaces a <HunkSeparator> when the user clicks on it to expand the |
| b69ab31 | | | 507 | * hidden file contents. |
| b69ab31 | | | 508 | * By rendering this, additional lines are automatically fetched. |
| b69ab31 | | | 509 | */ |
| b69ab31 | | | 510 | function ExpandingSeparator({ |
| b69ab31 | | | 511 | ctx, |
| b69ab31 | | | 512 | path, |
| b69ab31 | | | 513 | start, |
| b69ab31 | | | 514 | numLines, |
| b69ab31 | | | 515 | beforeLineStart, |
| b69ab31 | | | 516 | afterLineStart, |
| b69ab31 | | | 517 | }: { |
| b69ab31 | | | 518 | ctx: Context; |
| b69ab31 | | | 519 | path: string; |
| b69ab31 | | | 520 | numLines: number; |
| b69ab31 | | | 521 | start: number; |
| b69ab31 | | | 522 | beforeLineStart: number; |
| b69ab31 | | | 523 | afterLineStart: number; |
| b69ab31 | | | 524 | }): React.ReactElement { |
| b69ab31 | | | 525 | const result = useFetchLines(ctx, numLines, start); |
| b69ab31 | | | 526 | const t = ctx.t ?? (s => s); |
| b69ab31 | | | 527 | |
| b69ab31 | | | 528 | const tokenization = useTokenizedContents(path, result?.value, ctx.useThemeHook); |
| b69ab31 | | | 529 | if (result == null) { |
| b69ab31 | | | 530 | return ( |
| b69ab31 | | | 531 | <SeparatorRow> |
| b69ab31 | | | 532 | <div className="split-diff-view-loading-row"> |
| b69ab31 | | | 533 | <Icon icon="loading" /> |
| b69ab31 | | | 534 | <span>{t('Loading...')}</span> |
| b69ab31 | | | 535 | </div> |
| b69ab31 | | | 536 | </SeparatorRow> |
| b69ab31 | | | 537 | ); |
| b69ab31 | | | 538 | } |
| b69ab31 | | | 539 | if (result.error) { |
| b69ab31 | | | 540 | return ( |
| b69ab31 | | | 541 | <SeparatorRow> |
| b69ab31 | | | 542 | <div className="split-diff-view-error-row"> |
| b69ab31 | | | 543 | <ErrorNotice error={result.error} title={t('Unable to fetch additional lines')} /> |
| b69ab31 | | | 544 | </div> |
| b69ab31 | | | 545 | </SeparatorRow> |
| b69ab31 | | | 546 | ); |
| b69ab31 | | | 547 | } |
| b69ab31 | | | 548 | |
| b69ab31 | | | 549 | const rows: React.ReactElement[] = []; |
| b69ab31 | | | 550 | addUnmodifiedRows( |
| b69ab31 | | | 551 | ctx.display === 'unified', |
| b69ab31 | | | 552 | result.value, |
| b69ab31 | | | 553 | path, |
| b69ab31 | | | 554 | 'expanded', |
| b69ab31 | | | 555 | beforeLineStart, |
| b69ab31 | | | 556 | afterLineStart, |
| b69ab31 | | | 557 | rows, |
| b69ab31 | | | 558 | tokenization, |
| b69ab31 | | | 559 | tokenization, |
| b69ab31 | | | 560 | ctx.openFileToLine, |
| b69ab31 | | | 561 | ctx.displayLineNumbers, |
| b69ab31 | | | 562 | ); |
| b69ab31 | | | 563 | return <>{rows}</>; |
| b69ab31 | | | 564 | } |
| b69ab31 | | | 565 | |
| b69ab31 | | | 566 | function SeparatorRow({children}: {children: React.ReactNode}): React.ReactElement { |
| b69ab31 | | | 567 | return ( |
| b69ab31 | | | 568 | <tr className="separator-row"> |
| b69ab31 | | | 569 | <td colSpan={4} className="separator"> |
| b69ab31 | | | 570 | {children} |
| b69ab31 | | | 571 | </td> |
| b69ab31 | | | 572 | </tr> |
| b69ab31 | | | 573 | ); |
| b69ab31 | | | 574 | } |
| b69ab31 | | | 575 | |
| b69ab31 | | | 576 | /** Fetches context lines */ |
| b69ab31 | | | 577 | function useFetchLines(ctx: Context, numLines: number, start: number) { |
| b69ab31 | | | 578 | const [fetchedLines, setFetchedLines] = useState<Result<Array<string>> | undefined>(undefined); |
| b69ab31 | | | 579 | |
| b69ab31 | | | 580 | // Use-case controlled key that allows invalidating the fetched lines. |
| b69ab31 | | | 581 | const invalidationKey = ctx.useComparisonInvalidationKeyHook?.(); |
| b69ab31 | | | 582 | |
| b69ab31 | | | 583 | const comparisonKey = comparisonStringKey(ctx.id.comparison); |
| b69ab31 | | | 584 | useEffect(() => { |
| b69ab31 | | | 585 | ctx.fetchAdditionalLines?.(ctx.id, start, numLines).then(result => { |
| b69ab31 | | | 586 | setFetchedLines(result); |
| b69ab31 | | | 587 | }); |
| b69ab31 | | | 588 | // eslint-disable-next-line react-hooks/exhaustive-deps |
| b69ab31 | | | 589 | }, [invalidationKey, ctx.id.path, comparisonKey, numLines, start]); |
| b69ab31 | | | 590 | |
| b69ab31 | | | 591 | return fetchedLines; |
| b69ab31 | | | 592 | } |