stevearc / conform.nvim

Lightweight yet powerful formatter plugin for Neovim
MIT License
2.58k stars 136 forks source link

bug: marks not being saved when using via formatexpr #388

Open dylanpinn opened 2 months ago

dylanpinn commented 2 months ago

Neovim version (nvim -v)

v0.9.5

Operating system/version

macOS 14.4.1

Add the debug logs

Log file

00:03:53[DEBUG] Running formatters on /Users/dylan/.dotfiles/nvim/after/plugin/conform.lua: { "stylua" }
00:03:53[INFO] Run stylua on /Users/dylan/.dotfiles/nvim/after/plugin/conform.lua
00:03:53[DEBUG] Run command: { "stylua", "--search-parent-directories", "--stdin-filepath", "/Users/dylan/.dotfiles/nvim/after/plugin/conform.lua", "-" }
00:03:53[DEBUG] Run CWD: /Users/dylan/.dotfiles
00:03:53[DEBUG] stylua exited with code 0

Describe the bug

When using Conform via the formatexpr, folds are not kept after using gq.

What is the severity of this bug?

minor (annoyance)

Steps To Reproduce

  1. nvim -u repro.lua sample.lua
  2. :5 - go to line 5
  3. mz -- mark current position as z
  4. gg -- go back to top of the file
  5. gqG -- format the file using formatexpr
  6. `z -- "Mark not set" error message

Expected Behavior

Should go back to mark z on line 5 after formatting file.

Minimal example file

local options = {
formatters_by_ft = {
lua = { "stylua" },
},
log_level = vim.log.levels.DEBUG,
}

require("conform").setup(options)

-- This should be okay to set globally, as will default back to the LSP
-- formatexpr if present and then to nothing.
vim.o.formatexpr = "v:lua.require'conform'.formatexpr()"

Minimal init.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.DEBUG,
        -- add your config here
    formatters_by_ft = {
      lua = { "stylua" },
    },
      })
    end,
  },
  -- add any other plugins here
}
require("lazy").setup(plugins, {
  root = root .. "/plugins",
})

vim.cmd.colorscheme("tokyonight")
-- add anything else here

vim.o.formatexpr = "v:lua.require'conform'.formatexpr()"

Additional context

No response

EdmundsEcho commented 1 month ago

I believe I'm experiencing something related to clearing the marks. When I save a file the view jumps to near the top of the buffer. In other words, updating the buffer with the formatted buffer changes my viewed position in the buffer.

PS: This is a great plugin that solves a configuration problem that I've had for years. Thank you!