Skip to content

Commit

Permalink
Utilizing Python's universal line endings and fixed logic. Fixes #9
Browse files Browse the repository at this point in the history
  • Loading branch information
NicholasBuse committed Jun 14, 2017
1 parent 3317721 commit 9ae04a6
Showing 1 changed file with 21 additions and 10 deletions.
31 changes: 21 additions & 10 deletions DeleteBlankLines.py
Original file line number Diff line number Diff line change
Expand Up @@ -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) )

0 comments on commit 9ae04a6

Please sign in to comment.