Skip to content

ranges for textDocument/rangeFormatting are wrong #19123

@jyn514

Description

@jyn514

rust-analyzer version: rust-analyzer 1.86.0-nightly (f7cc13a 2025-01-25)

rustc version: rustc 1.86.0-nightly (f7cc13a 2025-01-25)

rustfmt version: rustfmt 1.8.0-nightly (f7cc13a 2025-01-25)

editor or extension: neovim 0.10.4, lspconfig 1.6.0 00dae9f

relevant settings:

-- can skip these first two lines if you have a plugin manager
local lazypath = vim.fn.stdpath("data") .. "/lazy/nvim-lspconfig"
vim.opt.rtp:prepend(lazypath)

local lspconfig = require('lspconfig')
local settings = { ['rust-analyzer'] = { rustfmt = { rangeFormatting = { enable = true } } } }
lspconfig.rust_analyzer.setup {
    settings = settings,
    -- https://github.com/rust-lang/rust-analyzer/issues/17301
    init_options = settings,
    on_init = function(client)
        -- the real code checks if rustfmt is nightly or not
        vim.opt_local.formatexpr = 'v:lua.vim.lsp.formatexpr()'
        client.on_attach = function()
            vim.opt_local.formatexpr = 'v:lua.vim.lspformatexpr()'
        end
    end
}

code snippet to reproduce:

fn main() {
    let unit_offsets_cache = collect(dwarf.units  ())  ?;
}

steps to reproduce:

  1. verify that formatting with RA is enabled: set formatexpr? (should show formatexpr=v:lua.vim.lsp.formatexpr())
  2. move your cursor to line 2. run Vgq to format the current line. observe that nothing happens.
  3. move your cursor to line 3. run Vgq. observe that line 2 is formatted.

I debugged that nvim is sending 0-indexed lines, so maybe RA or rustfmt is expecting 1-indexed lines?

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions