mini.nvim icon indicating copy to clipboard operation
mini.nvim copied to clipboard

Add format function for LSP progress

Open KaspervdHeijden opened this issue 1 year ago • 4 comments

Contributing guidelines

Module(s)

mini.notify

Description

Currently the LSP progress message format is hardcoded. See https://github.com/echasnovski/mini.nvim/blob/df1559e2ed7ece458e0a97a8bb9556d465d1c775/lua/mini/notify.lua#L678

It would be nice to make this more dynamic, via a format function.

KaspervdHeijden avatar Oct 13 '24 12:10 KaspervdHeijden

Please see https://github.com/echasnovski/mini.nvim/pull/1276

KaspervdHeijden avatar Oct 13 '24 12:10 KaspervdHeijden

Thanks for the suggestion!

I think the proper way is to add some kind of source field to notification specification. One small issue is that add() was not really designed to be extensible (mostly by design to limit complexity). I'd take some time to think if adding source as fourth argument is worth it.

echasnovski avatar Oct 13 '24 14:10 echasnovski

Fair enough. My current workaround is quite hacky:

                notify.setup({
                    content = {
                        format = function (str)
                            local parts = vim.split(str.msg, ': ')

                            if #parts == 2 then
                                return parts[2]
                            end

                            return string.format('\n  %s  \n', str.msg)
                        end,
                    },
                    window = {
                        winblend = 0,
                        config   = function ()
                            local pad = vim.o.cmdheight + (vim.o.laststatus > 0 and 1 or 0)

                            return {
                                row    = vim.o.lines - pad - 1,
                                col    = vim.o.columns,
                                border = 'none',
                                anchor = 'SE',
                            }
                        end,
                    }
                })
            })

https://gitlab.com/kaspervdheijden/kickstart/-/blob/master/lua/plugins-enabled/mini-nvim.lua?ref_type=heads#L78

KaspervdHeijden avatar Oct 13 '24 14:10 KaspervdHeijden

Fair enough. My current workaround is quite hacky:

I don't find the code particularly hacky, on the contrary even - quite concise and to the point. The fact that it is applied to any notification and not only to the ones coming from LSP progress - yeah, maybe.

echasnovski avatar Oct 13 '24 15:10 echasnovski

This should now be cleanly possible on latest main through new data field of notifications. All notifications from built-in LSP progress handler set the following fields in data, which includes source. Here is an example of how to write content.format that behaves differently for notifications from LSP progress.

echasnovski avatar Feb 28 '25 08:02 echasnovski

@echasnovski Thank you, I have changed the function to the snippet below which works perfectly! 🥳

content = {
    format = function (notification)
        if notification.data.source == 'lsp_progress' then
            local s_pos, e_pos = string.find(notification.msg, notification.data.client_name .. ': ', 1, true)

            if s_pos ~= nil and e_pos ~= nil then
                return string.sub(notification.msg, e_pos + 1)
            end
        end

        return notification.msg
    end,
}

KaspervdHeijden avatar Feb 28 '25 16:02 KaspervdHeijden