diff --git a/lua/spooky/templates/choose/builtin.lua b/lua/spooky/templates/choose/builtin.lua index fa3675a..1fb2c5c 100644 --- a/lua/spooky/templates/choose/builtin.lua +++ b/lua/spooky/templates/choose/builtin.lua @@ -6,19 +6,20 @@ M.choose_one = function (fullpaths, user, do_with_choice) local no_template = user.ui.no_template local show_full = user.ui.select_full_path local show_no_template = user.show_no_template - local representation = (function () - local ret = {} + local representation, mappings = (function () + local ret, map = {}, {} for _, fullpath in ipairs(fullpaths) do local path = show_full and fullpath or vim.fn.fnamemodify(fullpath, ':t:r') table.insert(ret, path) + map[path] = fullpath end if show_no_template then table.insert(ret, no_template) end - return ret + return ret, map end)() - vim.ui.select(representation, { prompt = user.ui.prompt }, function (choice, idx) + vim.ui.select(representation, { prompt = user.ui.prompt }, function (choice, _) if choice == nil or choice == no_template then return end - local fullpath = fullpaths[idx] + local fullpath = assert(mappings[choice]) do_with_choice(fullpath) end) end diff --git a/lua/spooky/templates/choose/telescope.lua b/lua/spooky/templates/choose/telescope.lua index 5a307ad..2ddbdf2 100644 --- a/lua/spooky/templates/choose/telescope.lua +++ b/lua/spooky/templates/choose/telescope.lua @@ -5,13 +5,15 @@ local M = {} -- Telescope picker to select a single item from a list -- and return the selected item, or nil if none selected. -- User should have Telescope dependency installed. -M.choose_one = function (buf, items, user, do_with_choice) +M.choose_one = function (buf, fullpaths, user, do_with_choice) assert(require 'telescope', 'Telescope is not installed') local opts = user.ui.telescope_opts local prompt = user.ui.prompt local previewer_prompt = user.ui.previewer_prompt local no_template = user.ui.no_template + local show_full = user.ui.select_full_path + local show_no_template = user.ui.show_no_template local pickers = require 'telescope.pickers' local finders = require 'telescope.finders' @@ -21,6 +23,23 @@ M.choose_one = function (buf, items, user, do_with_choice) local action_state = require 'telescope.actions.state' local util = require 'telescope.previewers.utils' + -- Alright, so this could seem confusing, but there's good reason. + -- `representation` is the list of filepaths, full or not full, + -- that will be displayed in the picker as the options to choose from. + -- `mappings` is a table whose keys are the options to choose from, + -- and values are the full filepaths, + -- except for the `no_template` option, which has no value. + local representation, mappings = (function () + local ret, map = {}, {} + for _, fullpath in ipairs(fullpaths) do + local path = show_full and fullpath or vim.fn.fnamemodify(fullpath, ':t:r') + table.insert(ret, path) + map[path] = fullpath + end + if show_no_template then table.insert(ret, no_template) end + return ret, map + end)() + local previewer = previewers.new_buffer_previewer { title = previewer_prompt , define_preview = function (self, entry, _) @@ -28,8 +47,8 @@ M.choose_one = function (buf, items, user, do_with_choice) if entry_name == no_template then vim.api.nvim_buf_set_lines(self.state.bufnr, 0, -1, false, { '' }) else - local filepath = entry_name - local content = vim.fn.readfile(filepath) + local fullpath = mappings[entry_name] + local content = vim.fn.readfile(fullpath) local result = (function () if user.ui.preview_normalised then local normalised, _ = require('spooky.templates.normalisation').normalise(buf, content) @@ -51,21 +70,8 @@ M.choose_one = function (buf, items, user, do_with_choice) end } - local telescope_choices = (function () - if not user.show_no_template then - return items - end - - local ret = {} - table.insert(ret, no_template) - for _, item in ipairs(items) do - table.insert(ret, item) - end - return ret - end)() - pickers.new(opts, { prompt_title = prompt - , finder = finders.new_table { results = telescope_choices } + , finder = finders.new_table { results = representation } , sorter = conf.generic_sorter(opts) , previewer = previewer , attach_mappings = function (prompt_buf, _) @@ -75,7 +81,7 @@ M.choose_one = function (buf, items, user, do_with_choice) if #selection == 1 then local choice = selection[1] if choice == no_template then return end - do_with_choice(choice) + do_with_choice(mappings[choice]) elseif #selection ~= 0 then error [[[spooky] Tbh I'm not sure how to do with multiple selections. Please, just pick 1 for now.]]