From 8a740ab186cfb4c1ac99aa51fcb23837a760ebc6 Mon Sep 17 00:00:00 2001 From: Chiel ten Brinke Date: Wed, 28 Oct 2015 13:57:22 +0100 Subject: [PATCH 1/4] Try all possible new line characters to split on. --- plugin/autoformat.vim | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/plugin/autoformat.vim b/plugin/autoformat.vim index 9e1c47e..1875aa3 100644 --- a/plugin/autoformat.vim +++ b/plugin/autoformat.vim @@ -152,7 +152,14 @@ else: # about superfluous newlines, so we remove one empty newline at the end of the file. if stdoutdata[-1] == os.linesep: stdoutdata = stdoutdata[:-1] - vim.current.buffer[:] = stdoutdata.split(os.linesep) + + # 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]: + lines = [splitline for line in lines for splitline in line.split(eol)] + + vim.current.buffer[:] = lines EOF return 1 @@ -190,7 +197,16 @@ else: # about superfluous newlines, so we remove one empty newline at the end of the file. if stdoutdata[-1] == os.linesep: stdoutdata = stdoutdata[:-1] - vim.current.buffer[:] = stdoutdata.split(os.linesep) + + stdoutdata = stdoutdata.decode('utf-8') + + # 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 [b'\r\n', b'\r', b'\n', os.linesep.decode('utf-8')]: + lines = [splitline for line in lines for splitline in line.split(eol)] + + vim.current.buffer[:] = lines EOF return 1 From e6ecfff004915cc94de62545a99d79e78e867edd Mon Sep 17 00:00:00 2001 From: Chiel ten Brinke Date: Wed, 28 Oct 2015 13:59:03 +0100 Subject: [PATCH 2/4] Merge master. --- plugin/autoformat.vim | 1 - 1 file changed, 1 deletion(-) diff --git a/plugin/autoformat.vim b/plugin/autoformat.vim index ba04ed5..1875aa3 100644 --- a/plugin/autoformat.vim +++ b/plugin/autoformat.vim @@ -195,7 +195,6 @@ else: # 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. - stdoutdata = stdoutdata.decode('utf-8') if stdoutdata[-1] == os.linesep: stdoutdata = stdoutdata[:-1] From 735d709e84951aeb2879876373133931da297a95 Mon Sep 17 00:00:00 2001 From: Chiel ten Brinke Date: Thu, 29 Oct 2015 12:22:48 +0100 Subject: [PATCH 3/4] Fix python3 support. --- plugin/autoformat.vim | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/plugin/autoformat.vim b/plugin/autoformat.vim index 1875aa3..d958782 100644 --- a/plugin/autoformat.vim +++ b/plugin/autoformat.vim @@ -191,6 +191,8 @@ if stderrdata: print('Failing config: {} '.format(repr(formatprg), stderrdata)) vim.command('return 0') else: + 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 @@ -198,12 +200,10 @@ else: if stdoutdata[-1] == os.linesep: stdoutdata = stdoutdata[:-1] - stdoutdata = stdoutdata.decode('utf-8') - # 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 [b'\r\n', b'\r', b'\n', os.linesep.decode('utf-8')]: + for eol in ['\r\n', '\r', '\n', os.linesep]: lines = [splitline for line in lines for splitline in line.split(eol)] vim.current.buffer[:] = lines From 12434631437a02f35f772bd438471aa9822c66a3 Mon Sep 17 00:00:00 2001 From: Chiel ten Brinke Date: Thu, 29 Oct 2015 17:25:39 +0100 Subject: [PATCH 4/4] Also take different eols into account for removing last empty line --- plugin/autoformat.vim | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/plugin/autoformat.vim b/plugin/autoformat.vim index d958782..e91b341 100644 --- a/plugin/autoformat.vim +++ b/plugin/autoformat.vim @@ -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