From 1ca7486dc6b8d4a3d23c65c64f7512df012e7b92 Mon Sep 17 00:00:00 2001 From: Moosems <95927277+Moosems@users.noreply.github.com> Date: Fri, 18 Oct 2024 05:33:35 -0600 Subject: [PATCH 1/4] Check that the file has a character to reference --- porcupine/plugins/trailing_newline.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/porcupine/plugins/trailing_newline.py b/porcupine/plugins/trailing_newline.py index bdbc1103d..da9509738 100644 --- a/porcupine/plugins/trailing_newline.py +++ b/porcupine/plugins/trailing_newline.py @@ -9,7 +9,8 @@ def on_save(event: tkinter.Event[tabs.FileTab]) -> None: if event.widget.settings.get("insert_final_newline", bool): textwidget = event.widget.textwidget - if textwidget.get("end - 2 chars", "end - 1 char") != "\n": + char = textwidget.get("end - 2 chars", "end - 1 char") + if char and char != "\n": # doesn't end with a \n yet, be sure not to annoyingly move the # cursor like IDLE does cursor = textwidget.index("insert") From fff2d2a2fb489788b524c281f4aa8034482365f5 Mon Sep 17 00:00:00 2001 From: Moosems <95927277+Moosems@users.noreply.github.com> Date: Fri, 18 Oct 2024 05:39:41 -0600 Subject: [PATCH 2/4] Update tests --- tests/test_trailing_newline_plugin.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tests/test_trailing_newline_plugin.py b/tests/test_trailing_newline_plugin.py index 29178583b..3d0877002 100644 --- a/tests/test_trailing_newline_plugin.py +++ b/tests/test_trailing_newline_plugin.py @@ -1,5 +1,9 @@ def test_trailing_newline(filetab, tmp_path): filetab.path = tmp_path / "foo.py" + + filetab.save() + assert (tmp_path / "foo.py").read_text() == "\n" + filetab.textwidget.insert("1.0", "hello") assert filetab.textwidget.get("1.0", "end - 1 char") == "hello" From 2ad8ab2c84c5ba0d45f0c97844f7809fd59dabc8 Mon Sep 17 00:00:00 2001 From: Moosems <95927277+Moosems@users.noreply.github.com> Date: Fri, 18 Oct 2024 05:50:26 -0600 Subject: [PATCH 3/4] Fix test For some reason saving a file with just a newline simply becomes completely empty (guess it is kinda redundant) --- tests/test_trailing_newline_plugin.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/test_trailing_newline_plugin.py b/tests/test_trailing_newline_plugin.py index 3d0877002..a61071410 100644 --- a/tests/test_trailing_newline_plugin.py +++ b/tests/test_trailing_newline_plugin.py @@ -1,8 +1,10 @@ def test_trailing_newline(filetab, tmp_path): filetab.path = tmp_path / "foo.py" + assert filetab.textwidget.get("1.0", "end ") == "\n" filetab.save() - assert (tmp_path / "foo.py").read_text() == "\n" + assert filetab.textwidget.get("1.0", "end ") == "\n" + assert (tmp_path / "foo.py").read_text() == "" filetab.textwidget.insert("1.0", "hello") assert filetab.textwidget.get("1.0", "end - 1 char") == "hello" From aeed11fefc449f6e0a12aa4ed69ea44dfb7040c3 Mon Sep 17 00:00:00 2001 From: Moosems <95927277+Moosems@users.noreply.github.com> Date: Fri, 18 Oct 2024 05:51:01 -0600 Subject: [PATCH 4/4] Remove the space in "end " --- tests/test_trailing_newline_plugin.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/test_trailing_newline_plugin.py b/tests/test_trailing_newline_plugin.py index a61071410..bd9cc5746 100644 --- a/tests/test_trailing_newline_plugin.py +++ b/tests/test_trailing_newline_plugin.py @@ -1,9 +1,9 @@ def test_trailing_newline(filetab, tmp_path): filetab.path = tmp_path / "foo.py" - assert filetab.textwidget.get("1.0", "end ") == "\n" + assert filetab.textwidget.get("1.0", "end") == "\n" filetab.save() - assert filetab.textwidget.get("1.0", "end ") == "\n" + assert filetab.textwidget.get("1.0", "end") == "\n" assert (tmp_path / "foo.py").read_text() == "" filetab.textwidget.insert("1.0", "hello")