@@ -135,19 +135,34 @@ function getHighlightCode(language: string, code: string) {
135135 . replace ( new RegExp ( closeEntity , 'g' ) , '</span>' )
136136}
137137
138- function calcDiffStat ( changes : Change [ ] ) : DiffStat {
138+ function calcDiffStat ( changes : Change [ ] , ignoreRegex ?: RegExp ) : DiffStat {
139139 const count = ( s : string , c : string ) => ( s . match ( new RegExp ( c , 'g' ) ) || [ ] ) . length
140+ const ignoreCount = ( lines : string [ ] ) => lines . filter ( line => ignoreRegex ?. test ( line ) ) . length
140141
141142 let additionsNum = 0
142143 let deletionsNum = 0
144+ let ignoreAdditionsNum = 0
145+ let ignoreDeletionsNum = 0
143146 for ( const change of changes ) {
144- if ( change . added )
145- additionsNum += count ( change . value . trim ( ) , '\n' ) + 1
146-
147- if ( change . removed )
148- deletionsNum += count ( change . value . trim ( ) , '\n' ) + 1
147+ if ( change . added ) {
148+ const ignoreNum = ignoreCount ( change . value . trim ( ) . split ( '\n' ) )
149+ additionsNum += count ( change . value . trim ( ) , '\n' ) + 1 - ignoreNum
150+ ignoreAdditionsNum += ignoreNum
151+ continue
152+ }
153+ if ( change . removed ) {
154+ const ignoreNum = ignoreCount ( change . value . trim ( ) . split ( '\n' ) )
155+ deletionsNum += count ( change . value . trim ( ) , '\n' ) + 1 - ignoreNum
156+ ignoreDeletionsNum += ignoreNum
157+ continue
158+ }
159+ }
160+ return {
161+ additionsNum,
162+ deletionsNum,
163+ ignoreAdditionsNum,
164+ ignoreDeletionsNum,
149165 }
150- return { additionsNum, deletionsNum }
151166}
152167
153168export function createSplitDiff (
@@ -156,10 +171,12 @@ export function createSplitDiff(
156171 language = 'plaintext' ,
157172 diffStyle = 'word' ,
158173 context = 10 ,
174+ ignoreMatchingLines ?: string ,
159175) : SplitViewerChange {
160176 const newEmptySplitDiff = ( ) : DiffLine => ( { type : DiffType . EMPTY } )
161177 const newSplitDiff = ( type : DiffType , num : number , code : string ) : DiffLine => ( { type, num, code } )
162178 const changes = diffLines ( oldString , newString )
179+ const ignoreRegex = ignoreMatchingLines ? new RegExp ( ignoreMatchingLines ) : undefined
163180
164181 let delNum = 0
165182 let addNum = 0
@@ -169,7 +186,7 @@ export function createSplitDiff(
169186 const result : SplitViewerChange = {
170187 changes : rawChanges ,
171188 collector : [ ] ,
172- stat : calcDiffStat ( changes ) ,
189+ stat : calcDiffStat ( changes , ignoreRegex ) ,
173190 }
174191
175192 for ( let i = 0 ; i < changes . length ; i ++ ) {
@@ -257,13 +274,17 @@ export function createSplitDiff(
257274 const leftLine = curLines . length === nextLines . length ? renderWords ( nextLine , curLine , diffStyle ) : curLine
258275 const rightLine = curLines . length === nextLines . length ? renderWords ( curLine , nextLine , diffStyle ) : nextLine
259276
277+ // 忽略匹配的行等价于相等
278+ const leftDiffType = ignoreRegex ?. test ( curLine ) ? DiffType . EQUAL : DiffType . DELETE
279+ const rightDiffType = ignoreRegex ?. test ( nextLine ) ? DiffType . EQUAL : DiffType . ADD
280+
260281 const left
261282 = j < cur . count !
262- ? newSplitDiff ( DiffType . DELETE , delNum , getHighlightCode ( language , leftLine ) )
283+ ? newSplitDiff ( leftDiffType , delNum , getHighlightCode ( language , leftLine ) )
263284 : newEmptySplitDiff ( )
264285 const right
265286 = j < next . count !
266- ? newSplitDiff ( DiffType . ADD , addNum , getHighlightCode ( language , rightLine ) )
287+ ? newSplitDiff ( rightDiffType , addNum , getHighlightCode ( language , rightLine ) )
267288 : newEmptySplitDiff ( )
268289
269290 rawChanges . push ( { left, right } )
@@ -341,8 +362,10 @@ export function createUnifiedDiff(
341362 language = 'plaintext' ,
342363 diffStyle = 'word' ,
343364 context = 10 ,
365+ ignoreMatchingLines ?: string ,
344366) : UnifiedViewerChange {
345367 const changes = diffLines ( oldString , newString )
368+ const ignoreRegex = ignoreMatchingLines ? new RegExp ( ignoreMatchingLines ) : undefined
346369
347370 let delNum = 0
348371 let addNum = 0
@@ -352,7 +375,7 @@ export function createUnifiedDiff(
352375 const result : UnifiedViewerChange = {
353376 changes : rawChanges ,
354377 collector : [ ] ,
355- stat : calcDiffStat ( changes ) ,
378+ stat : calcDiffStat ( changes , ignoreRegex ) ,
356379 }
357380
358381 for ( let i = 0 ; i < changes . length ; i ++ ) {
@@ -414,7 +437,11 @@ export function createUnifiedDiff(
414437 delNum ++
415438
416439 const code = getHighlightCode ( language , renderWords ( nextLine , curLine , diffStyle ) )
417- rawChanges . push ( { type : DiffType . DELETE , code, delNum } )
440+ rawChanges . push ( {
441+ type : ignoreRegex ?. test ( curLine ) ? DiffType . EQUAL : DiffType . DELETE ,
442+ code,
443+ delNum,
444+ } )
418445 }
419446
420447 for ( let j = 0 ; j < nextLines . length ; j ++ ) {
@@ -423,7 +450,11 @@ export function createUnifiedDiff(
423450 addNum ++
424451
425452 const code = getHighlightCode ( language , renderWords ( curLine , nextLine , diffStyle ) )
426- rawChanges . push ( { type : DiffType . ADD , code, addNum } )
453+ rawChanges . push ( {
454+ type : ignoreRegex ?. test ( nextLine ) ? DiffType . EQUAL : DiffType . ADD ,
455+ code,
456+ addNum,
457+ } )
427458 }
428459
429460 skip = true
0 commit comments