mirror of
https://github.com/folke/snacks.nvim
synced 2025-12-23 08:47:57 +00:00
## Description <!-- Describe the big picture of your changes to communicate to the maintainers why we should accept this pull request. --> ## Related Issue(s) <!-- If this PR fixes any issues, please link to the issue here. - Fixes #<issue_number> --> ## Screenshots <!-- Add screenshots of the changes if applicable. -->
61 lines
1.7 KiB
Lua
61 lines
1.7 KiB
Lua
local M = {}
|
|
|
|
---@class snacks.scratch.actions
|
|
---@field [string] snacks.picker.Action.spec
|
|
M.actions = {
|
|
scratch_open = function(picker, item)
|
|
picker:close()
|
|
if not item then
|
|
return
|
|
end
|
|
Snacks.scratch.open({ icon = item.item.icon, file = item.item.file, name = item.item.name, ft = item.item.ft })
|
|
end,
|
|
scratch_delete = function(picker, item)
|
|
local current = item.file
|
|
os.remove(current)
|
|
os.remove(current .. ".meta")
|
|
picker:refresh()
|
|
end,
|
|
scratch_new = function(picker)
|
|
picker:close()
|
|
Snacks.scratch.open()
|
|
end,
|
|
}
|
|
|
|
---@param opts snacks.picker.proc.Config
|
|
---@type snacks.picker.finder
|
|
function M.scratch(opts)
|
|
local list = Snacks.scratch.list()
|
|
local items = {} ---@type snacks.picker.finder.Item[]
|
|
for _, item in ipairs(list) do
|
|
items[#items + 1] = {
|
|
file = item.file,
|
|
item = item,
|
|
title = item.name,
|
|
text = Snacks.picker.util.text(item, { "name", "branch", "ft" }),
|
|
branch = item.branch and ("branch:%s"):format(item.branch) or "",
|
|
}
|
|
end
|
|
return items
|
|
end
|
|
|
|
---@type snacks.picker.format
|
|
function M.format(item, picker)
|
|
local file = item.item
|
|
local ret = {} ---@type snacks.picker.Highlight[]
|
|
local a = Snacks.picker.util.align
|
|
local icon, icon_hl = file.icon, nil
|
|
if not icon then
|
|
icon, icon_hl = Snacks.util.icon(file.ft, "filetype")
|
|
end
|
|
ret[#ret + 1] = { a(icon, 3), icon_hl }
|
|
ret[#ret + 1] = { a(file.name, 20, { truncate = true }) }
|
|
ret[#ret + 1] = { " " }
|
|
ret[#ret + 1] = { a(item.branch, 20, { truncate = true }), "Number" }
|
|
ret[#ret + 1] = { " " }
|
|
---@diagnostic disable-next-line: missing-fields
|
|
vim.list_extend(ret, Snacks.picker.format.filename({ text = "", dir = true, file = file.cwd }, picker))
|
|
return ret
|
|
end
|
|
|
|
return M
|