Solve returns a DiffSolution. Internally it uses solveRange to find all matching regions, * then it uses the standard differ to create diffs on the intra-region area.
()
| 232 | * then it uses the standard differ to create diffs on the intra-region area. |
| 233 | */ |
| 234 | solve(): DiffPair[] { |
| 235 | const joinLines = (lines: string[], start: number, end: number) => { |
| 236 | if (start >= end) { |
| 237 | return ""; |
| 238 | } |
| 239 | |
| 240 | const s = lines.slice(start, end).join("\n"); |
| 241 | return s + "\n"; |
| 242 | }; |
| 243 | const lineLen = (line: string) => (line === "" ? 0 : Math.max(1, line.length - 1)); |
| 244 | |
| 245 | let apos = 0; |
| 246 | let bpos = 0; |
| 247 | let prevRegion = new MatchRegion(0, 0, 0, 0, 0); |
| 248 | // Find common lines. |
| 249 | const regions = this.fineSolveRegion(this.solveRange(0, this.aa.length, 0, this.bb.length)); |
| 250 | const pairs: DiffPair[] = []; |
| 251 | |
| 252 | // Calculate block differences. |
| 253 | for (let i = 0; i <= regions.length; i++) { |
| 254 | const region = regions[i]; |
| 255 | const aEnd = region ? region.aStart : this.aa.length; |
| 256 | const bEnd = region ? region.bStart : this.bb.length; |
| 257 | const aaStr = joinLines(this.aa, prevRegion.aEnd, aEnd); |
| 258 | const bbStr = joinLines(this.bb, prevRegion.bEnd, bEnd); |
| 259 | const aDiff = newDiff(apos, lineLen(aaStr), "del"); |
| 260 | const bDiff = newDiff(bpos, lineLen(bbStr), "ins"); |
| 261 | |
| 262 | if (aDiff.length > 0 || bDiff.length > 0) { |
| 263 | pairs.push({ |
| 264 | left: aDiff.length > 0 ? aDiff : undefined, |
| 265 | right: bDiff.length > 0 ? bDiff : undefined, |
| 266 | }); |
| 267 | } |
| 268 | |
| 269 | // Perform inline comparison after removing leading and trailing whitespace. |
| 270 | if (aaStr.length > 0 && bbStr.length > 0) { |
| 271 | const { left, right } = classify(myersDiff(aaStr, bbStr, { maxEditLength: 100 })); |
| 272 | left.forEach((d) => (d.offset += aDiff.offset)); |
| 273 | right.forEach((d) => (d.offset += bDiff.offset)); |
| 274 | aDiff.inlineDiffs = left; |
| 275 | bDiff.inlineDiffs = right; |
| 276 | } |
| 277 | |
| 278 | if (region) { |
| 279 | // Equal parts. Because trim is performed, the length of the equal parts needs to be calculated separately. |
| 280 | const aaComm = joinLines(this.aa, region.aStart, region.aEnd); |
| 281 | const bbComm = joinLines(this.bb, region.bStart, region.bEnd); |
| 282 | apos += aaStr.length + aaComm.length; |
| 283 | bpos += bbStr.length + bbComm.length; |
| 284 | prevRegion = region; |
| 285 | } |
| 286 | } |
| 287 | |
| 288 | return pairs; |
| 289 | } |
| 290 | } |
| 291 |
no test coverage detected