|
| 1 | +local snacks_picker = require "snacks.picker" |
| 2 | + |
| 3 | +local Path = require "obsidian.path" |
| 4 | +local abc = require "obsidian.abc" |
| 5 | +local Picker = require "obsidian.pickers.picker" |
| 6 | + |
| 7 | +local function debug_once(msg, ...) |
| 8 | + -- vim.notify(msg .. vim.inspect(...)) |
| 9 | +end |
| 10 | + |
| 11 | +---@param mapping table |
| 12 | +---@return table |
| 13 | +local function notes_mappings(mapping) |
| 14 | + if type(mapping) == "table" then |
| 15 | + local opts = { win = { input = { keys = {} } }, actions = {} } |
| 16 | + for k, v in pairs(mapping) do |
| 17 | + local name = string.gsub(v.desc, " ", "_") |
| 18 | + opts.win.input.keys = { |
| 19 | + [k] = { name, mode = { "n", "i" }, desc = v.desc }, |
| 20 | + } |
| 21 | + opts.actions[name] = function(picker, item) |
| 22 | + debug_once("mappings :", item) |
| 23 | + picker:close() |
| 24 | + vim.schedule(function() |
| 25 | + v.callback(item.value or item._path) |
| 26 | + end) |
| 27 | + end |
| 28 | + end |
| 29 | + return opts |
| 30 | + end |
| 31 | + return {} |
| 32 | +end |
| 33 | + |
| 34 | +---@class obsidian.pickers.SnacksPicker : obsidian.Picker |
| 35 | +local SnacksPicker = abc.new_class({ |
| 36 | + ---@diagnostic disable-next-line: unused-local |
| 37 | + __tostring = function(self) |
| 38 | + return "SnacksPicker()" |
| 39 | + end, |
| 40 | +}, Picker) |
| 41 | + |
| 42 | +---@param opts obsidian.PickerFindOpts|? Options. |
| 43 | +SnacksPicker.find_files = function(self, opts) |
| 44 | + opts = opts or {} |
| 45 | + |
| 46 | + ---@type obsidian.Path |
| 47 | + local dir = opts.dir.filename and Path:new(opts.dir.filename) or self.client.dir |
| 48 | + |
| 49 | + local map = vim.tbl_deep_extend("force", {}, notes_mappings(opts.selection_mappings)) |
| 50 | + |
| 51 | + local pick_opts = vim.tbl_extend("force", map or {}, { |
| 52 | + source = "files", |
| 53 | + title = opts.prompt_title, |
| 54 | + cwd = tostring(dir), |
| 55 | + confirm = function(picker, item, action) |
| 56 | + picker:close() |
| 57 | + if item then |
| 58 | + if opts.callback then |
| 59 | + debug_once("find files callback: ", item) |
| 60 | + opts.callback(item._path) |
| 61 | + else |
| 62 | + debug_once("find files jump: ", item) |
| 63 | + snacks_picker.actions.jump(picker, item, action) |
| 64 | + end |
| 65 | + end |
| 66 | + end, |
| 67 | + }) |
| 68 | + snacks_picker.pick(pick_opts) |
| 69 | +end |
| 70 | + |
| 71 | +---@param opts obsidian.PickerGrepOpts|? Options. |
| 72 | +SnacksPicker.grep = function(self, opts) |
| 73 | + opts = opts or {} |
| 74 | + |
| 75 | + debug_once("grep opts : ", opts) |
| 76 | + |
| 77 | + ---@type obsidian.Path |
| 78 | + local dir = opts.dir.filename and Path:new(opts.dir.filename) or self.client.dir |
| 79 | + |
| 80 | + local map = vim.tbl_deep_extend("force", {}, notes_mappings(opts.selection_mappings)) |
| 81 | + |
| 82 | + local pick_opts = vim.tbl_extend("force", map or {}, { |
| 83 | + source = "grep", |
| 84 | + title = opts.prompt_title, |
| 85 | + cwd = tostring(dir), |
| 86 | + confirm = function(picker, item, action) |
| 87 | + picker:close() |
| 88 | + if item then |
| 89 | + if opts.callback then |
| 90 | + debug_once("grep callback: ", item) |
| 91 | + opts.callback(item._path or item.filename) |
| 92 | + else |
| 93 | + debug_once("grep jump: ", item) |
| 94 | + snacks_picker.actions.jump(picker, item, action) |
| 95 | + end |
| 96 | + end |
| 97 | + end, |
| 98 | + }) |
| 99 | + snacks_picker.pick(pick_opts) |
| 100 | +end |
| 101 | + |
| 102 | +---@param values string[]|obsidian.PickerEntry[] |
| 103 | +---@param opts obsidian.PickerPickOpts|? Options. |
| 104 | +---@diagnostic disable-next-line: unused-local |
| 105 | +SnacksPicker.pick = function(self, values, opts) |
| 106 | + self.calling_bufnr = vim.api.nvim_get_current_buf() |
| 107 | + |
| 108 | + opts = opts or {} |
| 109 | + |
| 110 | + debug_once("pick opts: ", opts) |
| 111 | + |
| 112 | + local buf = opts.buf or vim.api.nvim_get_current_buf() |
| 113 | + |
| 114 | + local entries = {} |
| 115 | + for _, value in ipairs(values) do |
| 116 | + if type(value) == "string" then |
| 117 | + table.insert(entries, { |
| 118 | + text = value, |
| 119 | + value = value, |
| 120 | + }) |
| 121 | + elseif value.valid ~= false then |
| 122 | + local name = self:_make_display(value) |
| 123 | + table.insert(entries, { |
| 124 | + text = name, |
| 125 | + buf = buf, |
| 126 | + filename = value.filename, |
| 127 | + value = value.value, |
| 128 | + pos = { value.lnum, value.col or 0 }, |
| 129 | + }) |
| 130 | + end |
| 131 | + end |
| 132 | + |
| 133 | + local map = vim.tbl_deep_extend("force", {}, notes_mappings(opts.selection_mappings)) |
| 134 | + |
| 135 | + local pick_opts = vim.tbl_extend("force", map or {}, { |
| 136 | + tilte = opts.prompt_title, |
| 137 | + items = entries, |
| 138 | + layout = { |
| 139 | + preview = false, |
| 140 | + }, |
| 141 | + format = "text", |
| 142 | + confirm = function(picker, item, action) |
| 143 | + picker:close() |
| 144 | + if item then |
| 145 | + if opts.callback then |
| 146 | + debug_once("pick callback: ", item) |
| 147 | + opts.callback(item.value) |
| 148 | + else |
| 149 | + debug_once("pick jump: ", item) |
| 150 | + snacks_picker.actions.jump(picker, item, action) |
| 151 | + end |
| 152 | + end |
| 153 | + end, |
| 154 | + }) |
| 155 | + |
| 156 | + snacks_picker.pick(pick_opts) |
| 157 | +end |
| 158 | + |
| 159 | +return SnacksPicker |
0 commit comments