Skip to content

Commit 930a1d4

Browse files
authored
fix(editline): no newline if cursor within range on last line (#131)
If the cursor was at the very end of a string, but not past the width, it would still insert an extra line when formatting.
1 parent 82f333d commit 930a1d4

File tree

2 files changed

+106
-10
lines changed

2 files changed

+106
-10
lines changed

spec/09-editline-format_spec.lua

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -640,6 +640,43 @@ describe("EditLine:", function()
640640
end)
641641

642642

643+
it("doesn't add new line if cursor is at the end of a non-oversized last line", function()
644+
-- single line
645+
line = EditLine("some test data"):goto_end()
646+
assert.are.same({
647+
{
648+
'some test data|'
649+
},
650+
1,
651+
15,
652+
}, {testwrap({
653+
width = 20,
654+
first_width = 20,
655+
wordwrap = false,
656+
pad = false,
657+
pad_last = false
658+
})})
659+
660+
-- again with multiple lines
661+
line = EditLine("sometestda"):goto_end()
662+
assert.are.same({
663+
{
664+
'some|',
665+
'test|',
666+
'da|',
667+
},
668+
3,
669+
3,
670+
}, {testwrap({
671+
width = 4,
672+
first_width = 4,
673+
wordwrap = false,
674+
pad = false,
675+
pad_last = false
676+
})})
677+
end)
678+
679+
643680
it("doesn't add the extra newline if set to do so", function()
644681
line = EditLine("sometestdata"):goto_end()
645682
assert.are.same({
@@ -935,6 +972,43 @@ describe("EditLine:", function()
935972
end)
936973

937974

975+
it("doesn't add new line if cursor is at the end of a non-oversized last line", function()
976+
-- single line
977+
line = EditLine("some test data"):goto_end()
978+
assert.are.same({
979+
{
980+
'some test data|'
981+
},
982+
1,
983+
15,
984+
}, {testwrap({
985+
width = 60,
986+
first_width = 20,
987+
wordwrap = true,
988+
pad = false,
989+
pad_last = false
990+
})})
991+
992+
-- again with multiple lines
993+
line = EditLine("somexx testxx data"):goto_end()
994+
assert.are.same({
995+
{
996+
'somexx |',
997+
'testxx |',
998+
'data|',
999+
},
1000+
3,
1001+
5,
1002+
}, {testwrap({
1003+
width = 8,
1004+
first_width = 8,
1005+
wordwrap = true,
1006+
pad = false,
1007+
pad_last = false
1008+
})})
1009+
end)
1010+
1011+
9381012
it("doesn't add the extra newline if set to do so", function()
9391013
line = EditLine("some testdata"):goto_end()
9401014
assert.are.same({

src/terminal/editline.lua

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -603,18 +603,40 @@ do
603603
target_size = width -- update target size after first-line
604604
end
605605

606-
-- add extra line holding cursor, if cursor is at the end of the last line at exact length
607-
-- (so the cursor is beyond the width set, and is the only thing on the next line)
606+
-- did we set the cursor position? (not set if the cursor is at the very end)
608607
if cur_line == nil then
609-
if no_new_cursor_line then
610-
-- no new line, so cursor is at the end of the last line, beyond the width
611-
cur_line = #lines
612-
cur_col = width + 1
608+
-- if the last line isn't at full length, then we have the cursor in a regular position
609+
-- at the end of that line
610+
if #lines == 1 then
611+
local l = lines[1]:len_col()
612+
if l < first_width then
613+
-- cursor is on a first-line, on a regular position
614+
cur_line = 1
615+
cur_col = l + 1
616+
end
613617
else
614-
-- add an empty new line just to hold the cursor
615-
cur_line = #lines + 1
616-
lines[cur_line] = EditLine("")
617-
cur_col = 1
618+
local l = lines[#lines]:len_col()
619+
if l < width then
620+
-- cursor is on a second or later line, on a regular position
621+
cur_line = #lines
622+
cur_col = l + 1
623+
end
624+
end
625+
626+
-- wasn't on a regular position on the line, so past the end.
627+
-- add extra line holding cursor, if cursor is at the end of the last line at exact length
628+
-- (so the cursor is beyond the width set, and is the only thing on the next line)
629+
if cur_line == nil then
630+
if no_new_cursor_line then
631+
-- no new line, so cursor is at the end of the last line, beyond the width
632+
cur_line = #lines
633+
cur_col = width + 1
634+
else
635+
-- add an empty new line just to hold the cursor
636+
cur_line = #lines + 1
637+
lines[cur_line] = EditLine("")
638+
cur_col = 1
639+
end
618640
end
619641
end
620642

0 commit comments

Comments
 (0)