@@ -134,38 +134,65 @@ func (self *patchTransformer) transformHunkLines(hunk *Hunk, firstLineIdx int) [
134134 }
135135 isLineSelected := lo .Contains (self .opts .IncludedLineIndices , lineIdx )
136136
137- // Check for change block (multiple consecutive deletions)
137+ // Handle change block (multiple consecutive deletions followed by additions )
138138 if line .Kind == DELETION {
139139 deletionCount := 0
140140 for j := i ; j < len (hunk .bodyLines ) && hunk .bodyLines [j ].Kind == DELETION ; j ++ {
141141 deletionCount ++
142142 }
143143 if deletionCount > 1 {
144144 selectedDeletions := []* PatchLine {}
145- pendingContext := []* PatchLine {}
145+ leadingContext := []* PatchLine {}
146+ trailingContext := []* PatchLine {}
147+ foundFirstSelectedDeletion := false
148+
146149 for j := 0 ; j < deletionCount ; j ++ {
147150 delLine := hunk .bodyLines [i + j ]
148151 delLineIdx := i + j + firstLineIdx + 1
149152 if lo .Contains (self .opts .IncludedLineIndices , delLineIdx ) {
153+ foundFirstSelectedDeletion = true
150154 selectedDeletions = append (selectedDeletions , delLine )
151- } else if (delLine .Kind == DELETION && ! self .opts .Reverse ) || (delLine .Kind == ADDITION && self .opts .Reverse ) {
152- pendingContext = append (pendingContext , & PatchLine {Kind : CONTEXT , Content : " " + delLine .Content [1 :]})
155+ } else if ! self .opts .Reverse {
156+ ctx := & PatchLine {Kind : CONTEXT , Content : " " + delLine .Content [1 :]}
157+ if foundFirstSelectedDeletion {
158+ trailingContext = append (trailingContext , ctx )
159+ } else {
160+ leadingContext = append (leadingContext , ctx )
161+ }
153162 }
154163 }
155- newLines = append (newLines , selectedDeletions ... )
156164
157165 // Process additions in the block
166+ selectedAdditions := []* PatchLine {}
167+ trailingAdditionContext := []* PatchLine {}
168+ foundFirstSelectedAddition := false
158169 addIdx := i + deletionCount
159170 for addIdx < len (hunk .bodyLines ) && hunk .bodyLines [addIdx ].Kind == ADDITION {
171+ addLine := hunk .bodyLines [addIdx ]
160172 addLineIdx := addIdx + firstLineIdx + 1
161173 if lo .Contains (self .opts .IncludedLineIndices , addLineIdx ) {
162- newLines = append (newLines , hunk .bodyLines [addIdx ])
174+ foundFirstSelectedAddition = true
175+ selectedAdditions = append (selectedAdditions , addLine )
176+ } else if self .opts .Reverse {
177+ ctx := & PatchLine {Kind : CONTEXT , Content : " " + addLine .Content [1 :]}
178+ if foundFirstSelectedAddition {
179+ trailingAdditionContext = append (trailingAdditionContext , ctx )
180+ }
181+ // In reverse mode, unselected additions before any selected addition are dropped.
182+ // Unselected additions after a selected addition become context.
163183 } else {
164184 skippedNewlineMessageIndex = addLineIdx + 1
165185 }
166186 addIdx ++
167187 }
168- newLines = append (newLines , pendingContext ... )
188+
189+ // Output order: leading context, deletions, additions, trailing context, trailing addition context
190+ newLines = append (newLines , leadingContext ... )
191+ newLines = append (newLines , selectedDeletions ... )
192+ newLines = append (newLines , selectedAdditions ... )
193+ newLines = append (newLines , trailingContext ... )
194+ newLines = append (newLines , trailingAdditionContext ... )
195+
169196 i = addIdx - 1
170197 continue
171198 }
0 commit comments