| 398 | // it declares. The rest are compared in order where each list has as many, |
| 399 | // and as one run of tokens otherwise |
| 400 | const diff = (base: ModuleTokens, other: ModuleTokens): Hunk[] => { |
| 401 | const hunks: Hunk[] = [] |
| 402 | const compare = (b0: number, b1: number, o0: number, o1: number) => |
| 403 | hunks.push( |
| 404 | ...diffOf( |
| 405 | base.texts, |
| 406 | other.texts, |
| 407 | ...tokensIn(base, b0, b1), |
| 408 | ...tokensIn(other, o0, o1) |
| 409 | ) |
| 410 | ) |
| 411 | const compareEnds = (b: number, o: number) => { |
| 412 | const end = base.statements[b][1] |
| 413 | const otherEnd = other.statements[o][1] |
| 414 | const ends = base.texts[end - 1] === ";" |
| 415 | if (ends === (other.texts[otherEnd - 1] === ";")) return |
| 416 | hunks.push( |
| 417 | ends ? |
| 418 | [end - 1, end, otherEnd, otherEnd] |
| 419 | : [end, end, otherEnd - 1, otherEnd] |
| 420 | ) |
| 421 | } |
| 422 | const heads = base.statements.map(headOf(base)) |
| 423 | const otherHeads = other.statements.map(headOf(other)) |
| 424 | let [b, o] = [0, 0] |
| 425 | for (const [b0, b1, o0, o1] of [ |
| 426 | ...diffOf( |
| 427 | base.statements.map(keyOf(base)), |
| 428 | other.statements.map(keyOf(other)) |
| 429 | ), |
| 430 | [base.statements.length, base.statements.length, 0, 0] |
| 431 | ]) { |
| 432 | for (; b < b0; b++, o++) compareEnds(b, o) |
| 433 | for (const [h0, h1, g0, g1] of [ |
| 434 | ...diffOf(heads, otherHeads, b0, b1, o0, o1), |
| 435 | [b1, b1, o1, o1] |
| 436 | ]) { |
| 437 | for (; b < h0; b++, o++) compare(b, b + 1, o, o + 1) |
| 438 | if (h1 - h0 !== g1 - g0) compare(h0, h1, g0, g1) |
| 439 | else for (; b < h1; b++, o++) compare(b, b + 1, o, o + 1) |
| 440 | b = h1 |
| 441 | o = g1 |
| 442 | } |
| 443 | } |
| 444 | return hunks |
| 445 | } |
| 446 | |
| 447 | // the tokens of a module's statements, from one to before another |
| 448 | const tokensIn = ( |