Also take different eols into account for removing last empty line

This commit is contained in:
Chiel ten Brinke 2015-10-29 17:25:39 +01:00
parent 735d709e84
commit 1243463143

View File

@ -146,17 +146,20 @@ if stderrdata:
print('Failing config: {} '.format(repr(formatprg), stderrdata))
vim.command('return 0')
else:
# It is not certain what kind of line endings are being used by the format program.
# Therefore we simply split on all possible eol characters.
possible_eols = ['\r\n', os.linesep, '\r', '\n']
# Often shell commands will append a newline at the end of their output.
# It is not entirely clear when and why that happens.
# However, extra newlines are almost never required, while there are linters that complain
# about superfluous newlines, so we remove one empty newline at the end of the file.
if stdoutdata[-1] == os.linesep:
stdoutdata = stdoutdata[:-1]
for eol in possible_eols:
if stdoutdata[-1] == eol:
stdoutdata = stdoutdata[:-1]
# It is not certain what kind of line endings are being used by the format program.
# Therefore we simply split on all possible eol characters.
lines = [stdoutdata]
for eol in ['\r\n', '\r', '\n', os.linesep]:
for eol in possible_eols:
lines = [splitline for line in lines for splitline in line.split(eol)]
vim.current.buffer[:] = lines
@ -191,19 +194,22 @@ if stderrdata:
print('Failing config: {} '.format(repr(formatprg), stderrdata))
vim.command('return 0')
else:
# It is not certain what kind of line endings are being used by the format program.
# Therefore we simply split on all possible eol characters.
possible_eols = ['\r\n', os.linesep, '\r', '\n']
stdoutdata = stdoutdata.decode('utf-8')
# Often shell commands will append a newline at the end of their output.
# It is not entirely clear when and why that happens.
# However, extra newlines are almost never required, while there are linters that complain
# about superfluous newlines, so we remove one empty newline at the end of the file.
if stdoutdata[-1] == os.linesep:
stdoutdata = stdoutdata[:-1]
for eol in possible_eols:
if stdoutdata[-1] == eol:
stdoutdata = stdoutdata[:-1]
# It is not certain what kind of line endings are being used by the format program.
# Therefore we simply split on all possible eol characters.
lines = [stdoutdata]
for eol in ['\r\n', '\r', '\n', os.linesep]:
for eol in possible_eols:
lines = [splitline for line in lines for splitline in line.split(eol)]
vim.current.buffer[:] = lines