Only avoid the blank line at end of view if view is not editable

For editable views it is important to actually show the blank line so that we
can put the cursor there for typing.

This fixes problems with adding blank lines at the end of longer commit
messages.
This commit is contained in:
Stefan Haller committed 2025-01-20 17:52:53 +01:00
1 parent fe429c6184
commit 20d0b4316d
4 files changed
+59 -11

No files matched your search

+4 -2
View File
@@ -109,8 +109,10 @@ func ScanLinesAndTruncateWhenLongerThanBuffer(maxBufferSize int) func(data []byt
// - the line indices of the original lines, indexed by the wrapped line indices
// If wrap is false, the text is returned as is.
// This code needs to behave the same as `gocui.lineWrap` does.
func WrapViewLinesToWidth(wrap bool, text string, width int) ([]string, []int, []int) {
text = strings.TrimSuffix(text, "\n")
func WrapViewLinesToWidth(wrap bool, editable bool, text string, width int) ([]string, []int, []int) {
if !editable {
text = strings.TrimSuffix(text, "\n")
}
lines := strings.Split(text, "\n")
if !wrap {
indices := make([]int, len(lines))
+46 -1
View File
@@ -170,6 +170,7 @@ func TestWrapViewLinesToWidth(t *testing.T) {
tests := []struct {
name string
wrap bool
editable bool
text string
width int
expectedWrappedLines []string
@@ -378,10 +379,53 @@ func TestWrapViewLinesToWidth(t *testing.T) {
expectedWrappedLinesIndices: []int{0, 2, 6},
expectedOriginalLinesIndices: []int{0, 0, 1, 1, 1, 1, 2, 2},
},
{
name: "Avoid blank line at end if not editable",
wrap: true,
editable: false,
text: "First\nSecond\nThird\n",
width: 10,
expectedWrappedLines: []string{
"First",
"Second",
"Third",
},
expectedWrappedLinesIndices: []int{0, 1, 2},
expectedOriginalLinesIndices: []int{0, 1, 2},
},
{
name: "Avoid blank line at end if not editable",
wrap: true,
editable: false,
text: "First\nSecond\nThird\n",
width: 10,
expectedWrappedLines: []string{
"First",
"Second",
"Third",
},
expectedWrappedLinesIndices: []int{0, 1, 2},
expectedOriginalLinesIndices: []int{0, 1, 2},
},
{
name: "Keep blank line at end if editable",
wrap: true,
editable: true,
text: "First\nSecond\nThird\n",
width: 10,
expectedWrappedLines: []string{
"First",
"Second",
"Third",
"",
},
expectedWrappedLinesIndices: []int{0, 1, 2, 3},
expectedOriginalLinesIndices: []int{0, 1, 2, 3},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
wrappedLines, wrappedLinesIndices, originalLinesIndices := WrapViewLinesToWidth(tt.wrap, tt.text, tt.width)
wrappedLines, wrappedLinesIndices, originalLinesIndices := WrapViewLinesToWidth(tt.wrap, tt.editable, tt.text, tt.width)
assert.Equal(t, tt.expectedWrappedLines, wrappedLines)
if tt.expectedWrappedLinesIndices != nil {
assert.Equal(t, tt.expectedWrappedLinesIndices, wrappedLinesIndices)
@@ -394,6 +438,7 @@ func TestWrapViewLinesToWidth(t *testing.T) {
view := gocui.NewView("", 0, 0, tt.width+1, 1000, gocui.OutputNormal)
assert.Equal(t, tt.width, view.InnerWidth())
view.Wrap = tt.wrap
view.Editable = tt.editable
view.SetContent(tt.text)
assert.Equal(t, wrappedLines, view.ViewBufferLines())
})