@@ -215,6 +215,7 @@ end detailed_files
215215-- format conversion --
216216
217217constant kContent = "content"
218+ constant kAdditions = "additions"
218219constant kSource = "source"
219220constant kSourceLength = "sourceLength"
220221constant kContextLength = "contextLength"
@@ -270,6 +271,8 @@ private command begin_hunk @pHunk, pSource, pSourceLength, pContextLength
270271end begin_hunk
271272
272273private command end_hunk @pHunk
274+ flush_additions pHunk
275+
273276 if pHunk [kLastSourceIndex ] is not empty then
274277 add_context_before pHunk , pHunk [kLastSourceIndex ] + pHunk [kContextLength ] + 1
275278 end if
@@ -299,14 +302,22 @@ end add_deletion
299302
300303private command add_addition @pHunk, pIndex, pContent
301304 add_context_before pHunk , pIndex + 1
302- put "+" & pContent after pHunk [kContent ]
305+ // Defer adding additions to ensure they come after deletions
306+ put "+" & pContent after pHunk [kAdditions ]
303307 add 1 to pHunk [kDelta ]
304308
305309 if not (pContent ends with return ) then
306- put return & "\ No newline at end of file" & return after pHunk [kContent ]
310+ put return & "\ No newline at end of file" & return after pHunk [kAdditions ]
307311 end if
308312end add_addition
309313
314+ private command flush_additions @pHunk
315+ if pHunk [kAdditions ] is not empty then
316+ put pHunk [kAdditions ] after pHunk [kContent ]
317+ put empty into pHunk [kAdditions ]
318+ end if
319+ end flush_additions
320+
310321private command add_context_before @pHunk, pIndex
311322 local tStart , tEnd
312323 if pHunk [kLastSourceIndex ] is not empty then
@@ -327,6 +338,8 @@ private command add_context_before @pHunk, pIndex
327338 exit add_context_before
328339 end if
329340
341+ flush_additions pHunk
342+
330343 repeat with i = tStart to tEnd
331344 put space & pHunk [kSource ][i] after pHunk [kContent ]
332345 end repeat
0 commit comments