Skip to content

Whole-file run (no selection) drops the file's final newline #32

Description

@dpc00

Running "Remove Duplicate Lines" without a selection (whole file) removes the file's final newline.

dedupe() does lines = self.view.substr(selection).splitlines() and text = "\n".join(OrderedDict.fromkeys(lines)); splitlines() drops the trailing line break and "\n".join() doesn't add one back, so a buffer that ended with \n no longer does.

Reproduced in Sublime Text 4200 (Windows 11), RemoveDuplicateLines 2.1.1 from Package Control, on a scratch view, no selection:

'a\nb\na\nc\nb\n'  ->  'a\nb\nc'        # expected 'a\nb\nc\n'
'a\nb\na'          ->  'a\nb'           # (no final newline in, none out: fine)

Most text files (and Git) expect a final newline, so the command turns a clean file into "\ No newline at end of file" in diffs. (Selections that include the final newline of their last line behave correctly, e.g. the first three lines of the buffer above keep their newline.)

Fix: remember whether the region ended with a line break (self.view.substr(selection).endswith(("\n", "\r"))) and append "\n" to text in that case (or use split("\n") semantics instead of splitlines()).

Found while testing packages from the Package Control catalog.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions