1
0
mirror of https://github.com/SpaceVim/SpaceVim.git synced 2025-03-16 12:15:46 +08:00

feat(project): improve Telescope project

This commit is contained in:
wsdjeg 2023-06-19 23:59:08 +08:00
parent 472e982bb1
commit eda0c97f4a
2 changed files with 68 additions and 59 deletions

View File

@ -297,7 +297,7 @@ function M.list()
elseif layer.isLoaded('leaderf') then elseif layer.isLoaded('leaderf') then
sp.cmd("call SpaceVim#layers#leaderf#run_menu('Projects')") sp.cmd("call SpaceVim#layers#leaderf#run_menu('Projects')")
elseif layer.isLoaded('telescope') then elseif layer.isLoaded('telescope') then
sp.cmd('Telescope menu menu=Projects') sp.cmd('Telescope project')
else else
logger.warn('fuzzy find layer is needed to find project!') logger.warn('fuzzy find layer is needed to find project!')
end end
@ -305,7 +305,7 @@ end
function M.open(project) function M.open(project)
local path = project_paths[project]['path'] local path = project_paths[project]['path']
local name = project_paths[project]['name'] -- local name = project_paths[project]['name']
sp.cmd('tabnew') sp.cmd('tabnew')
-- I am not sure we should set the project name here. -- I am not sure we should set the project name here.
-- sp.cmd('let t:_spacevim_tab_name = "[' .. name .. ']"') -- sp.cmd('let t:_spacevim_tab_name = "[' .. name .. ']"')
@ -449,4 +449,9 @@ function M.current_root()
return rootdir return rootdir
end end
function M.get_project_history() -- {{{
return project_paths
end
-- }}}
return M return M

View File

@ -1,16 +1,17 @@
local action_state = require("telescope.actions.state") local action_state = require('telescope.actions.state')
local actions = require("telescope.actions") local actions = require('telescope.actions')
local conf = require("telescope.config").values local conf = require('telescope.config').values
local entry_display = require("telescope.pickers.entry_display") local entry_display = require('telescope.pickers.entry_display')
local finders = require("telescope.finders") local finders = require('telescope.finders')
local pickers = require("telescope.pickers") local pickers = require('telescope.pickers')
local pm = require('spacevim.plugin.projectmanager')
local function get_all_projects() local function get_all_projects()
local p = {} local p = {}
local projects = vim.api.nvim_eval('g:unite_source_menu_menus.Projects.command_candidates') local projects = pm.get_project_history()
for _, project in pairs(projects) do for _, k in pairs(projects) do
table.insert(p, project) table.insert(p, k)
end end
return p return p
end end
@ -30,42 +31,45 @@ local function show_changes(opts)
return displayer({ return displayer({
{ '[' .. entry.value.name .. ']', 'TelescopeResultsVariable' }, { '[' .. entry.value.name .. ']', 'TelescopeResultsVariable' },
{ entry.value.path, 'TelescopeResultsFunction' }, { entry.value.path, 'TelescopeResultsFunction' },
{ '<' .. vim.fn.strftime('%Y-%m-%d %T', entry.value.opened_time) .. '>', 'TelescopeResultsComment' }, {
'<' .. vim.fn.strftime('%Y-%m-%d %T', entry.value.opened_time) .. '>',
'TelescopeResultsComment',
},
}) })
end end
pickers.new(opts, { pickers
prompt_title = "Projects", .new(opts, {
finder = finders.new_table { prompt_title = 'Projects',
finder = finders.new_table({
results = get_all_projects(), results = get_all_projects(),
entry_maker = function(entry) entry_maker = function(entry)
return { return {
value = entry[3], value = entry,
command = entry[2],
display = make_display, display = make_display,
ordinal = entry[1] ordinal = entry.name,
} }
end end,
}, }),
sorter = conf.generic_sorter(opts), sorter = conf.generic_sorter(opts),
attach_mappings = function(prompt_bufnr) attach_mappings = function(prompt_bufnr)
actions.select_default:replace(function() actions.select_default:replace(function()
local entry = action_state.get_selected_entry() local entry = action_state.get_selected_entry()
actions.close(prompt_bufnr) actions.close(prompt_bufnr)
vim.cmd(entry.command) pm.open(entry.value.path)
end) end)
return true return true
end, end,
}):find() })
:find()
end end
local function run() local function run()
show_changes() show_changes()
end end
return require("telescope").register_extension({ return require('telescope').register_extension({
exports = { exports = {
-- Default when to argument is given, i.e. :Telescope project -- Default when to argument is given, i.e. :Telescope project
project = run, project = run,
}, },
}) })