|
| 1 | +local snacks_picker = require "snacks.picker" |
| 2 | +local snacks = require "snacks" |
| 3 | + |
| 4 | + |
| 5 | +local Path = require "obsidian.path" |
| 6 | +local abc = require "obsidian.abc" |
| 7 | +local Picker = require "obsidian.pickers.picker" |
| 8 | + |
| 9 | +---@param entry string |
| 10 | +---@return string |
| 11 | +local function clean_path(entry) |
| 12 | + if type(entry) == "string" then |
| 13 | + local path_end = assert(string.find(entry, ":", 1, true)) |
| 14 | + return string.sub(entry, 1, path_end - 1) |
| 15 | + end |
| 16 | + vim.notify("entry: " .. table.concat(vim.tbl_keys(entry), ", ")) |
| 17 | + return "" |
| 18 | +end |
| 19 | + |
| 20 | +---@class obsidian.pickers.SnacksPicker : obsidian.Picker |
| 21 | +local SnacksPicker = abc.new_class({ |
| 22 | + ---@diagnostic disable-next-line: unused-local |
| 23 | + __tostring = function(self) |
| 24 | + return "SnacksPicker()" |
| 25 | + end, |
| 26 | +}, Picker) |
| 27 | + |
| 28 | +SnacksPicker.find_files = function(self, opts) |
| 29 | + opts = opts and opts or {} |
| 30 | + |
| 31 | + ---@type obsidian.Path |
| 32 | + local dir = opts.dir and Path:new(opts.dir) or self.client.dir |
| 33 | + |
| 34 | + local result = snacks_picker.pick("files", { |
| 35 | + cwd = tostring(dir), |
| 36 | + }) |
| 37 | + |
| 38 | + if result and opts.callback then |
| 39 | + local path = clean_path(result) |
| 40 | + opts.callback(tostring(dir / path)) |
| 41 | + end |
| 42 | +end |
| 43 | + |
| 44 | +SnacksPicker.grep = function(self, opts) |
| 45 | + opts = opts and opts or {} |
| 46 | + |
| 47 | + ---@type obsidian.Path |
| 48 | + local dir = opts.dir and Path:new(opts.dir) or self.client.dir |
| 49 | + |
| 50 | + local result = snacks_picker.pick("grep", { |
| 51 | + cwd = tostring(dir), |
| 52 | + }) |
| 53 | + |
| 54 | + if result and opts.callback then |
| 55 | + local path = clean_path(result) |
| 56 | + opts.callback(tostring(dir / path)) |
| 57 | + end |
| 58 | +end |
| 59 | + |
| 60 | +SnacksPicker.pick = function(self, values, opts) |
| 61 | + |
| 62 | + self.calling_bufnr = vim.api.nvim_get_current_buf() |
| 63 | + |
| 64 | + local buf = opts.buf or vim.api.nvim_get_current_buf() |
| 65 | + |
| 66 | + opts = opts and opts or {} |
| 67 | + |
| 68 | + local entries = {} |
| 69 | + for _, value in ipairs(values) do |
| 70 | + if type(value) == "string" then |
| 71 | + table.insert(entries, { |
| 72 | + text = value, |
| 73 | + value = value, |
| 74 | + }) |
| 75 | + elseif value.valid ~= false then |
| 76 | + local name = self:_make_display(value) |
| 77 | + table.insert(entries, { |
| 78 | + text = name, |
| 79 | + buf = buf, |
| 80 | + filename = value.filename, |
| 81 | + value = value.value, |
| 82 | + pos = { value.lnum, value.col }, |
| 83 | + }) |
| 84 | + end |
| 85 | + end |
| 86 | + |
| 87 | + snacks_picker({ |
| 88 | + tilte = opts.prompt_title, |
| 89 | + items = entries, |
| 90 | + layout = { |
| 91 | + preview = false |
| 92 | + }, |
| 93 | + format = function(item, _) |
| 94 | + local ret = {} |
| 95 | + local a = snacks_picker.util.align |
| 96 | + ret[#ret + 1] = { a(item.text, 20) } |
| 97 | + return ret |
| 98 | + end, |
| 99 | + confirm = function(picker, item) |
| 100 | + picker:close() |
| 101 | + if item then |
| 102 | + if opts.callback then |
| 103 | + opts.callback(item.value) |
| 104 | + elseif item then |
| 105 | + vim.schedule(function() |
| 106 | + if item["buf"] then |
| 107 | + vim.api.nvim_set_current_buf(item["buf"]) |
| 108 | + end |
| 109 | + vim.api.nvim_win_set_cursor(0, {item["pos"][1], 0}) |
| 110 | + end) |
| 111 | + end |
| 112 | + end |
| 113 | + end, |
| 114 | + -- sort = require("snacks.picker.sort").idx(), |
| 115 | + }) |
| 116 | +end |
| 117 | + |
| 118 | +return SnacksPicker |
0 commit comments