Skip to content

Commit 49e696e

Browse files
committed
Improve "Sort lines" and "Reverse lines" commands
1 parent e4ad672 commit 49e696e

File tree

1 file changed

+22
-6
lines changed

1 file changed

+22
-6
lines changed

internal/application/actions.go

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -708,9 +708,17 @@ func handleSortLines() tview.Primitive {
708708
return nil
709709
}
710710
currentFileBuffer.editor.ActionController().TransformSelection(func(lines []string) []string {
711-
result := append([]string(nil), lines...)
712-
slices.Sort(result)
713-
return result
711+
lastLine := lines[len(lines)-1]
712+
if lastLine == "" { // Leave the last line out of the sort if it is zero length.
713+
result := append([]string(nil), lines[:len(lines)-1]...)
714+
slices.Sort(result)
715+
result = append(result, lastLine)
716+
return result
717+
} else {
718+
result := append([]string(nil), lines...)
719+
slices.Sort(result)
720+
return result
721+
}
714722
})
715723
return nil
716724
}
@@ -721,9 +729,17 @@ func handleReverseLines() tview.Primitive {
721729
return nil
722730
}
723731
currentFileBuffer.editor.ActionController().TransformSelection(func(lines []string) []string {
724-
result := append([]string(nil), lines...)
725-
slices.Reverse(result)
726-
return result
732+
lastLine := lines[len(lines)-1]
733+
if lastLine == "" { // Leave the last line out of the sort if it is zero length.
734+
result := append([]string(nil), lines[:len(lines)-1]...)
735+
slices.Reverse(result)
736+
result = append(result, lastLine)
737+
return result
738+
} else {
739+
result := append([]string(nil), lines...)
740+
slices.Reverse(result)
741+
return result
742+
}
727743
})
728744
return nil
729745
}

0 commit comments

Comments
 (0)