diff --git a/README.md b/README.md index c14bf46..9fc1c55 100644 --- a/README.md +++ b/README.md @@ -39,7 +39,6 @@ Lightweight and quick file operations without being a full-blown file manager. { "chrisgrieser/nvim-genghis", dependencies = "stevearc/dressing.nvim" - cmd = "Genghis", }, -- packer @@ -55,6 +54,10 @@ The `setup` call is optional. ```lua -- default config require("genghis").setup { + backdrop = { + enabled = true, + blend = 50, + }, -- cli name, default is `trash` on Mac and Windows, and `gio trash` on Linux trashCmd = "trash", } diff --git a/lua/genghis/config.lua b/lua/genghis/config.lua index e5ebf78..cec7bec 100644 --- a/lua/genghis/config.lua +++ b/lua/genghis/config.lua @@ -3,6 +3,11 @@ local M = {} ---@class Genghis.config local defaultConfig = { + backdrop = { + enabled = true, + blend = 50, + }, + -- cli name, default is `trash` on Mac and Windows, and `gio trash` on Linux trashCmd = nil, } diff --git a/lua/genghis/operations/file.lua b/lua/genghis/operations/file.lua index 0a78d20..9ec1458 100644 --- a/lua/genghis/operations/file.lua +++ b/lua/genghis/operations/file.lua @@ -1,5 +1,6 @@ local M = {} +local backdrop = require("genghis.support.backdrop") local rename = require("genghis.support.lsp-rename") local u = require("genghis.support.utils") local osPathSep = package.config:sub(1, 1) @@ -38,6 +39,13 @@ local function fileOp(op) prefill = "" end + -- backdrop + vim.api.nvim_create_autocmd("FileType", { + group = vim.api.nvim_create_augroup("InputGenghisBackdrop", {}), + pattern = "DressingInput", + callback = function(ctx) backdrop.new(ctx.buf) end, + }) + vim.ui.input({ prompt = promptStr, default = prefill, @@ -119,6 +127,8 @@ function M.duplicateFile() fileOp("duplicate") end function M.createNewFile() fileOp("new") end function M.moveSelectionToNewFile() fileOp("new-from-selection") end +-------------------------------------------------------------------------------- + function M.moveToFolderInCwd() local curFilePath = vim.api.nvim_buf_get_name(0) local parentOfCurFile = vim.fs.dirname(curFilePath) .. osPathSep @@ -148,6 +158,12 @@ function M.moveToFolderInCwd() -- insert cwd at bottom, since modification of is likely due to subfolders if cwd ~= parentOfCurFile then table.insert(foldersInCwd, cwd) end + local autocmd = vim.api.nvim_create_autocmd("FileType", { + group = vim.api.nvim_create_augroup("SelectorGenghisBackdrop", {}), + pattern = { "DressingSelect", "TelescopePrompt" }, + callback = function(ctx) backdrop.new(ctx.buf) end, + }) + -- prompt user and move local promptStr = "Choose Destination Folder" if lspSupportsRenaming then promptStr = promptStr .. " (with updated imports)" end @@ -156,6 +172,9 @@ function M.moveToFolderInCwd() kind = "genghis.moveToFolderInCwd", format_item = function(path) return path:sub(#cwd) end, -- only relative path }, function(destination) + -- in case neither dressing nor telescope was used as selector-backend + vim.api.nvim_del_autocmd(autocmd) + if not destination then return end local newFilePath = destination .. osPathSep .. filename @@ -178,7 +197,5 @@ function M.moveToFolderInCwd() end) end --------------------------------------------------------------------------------- - -------------------------------------------------------------------------------- return M diff --git a/lua/genghis/support/backdrop.lua b/lua/genghis/support/backdrop.lua new file mode 100644 index 0000000..5787ea1 --- /dev/null +++ b/lua/genghis/support/backdrop.lua @@ -0,0 +1,46 @@ +local M = {} +-------------------------------------------------------------------------------- + +local backdropName = "GenghisBackdrop" + +---@param referenceBuf number Reference buffer, when that buffer is closed, the backdrop will be closed too +---@param referenceZindex? number zindex of the reference window, where the backdrop should be placed below +function M.new(referenceBuf, referenceZindex) + local config = require("genghis.config").config + if not config.backdrop.enabled then return end + local blend = config.backdrop.blend + if not referenceZindex then referenceZindex = 10 end + + local bufnr = vim.api.nvim_create_buf(false, true) + local winnr = vim.api.nvim_open_win(bufnr, false, { + relative = "editor", + row = 0, + col = 0, + width = vim.o.columns, + height = vim.o.lines, + focusable = false, + style = "minimal", + zindex = referenceZindex - 1, -- ensure it's below the reference window + }) + vim.api.nvim_set_hl(0, backdropName, { bg = "#000000", default = true }) + vim.wo[winnr].winhighlight = "Normal:" .. backdropName + vim.wo[winnr].winblend = blend + vim.bo[bufnr].buftype = "nofile" + vim.bo[bufnr].filetype = backdropName + + -- close backdrop when the reference buffer is closed + vim.api.nvim_create_autocmd({ "WinClosed", "BufLeave" }, { + group = vim.api.nvim_create_augroup(backdropName, { clear = true }), + once = true, + buffer = referenceBuf, + callback = function() + if vim.api.nvim_win_is_valid(winnr) then vim.api.nvim_win_close(winnr, true) end + if vim.api.nvim_buf_is_valid(bufnr) then + vim.api.nvim_buf_delete(bufnr, { force = true }) + end + end, + }) +end + +-------------------------------------------------------------------------------- +return M