From 511ff73dc2e3f78c7fb237d7eaa9c39d72e5bbe8 Mon Sep 17 00:00:00 2001 From: Patrick Auernig Date: Thu, 2 May 2024 13:02:33 +0200 Subject: [PATCH] Add directory insertion keybind for oil --- lua/valeth/plugins/init.lua | 14 -------- lua/valeth/plugins/oil.lua | 71 +++++++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+), 14 deletions(-) create mode 100644 lua/valeth/plugins/oil.lua diff --git a/lua/valeth/plugins/init.lua b/lua/valeth/plugins/init.lua index dcbed25..bb27495 100644 --- a/lua/valeth/plugins/init.lua +++ b/lua/valeth/plugins/init.lua @@ -13,18 +13,4 @@ return { "stevearc/dressing.nvim", opts = {}, }, - - { - "stevearc/oil.nvim", - dependencies = { - "nvim-lua/plenary.nvim" - }, - opts = { - columns = { "icon", "permissions" }, - }, - cmd = { "Oil" }, - keys = { - { "ft", "Oil", mode = "n" } - }, - }, } diff --git a/lua/valeth/plugins/oil.lua b/lua/valeth/plugins/oil.lua new file mode 100644 index 0000000..fd2201d --- /dev/null +++ b/lua/valeth/plugins/oil.lua @@ -0,0 +1,71 @@ +local function get_directory_name(buf, line) + local oil = require("oil") + + local entry = oil.get_entry_on_line(buf, line) + if entry == nil then + return + end + + if entry.type == "directory" then + return entry.name .. "/" + else + local dirname = entry.name:match("^(.*/).*$") + if dirname == nil then + return + end + + return dirname + end +end + + +local function insert_directory(direction) + local cur_buf = vim.api.nvim_get_current_buf() + local cur_win = vim.api.nvim_get_current_win() + local cursor = vim.api.nvim_win_get_cursor(cur_win) + + local dir = get_directory_name(cur_buf, cursor[1]) + + if dir == nil then + return + end + + if direction == "below" then + local line = cursor[1] + vim.api.nvim_buf_set_lines(cur_buf, line, line, false, { dir }) + vim.api.nvim_win_set_cursor(cur_win, { line + 1, cursor[2] }) + else + local line = cursor[1] - 1 + vim.api.nvim_buf_set_lines(cur_buf, line, line, false, { dir }) + vim.api.nvim_win_set_cursor(cur_win, { cursor[1], cursor[2] }) + end +end + + +local spec = { + "stevearc/oil.nvim", +} + +spec.dependencies = { + "nvim-lua/plenary.nvim" +} + +spec.opts = { + columns = { "icon", "permissions" }, + keymaps = { + ["J"] = function() + insert_directory("below") + end, + ["K"] = function() + insert_directory("above") + end, + } +} + +spec.cmd = { "Oil" } + +spec.keys = { + { "ft", "Oil", mode = "n" } +} + +return spec