mrcjkb / rustaceanvim

🦀 Supercharge your Rust experience in Neovim! A heavily modified fork of rust-tools.nvim
GNU General Public License v2.0
1.62k stars 58 forks source link

Autoformat on save not working #28

Closed NicolasGB closed 11 months ago

NicolasGB commented 11 months ago

Neovim version (nvim -v)

v0.9.4

Operating system/version

ArchLinux

Output of :checkhealth ferris

Checking for Lua dependencies ~ - OK nvim-lua/plenary.nvim installed. - OK mfussenegger/nvim-dap installed. Checking external dependencies ~ - OK rust-analyzer: found rustup 1.26.0 (2023-05-04) - OK Cargo: found cargo 1.73.0 (9c4383fb5 2023-08-26) - OK rustc: found rustc 1.73.0 (cc66ad468 2023-10-03) - WARNING lldb: not found. Install lldb for extended capabilities. A debug adapter (defaults to: LLDB). Required for debugging features. Checking config ~ - OK vim.g.rustaceanvim is set - OK No errors found in config. Checking for conflicting plugins ~ - OK No conflicting plugins detected.

How to reproduce the issue

Open rust file such as main.rs with the following:

fn main() {
            println!("Hello, world!");
}

Save the file.

Expected behaviour

The println line should be formatted and indented by RustFmt.

In the minimal config I've added Lsp-Zero and rust-tools.nvim, we can verify that when commenting rustaceanvim and uncomment rust-tools, main gets formatted. But when rustaceanvim is active no formatting happens.

vim.lsp.buf.format() Does work.

Actual behaviour

Not auto-formatting when saving.

The minimal config used to reproduce this issue.

-- Minimal nvim config with lazy
-- Assumes a directory in $NVIM_DATA_MINIMAL
-- Start with
--
-- export NVIM_DATA_MINIMAL=$(mktemp -d)
-- export NVIM_APP_NAME="nvim-ht-minimal"
-- nvim -u minimal.lua
--
-- Then exit out of neovim and start again.

-- Ignore default config
local config_path = vim.fn.stdpath('config')
vim.opt.rtp:remove(config_path)

-- Ignore default plugins
local data_path = vim.fn.stdpath('data')
local pack_path = data_path .. '/site'
vim.opt.packpath:remove(pack_path)

-- bootstrap lazy.nvim
data_path = assert(os.getenv('NVIM_DATA_MINIMAL'), '$NVIM_DATA_MINIMAL environment variable not set!')
local lazypath = data_path .. '/lazy/lazy.nvim'
local uv = vim.uv
    ---@diagnostic disable-next-line: deprecated
    or vim.loop
if not uv.fs_stat(lazypath) then
    vim.fn.system {
        'git',
        'clone',
        '--filter=blob:none',
        'git@github.com:folke/lazy.nvim.git',
        '--branch=stable',
        lazypath,
    }
end
vim.opt.rtp:prepend(lazypath)

local lazy = require('lazy')

lazy.setup({
    {
        'mrcjkb/rustaceanvim',
        version = '^3',
        init = function()
            -- Configure rustaceanvim here
            vim.g.rustaceanvim = {}
        end,
        ft = { 'rust' },
    },
    -- {
    --     'simrat39/rust-tools.nvim',
    --     dependencies = {
    --         'neovim/nvim-lspconfig'
    --     },
    --     init = function()
    --         require('rust-tools').setup({})
    --     end,
    --     ft = { 'rust' },
    -- },
    {
        'VonHeikemen/lsp-zero.nvim',
        branch = 'v3.x',
        dependencies = {
            'neovim/nvim-lspconfig'
        },
        config = function()
            local lsp = require("lsp-zero").preset({})
            -- Format on save
            lsp.format_on_save({
                format_opts = {
                    async = false,
                    timeout_ms = 10000,
                },
                servers = {
                    ["rust_analyzer"] = { "rust" },
                }
            })
        end

    }
    -- Add any other plugins needed to reproduce the issue.
    -- see https://github.com/folke/lazy.nvim#-lazynvim for details.
}, { root = data_path, state = data_path .. '/lazy-state.json', lockfile = data_path .. '/lazy-lock.json' })
mrcjkb commented 11 months ago

Hey :wave:

The client name in rustaceanvim is rust-analyzer. So if you change

lsp.format_on_save({
    servers = {
        ["rust_analyzer"] = { "rust" },
    }
})

to

lsp.format_on_save({
    servers = {
        ["rust-analyzer"] = { "rust" },
    }
})

it should work :smile:

NicolasGB commented 11 months ago

Hi! Thanks for the quick response.

It does work! Didn't notice the name change checked many things but not that.

Thanks for your work on the plugin, the new explain error command is very nice to have!

mrcjkb commented 11 months ago

Thanks for the feedback :smile:

9mm commented 5 months ago

@mrcjkb how do we do format on save with just a standard rustacean config? im not using lsp zero. this is what i currently have

  {
    "mrcjkb/rustaceanvim",
    ft = { "rust" },
    init = function()
      vim.g.rustaceanvim = {
        server = {
          on_attach = function(client, bufnr)
            local lsp_map = function(mode, keys, func, desc)
              vim.keymap.set(mode, keys, func, { buffer = bufnr, desc = desc })
            end

            -- rust-lsp
            lsp_map("n", "K", function() vim.cmd.RustLsp({ "hover", "actions" }) end, "Rust hover docs")
            lsp_map("n", "J", function() vim.cmd.RustLsp("joinLines") end, "Rust join lines")
            lsp_map("n", "<Leader>ca", function() vim.cmd.RustLsp("codeAction") end, "Rust Code action")
            lsp_map("n", "<Leader>rue", function() vim.cmd.RustLsp("explainError") end, "Rust error explain")
            lsp_map("n", "<Leader>rud", function() vim.cmd.RustLsp("openDocs") end, "Rust docs")
            lsp_map("n", "<Leader>rum", function() vim.cmd.RustLsp("expandMacro") end, "Rust expand macro")

            -- copy from lsp_config
            lsp_map("n", "gd", vim.lsp.buf.definition, "Goto definition")
            lsp_map("n", "gD", vim.lsp.buf.declaration, "Goto declaration")
            lsp_map("n", "gI", vim.lsp.buf.implementation, "Goto implementation")
            lsp_map("n", "go", vim.lsp.buf.type_definition, "Goto type definition")
          end,
        },
      }
    end,
  },
mrcjkb commented 5 months ago

@9mm you could call vim.lsp.buf.format in the callback of a BufWritePre autocommand. See also :h lua-guide-autocommands.

9mm commented 5 months ago

Thanks, if anyone else wants copy paste this is how i did it inside on_attach

local format_sync_grp = vim.api.nvim_create_augroup("RustaceanFormat", {})
vim.api.nvim_create_autocmd("BufWritePre", {
  buffer = bufnr,
  callback = function() vim.lsp.buf.format() end,
  group = format_sync_grp,
})