nvimdev / guard.nvim

async fast minimalist plugin make format easy in neovim
MIT License
448 stars 24 forks source link

Range formatting by clang-format return wrong indent #146

Closed Sologala closed 5 months ago

Sologala commented 5 months ago

Guard.nvim version 81a0995f07cc370fbf15d6d03abc4b1f8651d23f neovim version : NVIM v0.9.4

keymap("n", "<leader>F", "<cmd>GuardFmt<cr>", opts)
keymap('v', '<leader>f', "<cmd>GuardFmt<CR>", opts)

Reproduce steps:

  1. press v to get into visual mode, and move cursor to select target lines.
  2. press <leader>f to trigger guardfmt The result are recorded as following: Peek 2024-05-04 16-32

The guard config:

{
        "nvimdev/guard.nvim",
        commit = "81a0995f07cc370fbf15d6d03abc4b1f8651d23f",
        -- Builtin configuration, optional
        dependencies = {
            "nvimdev/guard-collection",
        },
        config = function()
            local ft = require('guard.filetype')
            local guard = require("guard")
            ft('python'):fmt('autopep8')
            ft('lua'):fmt("lsp")
            ft('cmake'):fmt({
                cmd = "cmake-format",
                args = { "-" },
                stdio = true,
            })
            ft('c,cpp'):fmt({
                cmd = 'clang-format',
                stdin = true,
                args = {
                    [[--style={BasedOnStyle: Microsoft,
                    AlignConsecutiveAssignments: true,
                    AlignConsecutiveDeclarations: true,
                    AlignEscapedNewlines: Right,
                    AlignTrailingComments: true,
                    AllowShortBlocksOnASingleLine: false,
                    AlwaysBreakBeforeMultilineStrings: false,
                    AlwaysBreakTemplateDeclarations: Yes,
                    IndentWidth: 4,
                    TabWidth: 4,
                    UseTab: Never,
                    PointerAlignment: Right,
                    Standard: Cpp11}]]
                }
            })
            guard.setup({
                fmt_on_save = false,
                lsp_as_default_formatter = true
            })
        end
    }
Sologala commented 5 months ago

I know https://github.com/nvimdev/guard.nvim/commit/81a0995f07cc370fbf15d6d03abc4b1f8651d23f is not the latest guard.nvim version, But newest commit has the issus of deleteing some lines; https://github.com/nvimdev/guard.nvim/issues/145#issuecomment-1975368878

xiaoshihou514 commented 5 months ago

There's not much we could do about that unfourtunately :( In fact in the README:

Use GuardFmt to manually call format, when there is a visual selection only the selection is formatted. NOTE: Regional formatting just sends your selection to the formatter, if there's not enough context incoherent formatting might occur (e.g. indent being erased)

There's just not enough context in terms what one actually wants when asking for range formatting, in addition to the fact that the surrounding context may not even be formatted by the formatter you are using.