stevearc / conform.nvim

Lightweight yet powerful formatter plugin for Neovim
MIT License
2.72k stars 142 forks source link

when prettier is configed to use crlf eol, unexpected empty line is appended to the file after conform formatting #360

Closed li6in9muyou closed 3 months ago

li6in9muyou commented 4 months ago

Neovim version (nvim -v)

nvim 0.9.5

Operating system/version

Windows 11

Add the debug logs

Log file

before my fix

22:50:06[DEBUG] Running formatters on D:\projects\conform-prettier-crlf-reproduce-bug\index.html: { "prettier" }
22:50:06[INFO] Run prettier on D:\projects\conform-prettier-crlf-reproduce-bug\index.html
22:50:06[TRACE] Input lines: { "<body>", "  <div></div>", "</body>" }
22:50:06[DEBUG] Run command: { "prettier.cmd", "--stdin-filepath", "D:\\projects\\conform-prettier-crlf-reproduce-bug\\index.html" }
22:50:06[DEBUG] Run CWD: D:/projects/conform-prettier-crlf-reproduce-bug
22:50:06[DEBUG] prettier exited with code 0
22:50:06[TRACE] Output lines: { "<body>\r", "  <div></div>\r", "</body>\r" }
22:50:06[TRACE] prettier stderr: { "" }
22:50:06[TRACE] Applying formatting to D:\projects\conform-prettier-crlf-reproduce-bug\index.html
22:50:06[TRACE] Comparing lines { "<body>", "  <div></div>", "</body>" } and { "<body>\r", "  <div></div>\r", "</body>\r" }
22:50:06[TRACE] Diff indices { { 1, 3, 1, 3 } }
22:50:06[TRACE] Applying text edits: { {
    newText = "\r\n  <div></div>\r\n</body>\r",
    range = {
      ["end"] = {
        character = 7,
        line = 2
      },
      start = {
        character = 6,
        line = 0
      }
    }
  } }
22:50:06[TRACE] Done formatting D:\projects\conform-prettier-crlf-reproduce-bug\index.html

after my fix

22:50:03[DEBUG] Running formatters on D:\projects\conform-prettier-crlf-reproduce-bug\index.html: { "prettier" }
22:50:03[INFO] Run prettier on D:\projects\conform-prettier-crlf-reproduce-bug\index.html
22:50:03[TRACE] Input lines: { "<body>", "  <div></div>", "</body>" }
22:50:03[DEBUG] Run command: { "prettier.cmd", "--stdin-filepath", "D:\\projects\\conform-prettier-crlf-reproduce-bug\\index.html" }
22:50:03[DEBUG] Run CWD: D:/projects/conform-prettier-crlf-reproduce-bug
22:50:04[DEBUG] prettier exited with code 0
22:50:04[TRACE] Output lines: { "<body>", "  <div></div>", "</body>" }
22:50:04[TRACE] prettier stderr: { "" }
22:50:04[TRACE] Applying formatting to D:\projects\conform-prettier-crlf-reproduce-bug\index.html
22:50:04[TRACE] Comparing lines { "<body>", "  <div></div>", "</body>" } and { "<body>", "  <div></div>", "</body>" }
22:50:04[TRACE] Diff indices {}
22:50:04[TRACE] Applying text edits: {}
22:50:04[TRACE] Done formatting D:\projects\conform-prettier-crlf-reproduce-bug\index.html

Describe the bug

when format a html file with the prettier formatter, an empty line is unexpected appended to the file after running conform formatting.

What is the severity of this bug?

minor (annoyance)

Steps To Reproduce

  1. create a .prettierrc.json with content {"endOfLine": "crlf"}
  2. create a html fie with content <body>\r\n<div></div>\r\n</body>
  3. trigger a conform format with :w

Expected Behavior

Nothing should be changed by prettier however actual behaviour is that an empty line is append to the html file.

Minimal example file

### Minimal init.lua ```Lua -- DO NOT change the paths and don't remove the colorscheme local root = vim.fn.fnamemodify("./.repro", ":p") -- set stdpaths to use .repro for _, name in ipairs({ "config", "data", "state", "cache" }) do vim.env[("XDG_%s_HOME"):format(name:upper())] = root .. "/" .. name end -- bootstrap lazy local lazypath = root .. "/plugins/lazy.nvim" if not vim.loop.fs_stat(lazypath) then vim.fn.system({ "git", "clone", "--filter=blob:none", "--single-branch", "https://github.com/folke/lazy.nvim.git", lazypath, }) end vim.opt.runtimepath:prepend(lazypath) -- install plugins local plugins = { "folke/tokyonight.nvim", { "stevearc/conform.nvim", config = function() require("conform").setup({ log_level = vim.log.levels.TRACE, notify_on_error = false, format_on_save = function(bufnr) -- Disable "format_on_save lsp_fallback" for languages that don't -- have a well standardized coding style. You can add additional -- languages here or re-enable it for the disabled ones. local disable_filetypes = { c = true, cpp = true } return { timeout_ms = 500, lsp_fallback = not disable_filetypes[vim.bo[bufnr].filetype], } end, formatters_by_ft = { lua = { 'stylua' }, html = { 'prettier' }, -- Conform can also run multiple formatters sequentially -- python = { "isort", "black" }, -- -- You can use a sub-list to tell conform to run *until* a formatter -- is found. javascript = { 'prettier' }, typescript = { 'prettier' }, }, }) end, }, -- add any other plugins here } require("lazy").setup(plugins, { root = root .. "/plugins", }) vim.cmd.colorscheme("tokyonight") -- add anything else here ``` ### Additional context When fixing #274 in commit 9a785eb in file `lua/conform/runner.lua`, `\r\n` eol is handled in one branch of the if block so that no elements `output` end with `\r`. My fix ensures that this behaviour is consistent in both branches.
stevearc commented 3 months ago

Is this fixed by your PR?