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

feat(plugin): support running all init functions

Open MeanderingProgrammer opened this issue 1 year ago • 1 comments

Description

I have taken to structuring my configuration per language similar to LazyVim's extras. Sometimes for a particular plugin I want to specify initialization logic within the spec for the language. I use the init function for this but currently only the last one of these is ran.

For example I'll have a top level conform.nvim config:

return {
    'stevearc/conform.nvim',
    opts = {
        formatters_by_ft = {},
        format_after_save = function()
            return { lsp_format = 'fallback' }
        end,
    },
}

Then for each language I'll update the options using opts and do some environment setup related to the formatter in the init function. For example my typescript extra looks like:

return {
    {
        'stevearc/conform.nvim',
        init = function()
            -- Avoid running when project does not use prettier
            vim.env.PRETTIERD_LOCAL_PRETTIER_ONLY = 1

            -- Due to prettierd not picking up changes
            -- https://github.com/fsouza/prettierd/issues/719
            vim.api.nvim_create_autocmd('BufWritePost', {
                group = vim.api.nvim_create_augroup('RestartPrettierd', { clear = true }),
                pattern = '*prettier*',
                callback = function()
                    vim.fn.system('prettierd restart')
                end,
            })
        end,
        opts = {
            formatters_by_ft = {
                javascript = { 'prettierd' },
                typescript = { 'prettierd' },
            },
        },
    },
}

The issue comes up if I want to have initialization logic related to a formatter for another language. I can create a toy example if you would like.

This is a niche issue so if you prefer to leave it out totally understandable. There are also workarounds like putting this logic in the opts function, I just prefer having the environment stuff and the options to be separate but its a very minor gripe.

Related Issue(s)

I was unable to find any similar requests.

Screenshots

N/A

MeanderingProgrammer avatar Nov 04 '24 20:11 MeanderingProgrammer

This PR is stale because it has been open 30 days with no activity.

github-actions[bot] avatar Dec 05 '24 02:12 github-actions[bot]