From 8623e7b6e782583d35ecfca212a1b917c2558a48 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Wed, 9 Nov 2016 11:19:17 +0530 Subject: [PATCH] More tests for rewrap --- kitty/line-buf.c | 39 +++++++++++++++++++++++---------------- kitty_tests/datatypes.py | 17 ++++++++++++++--- 2 files changed, 37 insertions(+), 19 deletions(-) diff --git a/kitty/line-buf.c b/kitty/line-buf.c index c8b770dd3..2dcecbeea 100644 --- a/kitty/line-buf.c +++ b/kitty/line-buf.c @@ -76,7 +76,10 @@ new(PyTypeObject *type, PyObject *args, PyObject UNUSED *kwds) { static void dealloc(LineBuf* self) { - PyMem_Free(self->buf); PyMem_Free(self->line_map); PyMem_Free(self->continued_map); PyMem_Free(self->scratch); + PyMem_Free(self->buf); + PyMem_Free(self->line_map); + PyMem_Free(self->continued_map); + PyMem_Free(self->scratch); Py_CLEAR(self->line); Py_TYPE(self)->tp_free((PyObject*)self); } @@ -387,10 +390,11 @@ static Py_ssize_t rewrap_inner(nextlinefunc src_next, void *src_data, nextlinefu Line *src, *dest; Py_ssize_t src_x = src_col, dest_x = dest_xnum, num; src = src_next(src_data, oom); dest = dest_next(dest_data, oom); + bool prev_line_was_continued = false; while (src && dest) { - if (src_x == src->xnum) { + if (!prev_line_was_continued && src_x == src->xnum) { // Trim trailing whitespace - while(src_x && (src->chars[src_x - 1] & CHAR_MASK) != 32) src_x--; + while(src_x && (src->chars[src_x - 1] & CHAR_MASK) == 32) src_x--; } num = MIN(dest_x, src_x); if (num > 0) { @@ -398,19 +402,22 @@ static Py_ssize_t rewrap_inner(nextlinefunc src_next, void *src_data, nextlinefu copy_range(src, src_x, dest, dest_x, num); } if (src_x <= 0) { - if (!src->continued) { - // Hard break, start new line on dest - if (set_continued != NULL) set_continued(dest_data, false); - else dest->continued = false; - if (dest_x > 0) { - left_shift_line(src, 0, dest_x); - CLEAR_LINE(src, dest_xnum - dest_x, dest_x); - } - dest = dest_next(dest_data, oom); - dest_x = dest_xnum; - } + prev_line_was_continued = src->continued; src = src_next(src_data, oom); src_x = src_xnum; + if (!prev_line_was_continued) { + // Hard break, finalize this line + if (dest_x > 0) { // Left align dest line + left_shift_line(dest, 0, dest_x); + CLEAR_LINE(dest, dest_xnum - dest_x, dest_x); + } + if (src) { // Only start a new line if there is more in src + if (set_continued != NULL) set_continued(dest_data, false); + else dest->continued = false; + dest = dest_next(dest_data, oom); + } + dest_x = dest_xnum; + } } if (dest_x <= 0) { if (set_continued != NULL) set_continued(dest_data, true); @@ -483,8 +490,8 @@ rewrap(LineBuf *self, PyObject *val) { if (dest_y > 0) { // Shift lines up so that untouched lines are at the bottom - do_delete(other, dest_y, 0, other->ynum); - cursor_y = MAX(0, other->ynum - dest_y - 1); + do_delete(other, dest_y, 0, other->ynum - 1); + cursor_y = MAX(0, other->ynum - (dest_y + 1)); } else cursor_y = other->ynum - 1; if (src_y > 0 || (src_y == 0 && src_x > 0)) { diff --git a/kitty_tests/datatypes.py b/kitty_tests/datatypes.py index e6c5660dc..9b7978ff1 100644 --- a/kitty_tests/datatypes.py +++ b/kitty_tests/datatypes.py @@ -192,8 +192,8 @@ class TestDataTypes(BaseTest): l.set_char(0, 'x', 1, q) self.assertEqualAttributes(l.cursor_from(0), q) - def test_rewrap(self): - # Simple tests when xnum is unchanged + def test_rewrap_simple(self): + ' Same width buffers ' lb = filled_line_buf(5, 5) lb2 = LineBuf(lb.ynum, lb.xnum) lb.rewrap(lb2) @@ -204,13 +204,24 @@ class TestDataTypes(BaseTest): for i in range(lb2.ynum): self.ae(lb2.line(i), lb.line(i + 2)) lb2 = LineBuf(8, 5) - lb.rewrap(lb2) + cy = lb.rewrap(lb2)[1] + self.ae(cy, 4) for i in range(lb.ynum): self.ae(lb2.line(i), lb.line(i)) empty = LineBuf(1, lb2.xnum) for i in range(lb.ynum, lb2.ynum): self.ae(str(lb2.line(i)), str(empty.line(0))) + def test_rewrap_wider(self): + ' New buffer wider ' + lb = LineBuf(5, 5) + lb.line(0).set_text('01234', 0, 5, C()) + lb.line(1).set_text('56789', 0, 5, C()) + lb.set_continued(1, True) + lb2 = LineBuf(2, 6) + lb.rewrap(lb2) + self.ae(str(lb2.line(1)), '456789') + def test_utils(self): d = codecs.getincrementaldecoder('utf-8')('strict').decode self.ae(tuple(map(wcwidth, 'a1\0コ')), (1, 1, 0, 2))