Skip to content

Commit

Permalink
refactor: file actions
Browse files Browse the repository at this point in the history
  • Loading branch information
phanen committed May 7, 2024
1 parent 26ad476 commit bd62fe3
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 5 deletions.
22 changes: 19 additions & 3 deletions lua/fzf-lua-overlay/actions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,30 @@ M.create_whatever = function(_, opts)

-- create, then open
if not vim.uv.fs_stat(path) then
local ok = u.write_file(path, nil, 'w')
vim.fn.mkdir(vim.fn.fnamemodify(path, ':p:h'), 'p')
local ok = u.write_file(path)
if not ok then return vim.notify(('fail to create %s'):format(path)) end
end
vim.cmd.e(path)
vim.notify(('%s created'):format(query), vim.log.levels.INFO)
end

M.delete_files = function(selected, opts)
-- open file (create if not exist)
M.file_create_open = function(_, opts)
local query = require('fzf-lua').get_last_query()
if print then return vim.print(opts) end
local path = vim.fn.expand(('%s/%s'):format(opts.cwd or vim.uv.cwd(), query))

if not vim.uv.fs_stat(path) then
vim.fn.mkdir(vim.fn.fnamemodify(path, ':p:h'), 'p')
local ok = u.write_file(path)
if not ok then return vim.notify(('fail to create %s'):format(path)) end
end

vim.cmd.e(path)
end

M.file_delete = function(selected, opts)
-- TODO: multi?
-- local cwd = opts.cwd or vim.fn.getcwd()
-- local path = vim.fn.expand(('%s/%s'):format(cwd, selected[1]))
Expand All @@ -86,7 +102,7 @@ M.delete_files = function(selected, opts)
end

-- used by fzf's builtin file pickers
M.rename_files = function(selected, opts)
M.file_rename = function(selected, opts)
local file = require('fzf-lua').path.entry_to_file(selected[1], opts)
local oldpath = file.path
local oldname = vim.fs.basename(oldpath)
Expand Down
2 changes: 1 addition & 1 deletion lua/fzf-lua-overlay/providers/find_notes.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ M.opts = {
return require('fzf-lua-overlay').grep_notes({ query = last_query })
end,
['ctrl-n'] = function(...) require('fzf-lua-overlay.actions').create_whatever(...) end,
['ctrl-x'] = function(...) require('fzf-lua-overlay.actions').delete_files(...) end,
['ctrl-x'] = function(...) require('fzf-lua-overlay.actions').file_delete(...) end,
},
fzf_opts = { ['--history'] = notes_history },
-- file_icons = false,
Expand Down
1 change: 0 additions & 1 deletion lua/fzf-lua-overlay/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ u.read_file = function(path)
end

u.write_file = function(path, str, flag)
vim.fn.mkdir(vim.fn.fnamemodify(path, ':p:h'), 'p')
local fd = io.open(path, flag or 'w')
if not fd then return false end
if str then fd:write(str) end
Expand Down

0 comments on commit bd62fe3

Please sign in to comment.