rustaceanvim icon indicating copy to clipboard operation
rustaceanvim copied to clipboard

Config for placement of diagnostics float

Open GordianDziwis opened this issue 1 year ago • 3 comments

Feature description

I have a function for lsp-diagnostics, whihc place the diagnostic float in the lower left corner:

            local _, winnr = vim.diagnostic.open_float({ source = true })
            if winnr then
                local width = vim.api.nvim_get_option("columns")
                local height = vim.api.nvim_get_option("lines")
                local config = {
                  anchor = 'SW',
                  relative = 'editor',
                  row = height - 2,
                  col = 1,
                }
                vim.api.nvim_win_set_config(winnr, config)
            end

Because rusteceanvim does not show hints and info diagnostic levels, I have a function to fall back to lsp diagnostics in `ftplugin/rust.lua:

local bufnr = vim.api.nvim_get_current_buf()
local opts = { silent = true, buffer = bufnr }
vim.keymap.set("n", "<localleader>e", function()
    local output = vim.fn.execute('RustLsp renderDiagnostic current')
    if output:match("No renderable diagnostics found.") then
        vim.cmd('lua _G.show_diagnostics()')
    end
end, opts)

But now the diagnostic float placement is inconsistent between native diagnostics and rustaceanvim.

Can I modify the window placement for RustLsp renderDiagnostic current dynamically?

GordianDziwis avatar Jul 23 '24 09:07 GordianDziwis

Hey 👋

You can configure rustaceanvim's floating windows with vim.g.rustaceanvim.tools.float_win_config.

See :h rustaceanvim.config.

If that doesn't provide what you're looking for, PRs are welcome 😀.

mrcjkb avatar Jul 24 '24 15:07 mrcjkb

@mrcjkb Is this config lazily evaluated on creation of a float?

GordianDziwis avatar Aug 08 '24 10:08 GordianDziwis

@mrcjkb Is this config lazily evaluated on creation of a float?

No.

mrcjkb avatar Aug 08 '24 10:08 mrcjkb

Is there a way to modify the width/height of the floating windows? When I execute the debuggables command, the hover window is too horizontally small to display the entire names of the debuggables.

retarpt avatar Mar 25 '25 16:03 retarpt

@retarpt You can globally override the floating preview config:

local border = { '▗', '▄', '▖', '▌', '▘', '▀', '▝', '▐' };

local orig_util_open_floating_preview = vim.lsp.util.open_floating_preview;
function vim.lsp.util.open_floating_preview(contents, syntax, opts, ...)
    opts = opts or {};
    opts.border = border;
    opts.max_width = opts.max_width or 100;
    opts.wrap = opts.wrap or true;
    return orig_util_open_floating_preview(contents, syntax, opts, ...);
end;

GordianDziwis avatar Mar 26 '25 09:03 GordianDziwis

Thank you very much!

retarpt avatar Mar 26 '25 15:03 retarpt

@mrcjkb Is this config lazily evaluated on creation of a float?

With regards to this point, does this mean relative positioning to the right of the window is not possible, because we have to get the width of the window at runtime, and not when configuring the global Rustaceanvim float config?

JeanMertz avatar Apr 09 '25 18:04 JeanMertz

As of now, the only way to do that would be by overriding vim.lsp.util.open_floating_preview, I suppose.

mrcjkb avatar Apr 09 '25 18:04 mrcjkb

As of now, the only way to do that would be by overriding vim.lsp.util.open_floating_preview, I suppose.

I did think about that, but because opening the float happens asynchronously, I don't believe I can dynamically change the float preview config for a single rustaceanvim call? E.g. this won't work?

-- override vim.lsp.util.open_floating_preview
vim.cmd.RustLsp({ "renderDiagnostic", "current" }) -- calls `open_floating_preview` in a scheduled task
-- revert to old vim.lsp.util.open_floating_preview

Or perhaps this will work? I'll have to experiment with it.

JeanMertz avatar Apr 10 '25 05:04 JeanMertz

No, but you could override it globally and change the behaviour based on the focus_id, which is 'rust-analyzer-hover-actions'. It's a hacky solution, but it should work.

mrcjkb avatar Apr 10 '25 07:04 mrcjkb