vitorpy's Dotfiles
at main 71 lines 2.2 kB view raw
1-- IDE Layout Command 2-- Creates a layout with nvim-tree on the left (1/4 width) 3-- and a split editor/terminal on the right (3/4 width) 4 5local M = {} 6 7function M.setup_ide_layout() 8 -- Close all windows except current (ignore errors from floating windows) 9 pcall(function() vim.cmd("only") end) 10 11 -- Close nvim-tree if open 12 pcall(function() vim.cmd("NvimTreeClose") end) 13 14 -- Open nvim-tree on the left 15 vim.cmd("NvimTreeOpen") 16 17 -- Move to the right window (main editor area) 18 vim.cmd("wincmd l") 19 20 -- Create horizontal split for terminal at bottom 21 vim.cmd("split") 22 23 -- Move to bottom split and resize to 1/5 of height 24 vim.cmd("wincmd j") 25 local total_height = vim.o.lines 26 local term_height = math.floor(total_height * 0.20) 27 vim.cmd("resize " .. term_height) 28 29 -- Open terminal in bottom split 30 vim.cmd("terminal") 31 32 -- Move back to top split (editor) 33 vim.cmd("wincmd k") 34 35 -- Setup autocommand to quit when only tree and terminal are left 36 vim.api.nvim_create_autocmd("BufDelete", { 37 group = vim.api.nvim_create_augroup("IDEAutoQuit", { clear = true }), 38 callback = function() 39 -- Small delay to let buffer deletion complete 40 vim.defer_fn(function() 41 local buf_list = vim.api.nvim_list_bufs() 42 local has_normal_buffer = false 43 44 for _, buf in ipairs(buf_list) do 45 -- Only check loaded and valid buffers 46 if vim.api.nvim_buf_is_loaded(buf) and vim.api.nvim_buf_is_valid(buf) then 47 local buftype = vim.api.nvim_buf_get_option(buf, "buftype") 48 local filetype = vim.api.nvim_buf_get_option(buf, "filetype") 49 local bufname = vim.api.nvim_buf_get_name(buf) 50 51 -- Check if it's a normal editable buffer (not tree, not terminal, not special) 52 if buftype == "" and filetype ~= "NvimTree" and bufname ~= "" then 53 has_normal_buffer = true 54 break 55 end 56 end 57 end 58 59 -- If no normal buffers left, quit vim 60 if not has_normal_buffer then 61 vim.cmd("qall") 62 end 63 end, 100) 64 end, 65 }) 66end 67 68-- Create the :IDE command 69vim.api.nvim_create_user_command("IDE", M.setup_ide_layout, {}) 70 71return M