Skip to content

fix: respect dos line endings #691

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 1 addition & 11 deletions lua/conform/formatters/isort.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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",
"-",
Expand Down
12 changes: 7 additions & 5 deletions lua/conform/runner.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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
Expand Down
13 changes: 13 additions & 0 deletions lua/conform/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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