MCPcopy Create free account
hub / github.com/Effect-TS/effect / hunks

Function hunks

packages/effect/src/TextDiff.ts:533–620  ·  view source on GitHub ↗
(edits: ReadonlyArray<Edit<string>>, options?: {
  /**
   * Number of unchanged lines kept around each change.
   *
   * @default 3
   */
  readonly context?: number | undefined
  /**
   * Whether paired lines carry the `ranges` that differ from their
   * counterpart, see {@link HunkLine}.
   *
   * @default false
   */
  readonly inline?: boolean | undefined
  /**
   * The `maxEdits` of the code point diff of each pair of lines, see
   * {@link codePoints}. Has no effect without `inline`.
   *
   * @default Infinity
   */
  readonly inlineMaxEdits?: number | undefined
})

Source from the content-addressed store, hash-verified

531 * @since 4.1.4
532 */
533export const hunks = (edits: ReadonlyArray<Edit<string>>, options?: {
534 /**
535 * Number of unchanged lines kept around each change.
536 *
537 * @default 3
538 */
539 readonly context?: number | undefined
540 /**
541 * Whether paired lines carry the `ranges` that differ from their
542 * counterpart, see {@link HunkLine}.
543 *
544 * @default false
545 */
546 readonly inline?: boolean | undefined
547 /**
548 * The `maxEdits` of the code point diff of each pair of lines, see
549 * {@link codePoints}. Has no effect without `inline`.
550 *
551 * @default Infinity
552 */
553 readonly inlineMaxEdits?: number | undefined
554}): Array<Hunk> => {
555 const a: Array<string> = []
556 const b: Array<string> = []
557 // Group consecutive deletions and insertions between Equal runs.
558 const changes: Array<Change> = []
559 let change: Change | undefined
560 for (const edit of edits) {
561 if (edit._tag === "Equal") {
562 change = undefined
563 for (const line of edit.items) {
564 a.push(line)
565 b.push(line)
566 }
567 continue
568 }
569 if (edit.items.length === 0) continue
570 if (change === undefined) {
571 change = { a0: a.length, a1: a.length, b0: b.length, b1: b.length }
572 changes.push(change)
573 }
574 if (edit._tag === "Delete") {
575 for (const line of edit.items) a.push(line)
576 change.a1 = a.length
577 } else {
578 for (const line of edit.items) b.push(line)
579 change.b1 = b.length
580 }
581 }
582
583 const context = Math.max(0, Math.floor(options?.context ?? 3))
584 const inline = options?.inline === true
585 const out: Array<Hunk> = []
586 let start = 0
587 while (start < changes.length) {
588 let end = start + 1
589 while (end < changes.length && changes[end].a0 - changes[end - 1].a1 <= 2 * context) end++
590 const first = changes[start]

Callers 1

unifiedFunction · 0.85

Calls 2

inlineRangesFunction · 0.85
pushMethod · 0.65

Tested by

no test coverage detected