From d5c237c4ba93c4da8e885dcdfb6b1d8588c5d9ab Mon Sep 17 00:00:00 2001 From: "Michael D. Lowis" Date: Thu, 22 Dec 2016 16:51:33 -0500 Subject: [PATCH] Fixed infinite loop in indent/unindent code --- TODO.md | 1 - libedit/view.c | 6 ++++-- tests/xedit.c | 25 +++++++++++++------------ 3 files changed, 17 insertions(+), 15 deletions(-) diff --git a/TODO.md b/TODO.md index 37149b6..f277b74 100644 --- a/TODO.md +++ b/TODO.md @@ -3,7 +3,6 @@ Up Next: * Tag line count should account for wrapped lines -* Indent/Unindent on first line of buffer causes infinite loop * block selection should handle brace-balancing * context sensitive selection of words, commands, line numbers, or filenames. * ctrl+f should move the pointer to the match diff --git a/libedit/view.c b/libedit/view.c index f25822d..3740a41 100644 --- a/libedit/view.c +++ b/libedit/view.c @@ -588,7 +588,8 @@ void view_indent(View* view, int dir) { view->selection.end = buf_eol(buf, view->selection.end); unsigned off = buf_bol(buf, view->selection.end); if (num_selected(view->selection) == 0) return; - while (off >= view->selection.beg) { + + do { if (dir == RIGHT) { buf_insert(buf, true, off, '\t'); view->selection.end += indoff; @@ -608,5 +609,6 @@ void view_indent(View* view, int dir) { } } off = buf_byline(buf, off, UP); - } + + } while (off && off >= view->selection.beg); } diff --git a/tests/xedit.c b/tests/xedit.c index c4d76d3..6e18753 100644 --- a/tests/xedit.c +++ b/tests/xedit.c @@ -483,12 +483,12 @@ TEST_SUITE(XeditTests) { CHECK(getsel(EDIT)->end == 0); } - //TEST(ctrl+[ should do nothing on empty buffer) { - // setup_view(EDIT, "a", CRLF, 0); - // send_keys(ModCtrl, '['); - // CHECK(getsel(EDIT)->beg == 0); - // CHECK(getsel(EDIT)->end == 0); - //} + TEST(ctrl+[ should do nothing at beginning of buffer) { + setup_view(EDIT, "a", CRLF, 0); + send_keys(ModCtrl, '['); + CHECK(getsel(EDIT)->beg == 0); + CHECK(getsel(EDIT)->end == 1); + } TEST(ctrl+] should do nothing on empty buffer) { setup_view(EDIT, "", CRLF, 0); @@ -497,12 +497,13 @@ TEST_SUITE(XeditTests) { CHECK(getsel(EDIT)->end == 0); } - //TEST(ctrl+] should indent the current line) { - // setup_view(EDIT, "a", CRLF, 0); - // send_keys(ModCtrl, ']'); - // CHECK(getsel(EDIT)->beg == 0); - // CHECK(getsel(EDIT)->end == 0); - //} + TEST(ctrl+] should indent the first line) { + setup_view(EDIT, "a", CRLF, 0); + send_keys(ModCtrl, ']'); + CHECK(getsel(EDIT)->beg == 0); + CHECK(getsel(EDIT)->end == 5); + CHECK(verify_text(EDIT, " a")); + } /* Key Handling - Special *************************************************************************/ -- 2.49.0