Skip to content

Commit

Permalink
impl for telescope
Browse files Browse the repository at this point in the history
  • Loading branch information
Futarimiti committed Oct 3, 2023
1 parent 18de661 commit 8f5777d
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 23 deletions.
11 changes: 6 additions & 5 deletions lua/spooky/templates/choose/builtin.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
42 changes: 24 additions & 18 deletions lua/spooky/templates/choose/telescope.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -21,15 +23,32 @@ 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, _)
local entry_name = entry[1]
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)
Expand All @@ -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, _)
Expand All @@ -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.]]
Expand Down

0 comments on commit 8f5777d

Please sign in to comment.