Skip to content

fix(splitjoin): handle empty positions in split#2485

Open
yilisharcs wants to merge 1 commit into
nvim-mini:mainfrom
yilisharcs:split-uno
Open

fix(splitjoin): handle empty positions in split#2485
yilisharcs wants to merge 1 commit into
nvim-mini:mainfrom
yilisharcs:split-uno

Conversation

@yilisharcs

Copy link
Copy Markdown

why is this a problem? well, i took your advice from #2470 to heart and
implemented the fennel logic entirely with hooks. but one minor hiccup
was revealed when i tried to do this:

-- brackets stay close to the first and last elements
local function lisp_split(positions)
        if #positions < 2 then
                return positions
        end

        -- here specifically, i am NOT splitting if a
        -- list only has 1 index or 1 key-value pair
        local res = {}
        for i = 2, #positions - 1 do
                res[i - 1] = positions[i]
        end

        return res
end

where on line 354 it would attempt to index local 'last' (a nil value).
i thought it'd be ugly if i allowed splits here and the rest is history.
i figure that this patch is simple enough that it won't trouble you this
time. keep up the good work! i love your plugins

why is this a problem? well, i took your advice from nvim-mini#2470 to heart and
implemented the fennel logic entirely with hooks. but one minor hiccup
was revealed when i tried to do this:

```lua
-- brackets stay close to the first and last elements
local function lisp_split(positions)
        if #positions < 2 then
                return positions
        end

        -- here specifically, i am NOT splitting if a
        -- list only has 1 index or 1 key-value pair
        local res = {}
        for i = 2, #positions - 1 do
                res[i - 1] = positions[i]
        end

        return res
end
```

where on line 354 it would attempt to index local 'last' (a nil value).
i thought it'd be ugly if i allowed splits here and the rest is history.
i figure that this patch is simple enough that it won't trouble you this
time. keep up the good work! i love your plugins
@echasnovski echasnovski added mini.splitjoin needs-repro This needs a reproduction example labels Jun 27, 2026
@echasnovski

Copy link
Copy Markdown
Member

Thanks for the PR!

Please provide a minimal reproduction steps that show the problem. This will help determining if the issue is on 'mini.splitjoin' side and potentially with writing tests.

@yilisharcs

Copy link
Copy Markdown
Author

like so?

nvim --clean --cmd "set rtp+=." -u minimal.lua

-- minimal.lua
require("mini.splitjoin").setup()

vim.cmd("edit " .. vim.fn.tempname() .. ".lua")
vim.api.nvim_buf_set_lines(0, 0, -1, false, {
        "[a]",
        "{ b = 1 }",
})

local function dont_split_loners(positions)
        if #positions < 2 then
                return positions
        end

        local res = {}
        for i = 2, #positions - 1 do
                res[i - 1] = positions[i]
        end

        return res
end,

vim.b.minisplitjoin_config = {
        split = {
                hooks_pre = { dont_split_loners },
        },
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

mini.splitjoin needs-repro This needs a reproduction example

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants