|
| 1 | +type Block = { start: { source: number, output: number }, end: { source: number, output: number }, type: string, source: string } |
| 2 | + |
| 3 | + |
| 4 | +export const makeDiffSyncByRow = (original: string, modified: string) => { |
| 5 | + const originalLines = original.split('\n') |
| 6 | + const modifiedLines = modified.split('\n') |
| 7 | + |
| 8 | + let sourceShift = 0 |
| 9 | + let outputShift = 0 |
| 10 | + const blocks = [] |
| 11 | + |
| 12 | + for (let i = 0; i < Math.max(originalLines.length, modifiedLines.length); i++) { |
| 13 | + const sourceLine = (originalLines[i] ?? '') + '\n' |
| 14 | + const modifiedLine = (modifiedLines[i] ?? '') + '\n' |
| 15 | + |
| 16 | + if (sourceLine === modifiedLine) { |
| 17 | + blocks.push({ |
| 18 | + type: 'equal', |
| 19 | + start: { source: sourceShift, output: outputShift }, |
| 20 | + end: { |
| 21 | + source: sourceShift + sourceLine.length, |
| 22 | + output: outputShift + sourceLine.length, |
| 23 | + }, |
| 24 | + source: sourceLine, |
| 25 | + }) |
| 26 | + } else { |
| 27 | + const lineDiff = makeDiffSync(sourceLine, modifiedLine) |
| 28 | + for (const block of lineDiff) { |
| 29 | + blocks.push({ |
| 30 | + ...block, |
| 31 | + start: { |
| 32 | + source: block.start.source + sourceShift, |
| 33 | + output: block.start.output + outputShift, |
| 34 | + }, |
| 35 | + end: { |
| 36 | + source: block.end.source + sourceShift, |
| 37 | + output: block.end.output + outputShift, |
| 38 | + }, |
| 39 | + }) |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + sourceShift += sourceLine.length |
| 44 | + outputShift += modifiedLine.length |
| 45 | + } |
| 46 | + |
| 47 | + return mergeEqualBlocks(blocks) |
| 48 | +} |
| 49 | + |
| 50 | +const mergeEqualBlocks = (blocks: Block[]) => { |
| 51 | + return blocks.reduce((acc, block) => { |
| 52 | + const last = acc.at(-1) |
| 53 | + if (block.type === 'equal' && last?.type === 'equal') { |
| 54 | + last.source += block.source |
| 55 | + last.end = block.end |
| 56 | + } else { |
| 57 | + acc.push(block) |
| 58 | + } |
| 59 | + return acc |
| 60 | + }, [] as Block[]) |
| 61 | +} |
| 62 | + |
| 63 | +const makeDiffSync = (source: string, output: string) => { |
| 64 | + let originalShift = 0 |
| 65 | + let modifiedShift = 0 |
| 66 | + |
| 67 | + function makeBlock(type: string, sourceStart: number, sourceEnd: number, outputStart: number, outputEnd: number) { |
| 68 | + const blockSource = type === 'insert' ? output.slice(outputStart, outputEnd) : source.slice(sourceStart, sourceEnd) |
| 69 | + |
| 70 | + return { |
| 71 | + start: { |
| 72 | + source: sourceStart, |
| 73 | + output: outputStart |
| 74 | + }, |
| 75 | + end: { |
| 76 | + source: sourceEnd, |
| 77 | + output: outputEnd |
| 78 | + }, |
| 79 | + type, |
| 80 | + source: blockSource |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + const blocks = [ |
| 85 | + makeBlock('equal', 0, 0, 0, 0), |
| 86 | + ] |
| 87 | + |
| 88 | + function findInsert(originalShift: number, modifiedShift: number) { |
| 89 | + const modifiedShiftStart = modifiedShift |
| 90 | + |
| 91 | + while (originalShift < source.length && modifiedShift < output.length) { |
| 92 | + if (output[modifiedShift] === source[originalShift]) { |
| 93 | + break |
| 94 | + } |
| 95 | + |
| 96 | + modifiedShift++ |
| 97 | + } |
| 98 | + |
| 99 | + if (output.length < modifiedShiftStart) { |
| 100 | + return |
| 101 | + } |
| 102 | + |
| 103 | + return makeBlock('insert', originalShift, originalShift, modifiedShiftStart, modifiedShift) |
| 104 | + } |
| 105 | + |
| 106 | + function findDelete(originalShift: number, modifiedShift: number) { |
| 107 | + const originalShiftStart = originalShift |
| 108 | + |
| 109 | + while (originalShift < source.length && modifiedShift < output.length) { |
| 110 | + if (output[modifiedShift] === source[originalShift]) { |
| 111 | + return makeBlock('delete', originalShiftStart, originalShift, modifiedShift, modifiedShift) |
| 112 | + } |
| 113 | + |
| 114 | + originalShift++ |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + |
| 119 | + while (originalShift < source.length && modifiedShift < output.length) { |
| 120 | + const originalChar = source[originalShift] |
| 121 | + const modifiedChar = output[modifiedShift] |
| 122 | + |
| 123 | + const currentBlock = blocks[blocks.length - 1] |
| 124 | + |
| 125 | + if (originalChar === modifiedChar) { |
| 126 | + if (currentBlock.type === 'equal') { |
| 127 | + currentBlock.end.output++ |
| 128 | + currentBlock.end.source++ |
| 129 | + currentBlock.source = source.slice(currentBlock.start.source, currentBlock.end.source) |
| 130 | + } else { |
| 131 | + blocks.push(makeBlock('equal', originalShift, originalShift + 1, modifiedShift, modifiedShift + 1)) |
| 132 | + } |
| 133 | + originalShift++ |
| 134 | + modifiedShift++ |
| 135 | + } else { |
| 136 | + const nextChain = findDelete(originalShift, modifiedShift) |
| 137 | + const insertBlock = findInsert(originalShift, modifiedShift) |
| 138 | + |
| 139 | + if (nextChain && insertBlock) { |
| 140 | + // Prioritize insert over deleting something in output string |
| 141 | + if (output.slice(modifiedShift).includes(nextChain.source)) { |
| 142 | + blocks.push(insertBlock) |
| 143 | + modifiedShift = insertBlock.end.output |
| 144 | + continue |
| 145 | + } |
| 146 | + |
| 147 | + blocks.push(nextChain) |
| 148 | + originalShift = nextChain.end.source |
| 149 | + continue |
| 150 | + } |
| 151 | + if (insertBlock) { |
| 152 | + blocks.push(insertBlock) |
| 153 | + modifiedShift = insertBlock.end.output |
| 154 | + continue |
| 155 | + } |
| 156 | + if (nextChain) { |
| 157 | + blocks.push(nextChain) |
| 158 | + originalShift = nextChain.end.source |
| 159 | + continue |
| 160 | + } |
| 161 | + |
| 162 | + break |
| 163 | + } |
| 164 | + } |
| 165 | + |
| 166 | + return blocks |
| 167 | +} |
0 commit comments