From 11015dfa9e68b7db2157a346edd8f5f640ba48fe Mon Sep 17 00:00:00 2001 From: Humza Shahid Date: Thu, 25 Sep 2025 05:28:13 +0100 Subject: [PATCH] add two more test cases for 'dj' motion that were failing when added, and modify 'MakeNormalDelete.deleteLineDown' to handle them --- fcore/normal-mode/make-normal-delete.sml | 13 +++++- temp.txt | 3 +- test/normal-delete.sml | 50 ++++++++++++++++++++++++ 3 files changed, 64 insertions(+), 2 deletions(-) diff --git a/fcore/normal-mode/make-normal-delete.sml b/fcore/normal-mode/make-normal-delete.sml index 041a8e9..a533bdb 100644 --- a/fcore/normal-mode/make-normal-delete.sml +++ b/fcore/normal-mode/make-normal-delete.sml @@ -413,7 +413,18 @@ struct * to the first column of the now-last line *) val newEndIdx = #textLength buffer - 1 in - if startIdx >= newEndIdx then + if newEndIdx < 0 then + (* deleted whole file; add newline to the end *) + let val buffer = LineGap.append ("\n", buffer) + in finishAfterDeletingBuffer (app, 0, buffer, time, initialMsg) + end + else if newEndIdx = 0 then + (* there is only one char left in the file *) + finishAfterDeletingBuffer (app, 0, buffer, time, initialMsg) + else if startIdx >= newEndIdx then + (* deleted the last part of the file such that the cursor's idx + * now refers to an index that no longer exists. + * Have to move cursor to the last line of the file. *) let val buffer = LineGap.goToIdx (newEndIdx, buffer) in diff --git a/temp.txt b/temp.txt index c100072..7bd861d 100644 --- a/temp.txt +++ b/temp.txt @@ -1,3 +1,4 @@ hello world - +trello +brillo diff --git a/test/normal-delete.sml b/test/normal-delete.sml index 61ba542..00bfccc 100644 --- a/test/normal-delete.sml +++ b/test/normal-delete.sml @@ -386,6 +386,56 @@ struct in Expect.isTrue (stringIsExpected andalso cursorIdxIsExpected) end) + , test + "leaves a newline at the end when deleting the whole file \ + \and file ends with a newline" + (fn _ => + let + (* arrange *) + val originalString = "hello\nworld\n" + val originalIdx = 0 + + val app = TestUtils.init originalString + val app = AppWith.idx (app, originalIdx) + + (* act *) + val {cursorIdx, buffer, ...} = TestUtils.updateMany (app, "33dj") + + (* assert *) + val expectedString = "\n" + val actualString = LineGap.toString buffer + val stringIsExpected = expectedString = actualString + + val expectedCursorIdx = 0 + val cursorIdxIsExpected = expectedCursorIdx = cursorIdx + in + Expect.isTrue (stringIsExpected andalso cursorIdxIsExpected) + end) + , test + "leaves a newline at the end when deleting the whole file \ + \and file does not end with a newline" + (fn _ => + let + (* arrange *) + val originalString = "hello\nworld" + val originalIdx = 0 + + val app = TestUtils.init originalString + val app = AppWith.idx (app, originalIdx) + + (* act *) + val {cursorIdx, buffer, ...} = TestUtils.updateMany (app, "33dj") + + (* assert *) + val expectedString = "\n" + val actualString = LineGap.toString buffer + val stringIsExpected = expectedString = actualString + + val expectedCursorIdx = 0 + val cursorIdxIsExpected = expectedCursorIdx = cursorIdx + in + Expect.isTrue (stringIsExpected andalso cursorIdxIsExpected) + end) ] val tests = [dhDelete, dlDelete, djDelete]