@@ -135,19 +135,28 @@ function getHighlightCode(language: string, code: string) {
135
135
. replace ( new RegExp ( closeEntity , 'g' ) , '</span>' )
136
136
}
137
137
138
- function calcDiffStat ( changes : Change [ ] ) : DiffStat {
138
+ function calcDiffStat ( changes : Change [ ] , ignoreRegex ?: RegExp ) : DiffStat {
139
139
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
140
141
141
142
let additionsNum = 0
142
143
let deletionsNum = 0
144
+ const ignoreNum = { additions : 0 , deletions : 0 }
143
145
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
+ }
149
158
}
150
- return { additionsNum, deletionsNum }
159
+ return { additionsNum, deletionsNum, ignoreNum }
151
160
}
152
161
153
162
export function createSplitDiff (
@@ -156,10 +165,12 @@ export function createSplitDiff(
156
165
language = 'plaintext' ,
157
166
diffStyle = 'word' ,
158
167
context = 10 ,
168
+ ignoreMatchingLines ?: string ,
159
169
) : SplitViewerChange {
160
170
const newEmptySplitDiff = ( ) : DiffLine => ( { type : DiffType . EMPTY } )
161
171
const newSplitDiff = ( type : DiffType , num : number , code : string ) : DiffLine => ( { type, num, code } )
162
172
const changes = diffLines ( oldString , newString )
173
+ const ignoreRegex = ignoreMatchingLines ? new RegExp ( ignoreMatchingLines ) : undefined
163
174
164
175
let delNum = 0
165
176
let addNum = 0
@@ -169,7 +180,7 @@ export function createSplitDiff(
169
180
const result : SplitViewerChange = {
170
181
changes : rawChanges ,
171
182
collector : [ ] ,
172
- stat : calcDiffStat ( changes ) ,
183
+ stat : calcDiffStat ( changes , ignoreRegex ) ,
173
184
}
174
185
175
186
for ( let i = 0 ; i < changes . length ; i ++ ) {
@@ -256,14 +267,17 @@ export function createSplitDiff(
256
267
257
268
const leftLine = curLines . length === nextLines . length ? renderWords ( nextLine , curLine , diffStyle ) : curLine
258
269
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
259
273
260
274
const left
261
275
= j < cur . count !
262
- ? newSplitDiff ( DiffType . DELETE , delNum , getHighlightCode ( language , leftLine ) )
276
+ ? newSplitDiff ( leftDiffType , delNum , getHighlightCode ( language , leftLine ) )
263
277
: newEmptySplitDiff ( )
264
278
const right
265
279
= j < next . count !
266
- ? newSplitDiff ( DiffType . ADD , addNum , getHighlightCode ( language , rightLine ) )
280
+ ? newSplitDiff ( rightDiffType , addNum , getHighlightCode ( language , rightLine ) )
267
281
: newEmptySplitDiff ( )
268
282
269
283
rawChanges . push ( { left, right } )
0 commit comments