From 9ae04a6f235c4c0fa6874dcb583de3546226bd76 Mon Sep 17 00:00:00 2001 From: Nicholas Buse Date: Wed, 14 Jun 2017 12:02:43 -0500 Subject: [PATCH] Utilizing Python's universal line endings and fixed logic. Fixes #9 --- DeleteBlankLines.py | 31 +++++++++++++++++++++---------- 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/DeleteBlankLines.py b/DeleteBlankLines.py index 404fbe5..079001a 100644 --- a/DeleteBlankLines.py +++ b/DeleteBlankLines.py @@ -37,16 +37,27 @@ def run( self, edit, surplus=False): def strip( self, edit, currentSelection, surplus ): # Convert the input range to a string, this represents the original selection. - tokens = { 'windows': '\r\n', - 'mac' : '\r', - 'unix' : '\n'} - line_endings = self.view.settings().get('default_line_ending') - rtnl = tokens.get(line_endings, '\n') - original = self.view.substr( currentSelection ) - lines = filter(None, map(lambda s: s.rstrip(), original.split(rtnl))) # strip the trailing spaces - if surplus: - rtnl *= 2 - output = rtnl.join(lines) + orig = self.view.substr(currentSelection) + lines = orig.splitlines() + + i = 0 + haveBlank = False + + while i < len(lines)-1: + if lines[i].rstrip() == '': + if not surplus or haveBlank: + del lines[i] + else: + i += 1 + haveBlank = True + else: + haveBlank = False + i += 1 + # END: if not surplus + # END: while + + output = '\n'.join(lines) + self.view.replace( edit, currentSelection, output ) return sublime.Region( currentSelection.begin(), currentSelection.begin() + len(output) )