From 87e35827c3e5c46d3a59377824fff8b8c7cd57d7 Mon Sep 17 00:00:00 2001 From: psychhim Date: Sat, 27 Sep 2025 03:31:17 +0530 Subject: [PATCH] new split windows repositioned to right/bottom for horizontal/vertical splits --- init.lua | 6 --- lua/custom/plugins/filetree.lua | 81 +++++++++++++++++++++------------ 2 files changed, 52 insertions(+), 35 deletions(-) diff --git a/init.lua b/init.lua index 792486d..702f0dd 100644 --- a/init.lua +++ b/init.lua @@ -402,12 +402,6 @@ end, { desc = 'Save & quit (prompt if new file)' }) --Discard changes and Close current window vim.keymap.set('n', 'qn', 'q!') ---Horizontal split -vim.keymap.set('n', 'sh', 'split', { desc = 'Split [H]orizontal' }) - ---Vertical split -vim.keymap.set('n', 'sv', 'vsplit', { desc = 'Split [V]ertical' }) - --Switch below/right split windows vim.keymap.set('n', '', '') diff --git a/lua/custom/plugins/filetree.lua b/lua/custom/plugins/filetree.lua index 1df88f1..dae405f 100644 --- a/lua/custom/plugins/filetree.lua +++ b/lua/custom/plugins/filetree.lua @@ -1,13 +1,13 @@ -- Unless you are still migrating, remove the deprecated commands from v1.x -vim.cmd([[ let g:neo_tree_remove_legacy_commands = 1 ]]) +vim.cmd [[ let g:neo_tree_remove_legacy_commands = 1 ]] return { - "nvim-neo-tree/neo-tree.nvim", - version = "*", + 'nvim-neo-tree/neo-tree.nvim', + version = '*', dependencies = { - "nvim-lua/plenary.nvim", - "nvim-tree/nvim-web-devicons", - "MunifTanjim/nui.nvim", + 'nvim-lua/plenary.nvim', + 'nvim-tree/nvim-web-devicons', + 'MunifTanjim/nui.nvim', }, config = function() local function smart_open(state) @@ -17,6 +17,13 @@ return { end local path = node:get_id() + -- If the node is a directory, just toggle expand/collapse + if node.type == 'directory' then + state.commands.toggle_node(state, node) + return + end + + -- --- File handling starts here --- -- Reuse already open buffer in any tab safely for _, tab in ipairs(vim.api.nvim_list_tabpages()) do if vim.api.nvim_tabpage_is_valid(tab) then @@ -30,10 +37,7 @@ return { for _, w in ipairs(vim.api.nvim_list_wins()) do if vim.api.nvim_win_is_valid(w) then local b = vim.api.nvim_win_get_buf(w) - if - vim.api.nvim_buf_is_valid(b) - and vim.api.nvim_buf_get_option(b, "filetype") == "neo-tree" - then + if vim.api.nvim_buf_is_valid(b) and vim.api.nvim_buf_get_option(b, 'filetype') == 'neo-tree' then vim.api.nvim_win_close(w, true) end end @@ -53,9 +57,9 @@ return { local buf = vim.api.nvim_win_get_buf(win) if vim.api.nvim_buf_is_valid(buf) then local bufname = vim.api.nvim_buf_get_name(buf) - local buftype = vim.api.nvim_buf_get_option(buf, "buftype") - local modified = vim.api.nvim_buf_get_option(buf, "modified") - if bufname == "" and buftype == "" and not modified then + local buftype = vim.api.nvim_buf_get_option(buf, 'buftype') + local modified = vim.api.nvim_buf_get_option(buf, 'modified') + if bufname == '' and buftype == '' and not modified then empty_buf = buf vim.api.nvim_set_current_win(win) break @@ -65,43 +69,62 @@ return { end if empty_buf then - vim.cmd("edit " .. vim.fn.fnameescape(path)) + vim.cmd('edit ' .. vim.fn.fnameescape(path)) else - vim.cmd("tabnew " .. vim.fn.fnameescape(path)) + vim.cmd('tabnew ' .. vim.fn.fnameescape(path)) end -- Always close Neo-tree window if open for _, win in ipairs(vim.api.nvim_list_wins()) do if vim.api.nvim_win_is_valid(win) then local buf = vim.api.nvim_win_get_buf(win) - if - vim.api.nvim_buf_is_valid(buf) - and vim.api.nvim_buf_get_option(buf, "filetype") == "neo-tree" - then + if vim.api.nvim_buf_is_valid(buf) and vim.api.nvim_buf_get_option(buf, 'filetype') == 'neo-tree' then vim.api.nvim_win_close(win, true) end end end end - require("neo-tree").setup({ + require('neo-tree').setup { close_if_last_window = true, - popup_border_style = "rounded", + popup_border_style = 'rounded', enable_git_status = true, enable_diagnostics = true, default_component_configs = { indent = { padding = 1, indent_size = 2 }, - icon = { folder_closed = "", folder_open = "", folder_empty = "ﰊ" }, + icon = { folder_closed = '', folder_open = '', folder_empty = 'ﰊ' }, }, window = { - position = "float", + position = 'float', width = 40, mapping_options = { noremap = true, nowait = true }, mappings = { - [""] = smart_open, - ["h"] = "open_vsplit", - ["v"] = "open_split", - ["t"] = "noop", + [''] = smart_open, + ['v'] = function(state) + local node = state.tree:get_node() + if node.type == 'directory' then + state.commands.toggle_node(state, node) + return + end + local path = node:get_id() + state.commands.open_split(state) -- opens horizontal split (top) + vim.schedule(function() + vim.cmd 'wincmd J' -- move the new split to bottom + end) + end, + ['h'] = function(state) + local node = state.tree:get_node() + if node.type == 'directory' then + state.commands.toggle_node(state, node) + return + end + local path = node:get_id() + state.commands.open_vsplit(state) -- opens vertical split (left) + vim.schedule(function() + vim.cmd 'wincmd L' -- move the new split to right + end) + end, + ['t'] = 'noop', }, }, filesystem = { @@ -109,13 +132,13 @@ return { enabled = true, -- updated to table format }, use_libuv_file_watcher = true, - hijack_netrw_behavior = "open_default", + hijack_netrw_behavior = 'open_default', filtered_items = { visible = true, hide_dotfiles = false, hide_gitignored = true, }, }, - }) + } end, }