MCPcopy Create free account
hub / github.com/vitest-dev/vitest / joinAlignedDiffsNoExpand

Function joinAlignedDiffsNoExpand

packages/utils/src/diff/joinAlignedDiffs.ts:115–293  ·  view source on GitHub ↗
(
  diffs: Array<Diff>,
  options: DiffOptionsNormalized,
)

Source from the content-addressed store, hash-verified

113// Given array of aligned strings with inverse highlight formatting,
114// return joined lines with diff formatting (and patch marks, if needed).
115export function joinAlignedDiffsNoExpand(
116 diffs: Array<Diff>,
117 options: DiffOptionsNormalized,
118): string {
119 const iLength = diffs.length
120 const nContextLines = options.contextLines
121 const nContextLines2 = nContextLines + nContextLines
122
123 // First pass: count output lines and see if it has patches.
124 let jLength = iLength
125 let hasExcessAtStartOrEnd = false
126 let nExcessesBetweenChanges = 0
127 let i = 0
128 while (i !== iLength) {
129 const iStart = i
130 while (i !== iLength && diffs[i][0] === DIFF_EQUAL) {
131 i += 1
132 }
133
134 if (iStart !== i) {
135 if (iStart === 0) {
136 // at start
137 if (i > nContextLines) {
138 jLength -= i - nContextLines // subtract excess common lines
139 hasExcessAtStartOrEnd = true
140 }
141 }
142 else if (i === iLength) {
143 // at end
144 const n = i - iStart
145 if (n > nContextLines) {
146 jLength -= n - nContextLines // subtract excess common lines
147 hasExcessAtStartOrEnd = true
148 }
149 }
150 else {
151 // between changes
152 const n = i - iStart
153 if (n > nContextLines2) {
154 jLength -= n - nContextLines2 // subtract excess common lines
155 nExcessesBetweenChanges += 1
156 }
157 }
158 }
159
160 while (i !== iLength && diffs[i][0] !== DIFF_EQUAL) {
161 i += 1
162 }
163 }
164
165 const hasPatch = nExcessesBetweenChanges !== 0 || hasExcessAtStartOrEnd
166 if (nExcessesBetweenChanges !== 0) {
167 jLength += nExcessesBetweenChanges + 1
168 } // add patch lines
169 else if (hasExcessAtStartOrEnd) {
170 jLength += 1
171 } // add patch line
172

Callers 1

printDiffLinesFunction · 0.90

Calls 4

pushCommonLineFunction · 0.85
createPatchMarkFunction · 0.85
pushDeleteLineFunction · 0.85
pushInsertLineFunction · 0.85

Tested by

no test coverage detected