refacto: create actions folder and move some code to it

- move trash.lua into actions
- move system-open from root file to actions
- move copypasta code from fs to actions
- add file_exists in utils
- add rename_loaded_buffers in utils (might need to move that in the
  future).
This commit is contained in:
kiyan
2022-01-21 11:25:49 +01:00
parent 691a1c9699
commit 1248bcec86
7 changed files with 313 additions and 269 deletions

View File

@@ -1,6 +1,8 @@
local M = {}
local a = vim.api
local uv = vim.loop
local M = {}
function M.path_to_matching_str(path)
return path:gsub('(%-)', '(%%-)'):gsub('(%.)', '(%%.)'):gsub('(%_)', '(%%_)')
end
@@ -160,8 +162,8 @@ end
---@param comparator function|nil
function M.merge_sort(t, comparator)
if not comparator then
comparator = function (a, b)
return a < b
comparator = function (left, right)
return left < right
end
end
@@ -182,4 +184,23 @@ function M.is_windows_exe(ext)
return pathexts[ext:upper()]
end
function M.rename_loaded_buffers(old_name, new_name)
for _, buf in pairs(a.nvim_list_bufs()) do
if a.nvim_buf_is_loaded(buf) then
if a.nvim_buf_get_name(buf) == old_name then
a.nvim_buf_set_name(buf, new_name)
-- to avoid the 'overwrite existing file' error message on write
vim.api.nvim_buf_call(buf, function() vim.cmd("silent! w!") end)
end
end
end
end
--- @param path string path to file or directory
--- @return boolean
function M.file_exists(path)
local _, error = vim.loop.fs_stat(path)
return error == nil
end
return M