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