From 594bd21c257512b639e30a39563464dab709206f Mon Sep 17 00:00:00 2001 From: Gatsik <74517072+Gatsik@users.noreply.github.com> Date: Fri, 18 Apr 2025 21:05:03 +0300 Subject: [PATCH] fix: respect dos line endings --- lua/conform/formatters/isort.lua | 12 +----------- lua/conform/runner.lua | 12 +++++++----- lua/conform/util.lua | 13 +++++++++++++ 3 files changed, 21 insertions(+), 16 deletions(-) diff --git a/lua/conform/formatters/isort.lua b/lua/conform/formatters/isort.lua index 72368736..c67b6c57 100644 --- a/lua/conform/formatters/isort.lua +++ b/lua/conform/formatters/isort.lua @@ -7,20 +7,10 @@ return { }, command = "isort", args = function(self, ctx) - -- isort doesn't do a good job of auto-detecting the line endings. - local line_ending - local file_format = vim.bo[ctx.buf].fileformat - if file_format == "dos" then - line_ending = "\r\n" - elseif file_format == "mac" then - line_ending = "\r" - else - line_ending = "\n" - end return { "--stdout", "--line-ending", - line_ending, + util.buf_line_ending(ctx.buf), "--filename", "$FILENAME", "-", diff --git a/lua/conform/runner.lua b/lua/conform/runner.lua index d409140b..e4b9afa2 100644 --- a/lua/conform/runner.lua +++ b/lua/conform/runner.lua @@ -122,7 +122,8 @@ local function create_text_edit( is_insert, is_replace, orig_line_start, - orig_line_end + orig_line_end, + line_ending ) local start_line, end_line = orig_line_start - 1, orig_line_end - 1 local start_char, end_char = 0, 0 @@ -152,7 +153,7 @@ local function create_text_edit( if is_insert and start_line < #original_lines then table.insert(replacement, "") end - local new_text = table.concat(replacement, "\n") + local new_text = table.concat(replacement, line_ending) return { newText = new_text, @@ -251,7 +252,8 @@ M.apply_format = function( is_insert, is_replace, orig_line_start, - orig_line_end + orig_line_end, + util.buf_line_ending(bufnr) ) table.insert(text_edits, text_edit) @@ -397,8 +399,8 @@ local function run_formatter(bufnr, formatter, config, ctx, input_lines, opts, c }, vim.schedule_wrap(function(result) local code = result.code - local stdout = result.stdout and vim.split(result.stdout, "\n") or {} - local stderr = result.stderr and vim.split(result.stderr, "\n") or {} + local stdout = result.stdout and vim.split(result.stdout, "\r?\n") or {} + local stderr = result.stderr and vim.split(result.stderr, "\r?\n") or {} if vim.tbl_contains(exit_codes, code) then local output = stdout if not config.stdin then diff --git a/lua/conform/util.lua b/lua/conform/util.lua index 786da19c..89923886 100644 --- a/lua/conform/util.lua +++ b/lua/conform/util.lua @@ -246,4 +246,17 @@ M.shell_build_argv = function(cmd) return argv end +---@param bufnr integer +---@return string +M.buf_line_ending = function(bufnr) + local fileformat = vim.bo[bufnr].fileformat + if fileformat == "dos" then + return "\r\n" + elseif fileformat == "mac" then + return "\r" + else + return "\n" + end +end + return M