Skip to content

Commit

Permalink
Fix trailing newline (#1518)
Browse files Browse the repository at this point in the history
  • Loading branch information
Moosems authored Oct 18, 2024
1 parent 229d8cd commit b6ea2ac
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
3 changes: 2 additions & 1 deletion porcupine/plugins/trailing_newline.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
6 changes: 6 additions & 0 deletions tests/test_trailing_newline_plugin.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
def test_trailing_newline(filetab, tmp_path):
filetab.path = tmp_path / "foo.py"

assert filetab.textwidget.get("1.0", "end") == "\n"
filetab.save()
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"

Expand Down

0 comments on commit b6ea2ac

Please sign in to comment.