echasnovski / mini.nvim

Library of 40+ independent Lua modules improving overall Neovim (version 0.8 and higher) experience with minimal effort
MIT License
4.9k stars 186 forks source link

simplify custom sessions autoread #890

Closed arcxio closed 3 months ago

arcxio commented 4 months ago

Contributing guidelines

Module(s)

mini.sessions,mini.misc

Description

I have a project-based sessions workflow, meaning I use sessions based on MiniMisc.find_root(). Implementing my own autowrite is trivial, but autoread - less so, the main problem being checking if something is shown, which is implemented in a default autoread but hidden as a helper function. So I have to rely on a copy of H.is_something_shown() in my config just so I can autoread a session with the name I supply. Here's how the whole setup looks:

MiniDeps.now(function()
    require('mini.sessions').setup()

    local function percent_name(path) return path and path:gsub('/', '%%'):gsub('%%$', '') or nil end

    local command = vim.api.nvim_create_user_command
    command('Sw', function() MiniSessions.write(percent_name(MiniMisc.find_root())) end, {})
    command('Sd', function() MiniSessions.delete(nil, { force = true }) end, {})

    local function autowrite() return vim.v.this_session ~= '' and MiniSessions.write() end
    local function autoread()
        -- return if something is shown
        if vim.fn.argc() > 0 then return end
        if more_than_one_buffer() then return end
        if vim.bo.filetype ~= '' then return end
        if vim.api.nvim_buf_line_count(0) > 1 then return end
        if vim.api.nvim_buf_get_lines(0, 0, 1, true)[1]:len() > 0 then return end

        local session = percent_name(vim.fn.getcwd())
        if vim.tbl_contains(vim.tbl_keys(MiniSessions.detected), session) then MiniSessions.read(session) end
    end

    vim.api.nvim_create_autocmd('VimEnter', { nested = true, once = true, callback = autoread })
    vim.api.nvim_create_autocmd('VimLeavePre', { callback = autowrite })
end)

I feel like making a custom sessions autoread should be more straightforward, but I'm not sure what the best solution for that would be. I can come up with two:

  1. Allowing autoread to get session name from a callback supplied to config. Probably too specific for my workflow.
  2. Exposing is_something_is_shown(), probably in 'mini.misc'.
echasnovski commented 4 months ago

Thanks for the suggestion!

My initial feeling is that this will over-complicate 'mini.sessions'. But I'll think about it.

echasnovski commented 3 months ago

Sorry, I think supporting custom autoreading logic beyond what currently is possible is a bit too much for 'mini.sessions'.

Exporting is_something_shown() in 'mini.misc' means duplicating it third time in 'mini.nvim' for not a lot of benefit. It is also not that complicated function to implement from scratch.

Closing as not planned.

arcxio commented 3 months ago

It is also not that complicated function to implement from scratch.

that's fair. I ended up leaving just the first line of is_something_shown (if vim.fn.argc() > 0 then return end) in autoread and found it sufficient.

arcxio commented 3 months ago

duplicating it third time in 'mini.nvim'

by the way, on that note, is_something_shown in sessions and starter have reversed order of conditionals. is that intentional? I thought starter's order makes more sense (from a more simple check to a more complicated check)

echasnovski commented 3 months ago

by the way, on that note, is_something_shown in sessions and starter have reversed order of conditionals. is that intentional? I thought starter's order makes more sense (from a more simple check to a more complicated check)

No, I don't think so. They probably should be the same. I'll look into it.

Thanks for noticing!

Edit: They are now the same on the main branch.