From eda0c97f4a75eef12f6b58f7e9544bf76562581f Mon Sep 17 00:00:00 2001 From: wsdjeg Date: Mon, 19 Jun 2023 23:59:08 +0800 Subject: [PATCH] feat(project): improve `Telescope project` --- lua/spacevim/plugin/projectmanager.lua | 9 +- lua/telescope/_extensions/project.lua | 118 +++++++++++++------------ 2 files changed, 68 insertions(+), 59 deletions(-) diff --git a/lua/spacevim/plugin/projectmanager.lua b/lua/spacevim/plugin/projectmanager.lua index 06aeeafb9..773812668 100644 --- a/lua/spacevim/plugin/projectmanager.lua +++ b/lua/spacevim/plugin/projectmanager.lua @@ -297,7 +297,7 @@ function M.list() elseif layer.isLoaded('leaderf') then sp.cmd("call SpaceVim#layers#leaderf#run_menu('Projects')") elseif layer.isLoaded('telescope') then - sp.cmd('Telescope menu menu=Projects') + sp.cmd('Telescope project') else logger.warn('fuzzy find layer is needed to find project!') end @@ -305,7 +305,7 @@ end function M.open(project) local path = project_paths[project]['path'] - local name = project_paths[project]['name'] + -- local name = project_paths[project]['name'] sp.cmd('tabnew') -- I am not sure we should set the project name here. -- sp.cmd('let t:_spacevim_tab_name = "[' .. name .. ']"') @@ -449,4 +449,9 @@ function M.current_root() return rootdir end +function M.get_project_history() -- {{{ + return project_paths +end +-- }}} + return M diff --git a/lua/telescope/_extensions/project.lua b/lua/telescope/_extensions/project.lua index bd54f579f..1e85bb6fb 100644 --- a/lua/telescope/_extensions/project.lua +++ b/lua/telescope/_extensions/project.lua @@ -1,71 +1,75 @@ -local action_state = require("telescope.actions.state") -local actions = require("telescope.actions") -local conf = require("telescope.config").values -local entry_display = require("telescope.pickers.entry_display") -local finders = require("telescope.finders") -local pickers = require("telescope.pickers") +local action_state = require('telescope.actions.state') +local actions = require('telescope.actions') +local conf = require('telescope.config').values +local entry_display = require('telescope.pickers.entry_display') +local finders = require('telescope.finders') +local pickers = require('telescope.pickers') +local pm = require('spacevim.plugin.projectmanager') local function get_all_projects() - local p = {} - local projects = vim.api.nvim_eval('g:unite_source_menu_menus.Projects.command_candidates') + local p = {} + local projects = pm.get_project_history() - for _, project in pairs(projects) do - table.insert(p, project) - end - return p + for _, k in pairs(projects) do + table.insert(p, k) + end + return p end local function show_changes(opts) - opts = opts or {} - local displayer = entry_display.create({ - separator = ' ', - items = { - { width = 20 }, - { width = vim.api.nvim_win_get_width(0) - 60 }, - { remaining = true }, - }, + opts = opts or {} + local displayer = entry_display.create({ + separator = ' ', + items = { + { width = 20 }, + { width = vim.api.nvim_win_get_width(0) - 60 }, + { remaining = true }, + }, + }) + local function make_display(entry) + -- print(vim.inspect(entry)) + return displayer({ + { '[' .. entry.value.name .. ']', 'TelescopeResultsVariable' }, + { entry.value.path, 'TelescopeResultsFunction' }, + { + '<' .. vim.fn.strftime('%Y-%m-%d %T', entry.value.opened_time) .. '>', + 'TelescopeResultsComment', + }, }) - local function make_display(entry) - -- print(vim.inspect(entry)) - return displayer({ - { '[' .. entry.value.name .. ']', 'TelescopeResultsVariable' }, - { entry.value.path, 'TelescopeResultsFunction' }, - { '<' .. vim.fn.strftime('%Y-%m-%d %T', entry.value.opened_time) .. '>', 'TelescopeResultsComment' }, - }) - end - pickers.new(opts, { - prompt_title = "Projects", - finder = finders.new_table { - results = get_all_projects(), - entry_maker = function(entry) - return { - value = entry[3], - command = entry[2], - display = make_display, - ordinal = entry[1] - } - end - }, - sorter = conf.generic_sorter(opts), - attach_mappings = function(prompt_bufnr) - actions.select_default:replace(function() - local entry = action_state.get_selected_entry() - actions.close(prompt_bufnr) - vim.cmd(entry.command) - end) - return true + end + pickers + .new(opts, { + prompt_title = 'Projects', + finder = finders.new_table({ + results = get_all_projects(), + entry_maker = function(entry) + return { + value = entry, + display = make_display, + ordinal = entry.name, + } end, - }):find() + }), + sorter = conf.generic_sorter(opts), + attach_mappings = function(prompt_bufnr) + actions.select_default:replace(function() + local entry = action_state.get_selected_entry() + actions.close(prompt_bufnr) + pm.open(entry.value.path) + end) + return true + end, + }) + :find() end local function run() - show_changes() + show_changes() end -return require("telescope").register_extension({ - exports = { - -- Default when to argument is given, i.e. :Telescope project - project = run, - }, +return require('telescope').register_extension({ + exports = { + -- Default when to argument is given, i.e. :Telescope project + project = run, + }, }) -