@@ -8,15 +8,15 @@ public partial class PatchGenerator
88 [ GeneratedRegex ( @"^@@ \-(\d+),?\d* \+(\d+),?\d* @@" ) ]
99 private static partial Regex REG_INDICATOR ( ) ;
1010
11- public PatchGenerator ( string saveTo , DiffOption option , TextDiff diff )
11+ public bool IsValid => _selection != null ;
12+
13+ public PatchGenerator ( DiffOption option , TextDiff diff , int startLine , int endLine , bool isCombined , bool isOldSide )
1214 {
13- _saveTo = saveTo ;
1415 _targetFile = option . Path ;
1516 _diff = diff ;
16- }
17+ _isCombined = isCombined ;
18+ _isOldSide = isOldSide ;
1719
18- public bool Generate ( int startLine , int endLine , bool isCombined , bool isOldSide , bool revert )
19- {
2020 var lines = _diff . Lines ;
2121 var hasChanges = false ;
2222
@@ -54,50 +54,65 @@ public bool Generate(int startLine, int endLine, bool isCombined, bool isOldSide
5454 }
5555 }
5656
57- if ( ! hasChanges )
58- return false ;
57+ if ( hasChanges )
58+ _selection = selection ;
59+ }
5960
60- if ( isCombined )
61- GenerateCombined ( selection , revert ) ;
62- else
63- GenerateForSingleSide ( selection , revert , isOldSide ) ;
61+ public void Generate ( string saveTo , bool revert )
62+ {
63+ if ( _selection == null )
64+ return ;
6465
65- return true ;
66+ using var writer = CreateWriter ( saveTo , revert ) ;
67+
68+ if ( _isCombined )
69+ GenerateCombined ( writer , revert ) ;
70+ else
71+ GenerateForSingleSide ( writer , revert ) ;
6672 }
6773
68- private void GenerateCombined ( Selection selection , bool revert )
74+ private void GenerateCombined ( StreamWriter writer , bool revert )
6975 {
70- using var writer = CreateWriter ( revert ) ;
7176 var lines = _diff . Lines ;
7277
7378 // If last line of selection is a change. Find one more line.
7479 TextDiffLine tail = null ;
75- if ( selection . EndLine < lines . Count - 1 )
80+ if ( _selection . EndLine < lines . Count - 1 )
7681 {
77- var lastLine = lines [ selection . EndLine ] ;
82+ var lastLine = lines [ _selection . EndLine ] ;
7883 if ( lastLine . Type == TextDiffLineType . Added || lastLine . Type == TextDiffLineType . Deleted )
7984 {
80- for ( int i = selection . EndLine + 1 ; i < lines . Count ; i ++ )
85+ for ( int i = _selection . EndLine + 1 ; i < lines . Count ; i ++ )
8186 {
8287 var line = lines [ i ] ;
8388 if ( line . Type == TextDiffLineType . Indicator )
8489 break ;
85- if ( line . Type == TextDiffLineType . Normal ||
86- ( revert && line . Type == TextDiffLineType . Added ) ||
87- ( ! revert && line . Type == TextDiffLineType . Deleted ) )
90+
91+ if ( revert )
8892 {
89- tail = line ;
90- break ;
93+ if ( line . Type == TextDiffLineType . Normal || line . Type == TextDiffLineType . Added )
94+ {
95+ tail = line ;
96+ break ;
97+ }
98+ }
99+ else
100+ {
101+ if ( line . Type == TextDiffLineType . Normal || line . Type == TextDiffLineType . Deleted )
102+ {
103+ tail = line ;
104+ break ;
105+ }
91106 }
92107 }
93108 }
94109 }
95110
96111 // If the first line is not indicator.
97- if ( lines [ selection . StartLine ] . Type != TextDiffLineType . Indicator )
112+ if ( lines [ _selection . StartLine ] . Type != TextDiffLineType . Indicator )
98113 {
99- var indicator = selection . StartLine ;
100- for ( int i = selection . StartLine - 1 ; i >= 0 ; i -- )
114+ var indicator = _selection . StartLine ;
115+ for ( int i = _selection . StartLine - 1 ; i >= 0 ; i -- )
101116 {
102117 var line = lines [ i ] ;
103118 if ( line . Type == TextDiffLineType . Indicator )
@@ -122,12 +137,12 @@ private void GenerateCombined(Selection selection, bool revert)
122137 }
123138 }
124139
125- for ( int i = indicator ; i < selection . StartLine ; i ++ )
140+ for ( int i = indicator ; i < _selection . StartLine ; i ++ )
126141 {
127142 var line = lines [ i ] ;
128143 if ( line . Type == TextDiffLineType . Indicator )
129144 {
130- ProcessIndicator ( writer , line , i , selection . StartLine , selection . EndLine , ignoreRemoves , ignoreAdds , revert , tail != null ) ;
145+ ProcessIndicator ( writer , line , i , ignoreRemoves , ignoreAdds , revert , tail != null ) ;
131146 }
132147 else if ( line . Type == TextDiffLineType . Added )
133148 {
@@ -147,12 +162,12 @@ private void GenerateCombined(Selection selection, bool revert)
147162 }
148163
149164 // Outputs the selected lines.
150- for ( int i = selection . StartLine ; i <= selection . EndLine ; i ++ )
165+ for ( int i = _selection . StartLine ; i <= _selection . EndLine ; i ++ )
151166 {
152167 var line = lines [ i ] ;
153168 if ( line . Type == TextDiffLineType . Indicator )
154169 {
155- if ( ! ProcessIndicator ( writer , line , i , selection . StartLine , selection . EndLine , selection . IgnoredDeletes , selection . IgnoredAdds , revert , tail != null ) )
170+ if ( ! ProcessIndicator ( writer , line , i , _selection . IgnoredDeletes , _selection . IgnoredAdds , revert , tail != null ) )
156171 break ;
157172 }
158173 else if ( line . Type == TextDiffLineType . Normal )
@@ -173,23 +188,23 @@ private void GenerateCombined(Selection selection, bool revert)
173188 Append ( writer , ' ' , tail ) ;
174189 }
175190
176- private void GenerateForSingleSide ( Selection selection , bool revert , bool isOldSide )
191+ private void GenerateForSingleSide ( StreamWriter writer , bool revert )
177192 {
178- using var writer = CreateWriter ( revert ) ;
179193 var lines = _diff . Lines ;
180194
181195 // If last line of selection is a change. Find one more line.
182196 TextDiffLine tail = null ;
183- if ( selection . EndLine < lines . Count - 1 )
197+ if ( _selection . EndLine < lines . Count - 1 )
184198 {
185- var lastLine = lines [ selection . EndLine ] ;
199+ var lastLine = lines [ _selection . EndLine ] ;
186200 if ( lastLine . Type == TextDiffLineType . Added || lastLine . Type == TextDiffLineType . Deleted )
187201 {
188- for ( int i = selection . EndLine + 1 ; i < lines . Count ; i ++ )
202+ for ( int i = _selection . EndLine + 1 ; i < lines . Count ; i ++ )
189203 {
190204 var line = lines [ i ] ;
191205 if ( line . Type == TextDiffLineType . Indicator )
192206 break ;
207+
193208 if ( revert )
194209 {
195210 if ( line . Type == TextDiffLineType . Normal || line . Type == TextDiffLineType . Added )
@@ -211,10 +226,10 @@ private void GenerateForSingleSide(Selection selection, bool revert, bool isOldS
211226 }
212227
213228 // If the first line is not indicator.
214- if ( lines [ selection . StartLine ] . Type != TextDiffLineType . Indicator )
229+ if ( lines [ _selection . StartLine ] . Type != TextDiffLineType . Indicator )
215230 {
216- var indicator = selection . StartLine ;
217- for ( int i = selection . StartLine - 1 ; i >= 0 ; i -- )
231+ var indicator = _selection . StartLine ;
232+ for ( int i = _selection . StartLine - 1 ; i >= 0 ; i -- )
218233 {
219234 var line = lines [ i ] ;
220235 if ( line . Type == TextDiffLineType . Indicator )
@@ -239,12 +254,12 @@ private void GenerateForSingleSide(Selection selection, bool revert, bool isOldS
239254 }
240255 }
241256
242- for ( int i = indicator ; i < selection . StartLine ; i ++ )
257+ for ( int i = indicator ; i < _selection . StartLine ; i ++ )
243258 {
244259 var line = lines [ i ] ;
245260 if ( line . Type == TextDiffLineType . Indicator )
246261 {
247- ProcessIndicatorForSingleSide ( writer , line , i , selection . StartLine , selection . EndLine , ignoreRemoves , ignoreAdds , revert , isOldSide , tail != null ) ;
262+ ProcessIndicatorForSingleSide ( writer , line , i , ignoreRemoves , ignoreAdds , revert , tail != null ) ;
248263 }
249264 else if ( line . Type == TextDiffLineType . Added )
250265 {
@@ -264,12 +279,12 @@ private void GenerateForSingleSide(Selection selection, bool revert, bool isOldS
264279 }
265280
266281 // Outputs the selected lines.
267- for ( int i = selection . StartLine ; i <= selection . EndLine ; i ++ )
282+ for ( int i = _selection . StartLine ; i <= _selection . EndLine ; i ++ )
268283 {
269284 var line = lines [ i ] ;
270285 if ( line . Type == TextDiffLineType . Indicator )
271286 {
272- if ( ! ProcessIndicatorForSingleSide ( writer , line , i , selection . StartLine , selection . EndLine , selection . IgnoredDeletes , selection . IgnoredAdds , revert , isOldSide , tail != null ) )
287+ if ( ! ProcessIndicatorForSingleSide ( writer , line , i , _selection . IgnoredDeletes , _selection . IgnoredAdds , revert , tail != null ) )
273288 break ;
274289 }
275290 else if ( line . Type == TextDiffLineType . Normal )
@@ -278,12 +293,12 @@ private void GenerateForSingleSide(Selection selection, bool revert, bool isOldS
278293 }
279294 else if ( line . Type == TextDiffLineType . Added )
280295 {
281- if ( isOldSide )
296+ if ( _isOldSide )
282297 {
283298 if ( revert )
284299 Append ( writer , ' ' , line ) ;
285300 else
286- selection . IgnoredAdds ++ ;
301+ _selection . IgnoredAdds ++ ;
287302 }
288303 else
289304 {
@@ -292,7 +307,7 @@ private void GenerateForSingleSide(Selection selection, bool revert, bool isOldS
292307 }
293308 else if ( line . Type == TextDiffLineType . Deleted )
294309 {
295- if ( isOldSide )
310+ if ( _isOldSide )
296311 {
297312 Append ( writer , '-' , line ) ;
298313 }
@@ -301,7 +316,7 @@ private void GenerateForSingleSide(Selection selection, bool revert, bool isOldS
301316 if ( ! revert )
302317 Append ( writer , ' ' , line ) ;
303318 else
304- selection . IgnoredDeletes ++ ;
319+ _selection . IgnoredDeletes ++ ;
305320 }
306321 }
307322 }
@@ -310,9 +325,9 @@ private void GenerateForSingleSide(Selection selection, bool revert, bool isOldS
310325 Append ( writer , ' ' , tail ) ;
311326 }
312327
313- private StreamWriter CreateWriter ( bool revert )
328+ private StreamWriter CreateWriter ( string saveTo , bool revert )
314329 {
315- var writer = new StreamWriter ( _saveTo ) { NewLine = "\n " } ;
330+ var writer = new StreamWriter ( saveTo ) { NewLine = "\n " } ;
316331 writer . WriteLine ( $ "diff --git \" a/{ _targetFile } \" \" b/{ _targetFile } \" ") ;
317332
318333 if ( string . IsNullOrEmpty ( _diff . OldMode ) )
@@ -354,14 +369,17 @@ private void Append(StreamWriter writer, char prefix, TextDiffLine line)
354369 writer . WriteLine ( "\\ No newline at end of file" ) ;
355370 }
356371
357- private bool ProcessIndicator ( StreamWriter writer , TextDiffLine indicator , int idx , int start , int end , int ignoreRemoves , int ignoreAdds , bool revert , bool tailed )
372+ private bool ProcessIndicator ( StreamWriter writer , TextDiffLine indicator , int idx , int ignoreRemoves , int ignoreAdds , bool revert , bool tailed )
358373 {
359374 var lines = _diff . Lines ;
375+ var start = _selection . StartLine ;
376+ var end = _selection . EndLine ;
360377 var match = REG_INDICATOR ( ) . Match ( indicator . Content ) ;
361378 var oldStart = int . Parse ( match . Groups [ 1 ] . Value ) ;
362379 var newStart = int . Parse ( match . Groups [ 2 ] . Value ) + ignoreRemoves - ignoreAdds ;
363380 var oldCount = 0 ;
364381 var newCount = 0 ;
382+
365383 for ( int i = idx + 1 ; i <= end ; i ++ )
366384 {
367385 var test = lines [ i ] ;
@@ -424,14 +442,17 @@ private bool ProcessIndicator(StreamWriter writer, TextDiffLine indicator, int i
424442 return true ;
425443 }
426444
427- private bool ProcessIndicatorForSingleSide ( StreamWriter writer , TextDiffLine indicator , int idx , int start , int end , int ignoreRemoves , int ignoreAdds , bool revert , bool isOldSide , bool tailed )
445+ private bool ProcessIndicatorForSingleSide ( StreamWriter writer , TextDiffLine indicator , int idx , int ignoreRemoves , int ignoreAdds , bool revert , bool tailed )
428446 {
429447 var lines = _diff . Lines ;
448+ var start = _selection . StartLine ;
449+ var end = _selection . EndLine ;
430450 var match = REG_INDICATOR ( ) . Match ( indicator . Content ) ;
431451 var oldStart = int . Parse ( match . Groups [ 1 ] . Value ) ;
432452 var newStart = int . Parse ( match . Groups [ 2 ] . Value ) + ignoreRemoves - ignoreAdds ;
433453 var oldCount = 0 ;
434454 var newCount = 0 ;
455+
435456 for ( int i = idx + 1 ; i <= end ; i ++ )
436457 {
437458 var test = lines [ i ] ;
@@ -445,7 +466,7 @@ private bool ProcessIndicatorForSingleSide(StreamWriter writer, TextDiffLine ind
445466 }
446467 else if ( test . Type == TextDiffLineType . Added )
447468 {
448- if ( i < start || isOldSide )
469+ if ( i < start || _isOldSide )
449470 {
450471 if ( revert )
451472 {
@@ -476,7 +497,7 @@ private bool ProcessIndicatorForSingleSide(StreamWriter writer, TextDiffLine ind
476497 }
477498 else
478499 {
479- if ( isOldSide )
500+ if ( _isOldSide )
480501 {
481502 oldCount ++ ;
482503 }
@@ -513,8 +534,10 @@ private class Selection
513534 public int IgnoredDeletes { get ; set ; } = 0 ;
514535 }
515536
516- private string _saveTo = null ;
517537 private string _targetFile = null ;
518538 private TextDiff _diff = null ;
539+ private bool _isCombined = false ;
540+ private bool _isOldSide = false ;
541+ private Selection _selection = null ;
519542 }
520543}
0 commit comments