diff --git a/lazy-lock.json b/lazy-lock.json index 77d03ee9..f4f0461f 100644 --- a/lazy-lock.json +++ b/lazy-lock.json @@ -22,6 +22,7 @@ "nvim-treesitter": { "branch": "main", "commit": "7caec274fd19c12b55902a5b795100d21531391f" }, "nvim-web-devicons": { "branch": "master", "commit": "d7462543c9e366c0d196c7f67a945eaaf5d99414" }, "opencode.nvim": { "branch": "main", "commit": "df533d6da724109bf08446392db860fdceddbd0c" }, + "persistence.nvim": { "branch": "main", "commit": "b20b2a7887bd39c1a356980b45e03250f3dce49c" }, "plenary.nvim": { "branch": "master", "commit": "b9fd5226c2f76c951fc8ed5923d85e4de065e509" }, "telescope-fzf-native.nvim": { "branch": "main", "commit": "6fea601bd2b694c6f2ae08a6c6fab14930c60e2c" }, "telescope-ui-select.nvim": { "branch": "master", "commit": "6e51d7da30bd139a6950adf2a47fda6df9fa06d2" }, diff --git a/lua/custom/plugins/opencode.lua b/lua/custom/plugins/opencode.lua index 39a1394c..c14e4086 100644 --- a/lua/custom/plugins/opencode.lua +++ b/lua/custom/plugins/opencode.lua @@ -25,6 +25,28 @@ return { }, }, config = function() + local sidebar_filetypes = { + ['neo-tree'] = true, + ['snacks_layout_box'] = true, + ['snacks_terminal'] = true, + ['toggleterm'] = true, + ['opencode'] = true, + } + + local function focus_main_window() + for _, win in ipairs(vim.api.nvim_tabpage_list_wins(0)) do + local buf = vim.api.nvim_win_get_buf(win) + local ft = vim.bo[buf].filetype + local bt = vim.bo[buf].buftype + if not sidebar_filetypes[ft] and bt == '' then + vim.api.nvim_set_current_win(win) + return true + end + end + + return false + end + ---@type opencode.Opts vim.g.opencode_opts = { server = { @@ -60,5 +82,21 @@ return { -- You may want these if you use the opinionated `` and `` keymaps above — otherwise consider `o…` (and remove terminal mode from the `toggle` keymap) vim.keymap.set('n', '+', '', { desc = 'Increment under cursor', noremap = true }) vim.keymap.set('n', '-', '', { desc = 'Decrement under cursor', noremap = true }) + + vim.api.nvim_create_autocmd('VimEnter', { + callback = function() + if vim.fn.argc() == 1 and vim.fn.isdirectory(vim.fn.argv(0)) == 1 then + vim.schedule(function() + pcall(function() require('persistence').load() end) + + vim.schedule(function() + focus_main_window() + require('opencode').toggle() + focus_main_window() + end) + end) + end + end, + }) end, } diff --git a/lua/custom/plugins/persistence.lua b/lua/custom/plugins/persistence.lua new file mode 100644 index 00000000..bac8e9a2 --- /dev/null +++ b/lua/custom/plugins/persistence.lua @@ -0,0 +1,15 @@ +return { + { + 'folke/persistence.nvim', + event = 'BufReadPre', + opts = { + need = 1, + }, + keys = { + { 'qs', function() require('persistence').load() end, desc = '[Q]uit: restore [S]ession' }, + { 'qS', function() require('persistence').select() end, desc = '[Q]uit: restore [S]ession (picker)' }, + { 'ql', function() require('persistence').load { last = true } end, desc = '[Q]uit: restore [L]ast session' }, + { 'qd', function() require('persistence').stop() end, desc = '[Q]uit: [D]isable session save' }, + }, + }, +}