From 80078196e036e2173c19886d14bfb6314d6e6f62 Mon Sep 17 00:00:00 2001 From: Humza Shahid Date: Wed, 24 Sep 2025 13:05:13 +0100 Subject: [PATCH] if 'MakeNormalDelete.deleteLine' deletes to the end of the file such that there is no newline at the end, then append a newline. This makes one of the tests we have pass. --- fcore/normal-mode/make-normal-delete.sml | 22 +++++++++++++++++++++- temp.txt | 1 + test/normal-delete.sml | 24 ++++++++++++++++++++++++ 3 files changed, 46 insertions(+), 1 deletion(-) diff --git a/fcore/normal-mode/make-normal-delete.sml b/fcore/normal-mode/make-normal-delete.sml index 8e5b20b..22e0e2f 100644 --- a/fcore/normal-mode/make-normal-delete.sml +++ b/fcore/normal-mode/make-normal-delete.sml @@ -354,11 +354,31 @@ struct val finishIdx = Cursor.viDlr (buffer, cursorIdx, count) + 2 val length = finishIdx - startIdx + val textLengthBeforeDelete = #textLength buffer + val buffer = LineGap.goToIdx (startIdx, buffer) val initialMsg = Fn.initMsgs (startIdx, length, buffer) val buffer = LineGap.delete (startIdx, length, buffer) in - finishAfterDeletingBuffer (app, startIdx, buffer, time, initialMsg) + if finishIdx >= textLengthBeforeDelete - 1 then + (* we deleted to the end of the buffer. + * Let's check if there is a newline at the end + * and insert one if there isn't. *) + let + val lastPosAfterDelete = #textLength buffer - 1 + val buffer = LineGap.goToIdx (lastPosAfterDelete, buffer) + val buffer = + if lastPosAfterDelete < 0 then + LineGap.append ("\n", buffer) + else if Cursor.isCursorAtStartOfLine (buffer, lastPosAfterDelete) then + buffer + else + LineGap.append ("\n", buffer) + in + finishAfterDeletingBuffer (app, startIdx, buffer, time, initialMsg) + end + else + finishAfterDeletingBuffer (app, startIdx, buffer, time, initialMsg) end fun finishDeleteLineBack (app, buffer, lineIdx, length, endOfLine, time) = diff --git a/temp.txt b/temp.txt index 94954ab..c100072 100644 --- a/temp.txt +++ b/temp.txt @@ -1,2 +1,3 @@ hello world + diff --git a/test/normal-delete.sml b/test/normal-delete.sml index 55a1f06..d27080c 100644 --- a/test/normal-delete.sml +++ b/test/normal-delete.sml @@ -312,6 +312,30 @@ struct in Expect.isTrue (stringIsExpected andalso cursorIdxIsExpected) end) + , test + "deletes when cursor is on first line and there are at least two lines" + (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, "dj") + + (* 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 "deletes first two lines when there are three lines \ \and cursor is on first line"