* Returns an array of rows with the needed data to render a side-by-side diff * with them. * * In some situations it will merge a deleted an added row into a single * modified row, in order to display them side by side (This happens when there * are consecutive added and deleted rows). * * @p
( hunkIndex: number, hunk: DiffHunk, showSideBySideDiff: boolean, enableDiffExpansion: boolean )
| 1804 | * @param showSideBySideDiff Whether or not show the diff in side by side mode. |
| 1805 | */ |
| 1806 | function getDiffRowsFromHunk( |
| 1807 | hunkIndex: number, |
| 1808 | hunk: DiffHunk, |
| 1809 | showSideBySideDiff: boolean, |
| 1810 | enableDiffExpansion: boolean |
| 1811 | ): ReadonlyArray<SimplifiedDiffRow> { |
| 1812 | const rows = new Array<SimplifiedDiffRow>() |
| 1813 | |
| 1814 | /** |
| 1815 | * Array containing multiple consecutive added/deleted lines. This |
| 1816 | * is used to be able to merge them into modified rows. |
| 1817 | */ |
| 1818 | let modifiedLines = new Array<ModifiedLine>() |
| 1819 | |
| 1820 | for (const [num, line] of hunk.lines.entries()) { |
| 1821 | const diffLineNumber = hunk.unifiedDiffStart + num |
| 1822 | |
| 1823 | if (line.type === DiffLineType.Delete || line.type === DiffLineType.Add) { |
| 1824 | modifiedLines.push({ line, diffLineNumber }) |
| 1825 | continue |
| 1826 | } |
| 1827 | |
| 1828 | if (modifiedLines.length > 0) { |
| 1829 | // If the current line is not added/deleted and we have any added/deleted |
| 1830 | // line stored, we need to process them. |
| 1831 | for (const row of getModifiedRows(modifiedLines, showSideBySideDiff)) { |
| 1832 | rows.push(row) |
| 1833 | } |
| 1834 | modifiedLines = [] |
| 1835 | } |
| 1836 | |
| 1837 | if (line.type === DiffLineType.Hunk) { |
| 1838 | rows.push({ |
| 1839 | type: DiffRowType.Hunk, |
| 1840 | content: line.text, |
| 1841 | expansionType: enableDiffExpansion |
| 1842 | ? hunk.expansionType |
| 1843 | : DiffHunkExpansionType.None, |
| 1844 | hunkIndex, |
| 1845 | }) |
| 1846 | continue |
| 1847 | } |
| 1848 | |
| 1849 | if (line.type === DiffLineType.Context) { |
| 1850 | assertNonNullable( |
| 1851 | line.oldLineNumber, |
| 1852 | `No oldLineNumber for ${diffLineNumber}` |
| 1853 | ) |
| 1854 | assertNonNullable( |
| 1855 | line.newLineNumber, |
| 1856 | `No newLineNumber for ${diffLineNumber}` |
| 1857 | ) |
| 1858 | |
| 1859 | rows.push({ |
| 1860 | type: DiffRowType.Context, |
| 1861 | content: line.content, |
| 1862 | beforeLineNumber: line.oldLineNumber, |
| 1863 | afterLineNumber: line.newLineNumber, |
no test coverage detected