@@ -135,19 +135,28 @@ 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+ const ignoreNum = { additions : 0 , deletions : 0 }
143145 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
146+ if ( change . added ) {
147+ const ignoreLines = ignoreCount ( change . value . trim ( ) . split ( '\n' ) )
148+ additionsNum += count ( change . value . trim ( ) , '\n' ) + 1 - ignoreLines
149+ ignoreNum . additions += ignoreLines
150+ continue
151+ }
152+ if ( change . removed ) {
153+ const ignoreLines = ignoreCount ( change . value . trim ( ) . split ( '\n' ) )
154+ deletionsNum += count ( change . value . trim ( ) , '\n' ) + 1 - ignoreLines
155+ ignoreNum . deletions += ignoreLines
156+ continue
157+ }
149158 }
150- return { additionsNum, deletionsNum }
159+ return { additionsNum, deletionsNum, ignoreNum }
151160}
152161
153162export function createSplitDiff (
@@ -156,10 +165,12 @@ export function createSplitDiff(
156165 language = 'plaintext' ,
157166 diffStyle = 'word' ,
158167 context = 10 ,
168+ ignoreMatchingLines ?: string ,
159169) : SplitViewerChange {
160170 const newEmptySplitDiff = ( ) : DiffLine => ( { type : DiffType . EMPTY } )
161171 const newSplitDiff = ( type : DiffType , num : number , code : string ) : DiffLine => ( { type, num, code } )
162172 const changes = diffLines ( oldString , newString )
173+ const ignoreRegex = ignoreMatchingLines ? new RegExp ( ignoreMatchingLines ) : undefined
163174
164175 let delNum = 0
165176 let addNum = 0
@@ -169,7 +180,7 @@ export function createSplitDiff(
169180 const result : SplitViewerChange = {
170181 changes : rawChanges ,
171182 collector : [ ] ,
172- stat : calcDiffStat ( changes ) ,
183+ stat : calcDiffStat ( changes , ignoreRegex ) ,
173184 }
174185
175186 for ( let i = 0 ; i < changes . length ; i ++ ) {
@@ -256,14 +267,17 @@ export function createSplitDiff(
256267
257268 const leftLine = curLines . length === nextLines . length ? renderWords ( nextLine , curLine , diffStyle ) : curLine
258269 const rightLine = curLines . length === nextLines . length ? renderWords ( curLine , nextLine , diffStyle ) : nextLine
270+ // 忽略匹配的行等价于相等
271+ const leftDiffType = ignoreRegex ?. test ( leftLine ) ? DiffType . EQUAL : DiffType . DELETE
272+ const rightDiffType = ignoreRegex ?. test ( rightLine ) ? DiffType . EQUAL : DiffType . ADD
259273
260274 const left
261275 = j < cur . count !
262- ? newSplitDiff ( DiffType . DELETE , delNum , getHighlightCode ( language , leftLine ) )
276+ ? newSplitDiff ( leftDiffType , delNum , getHighlightCode ( language , leftLine ) )
263277 : newEmptySplitDiff ( )
264278 const right
265279 = j < next . count !
266- ? newSplitDiff ( DiffType . ADD , addNum , getHighlightCode ( language , rightLine ) )
280+ ? newSplitDiff ( rightDiffType , addNum , getHighlightCode ( language , rightLine ) )
267281 : newEmptySplitDiff ( )
268282
269283 rawChanges . push ( { left, right } )
0 commit comments