78 lines
2.6 KiB
Lua
78 lines
2.6 KiB
Lua
-- Custom project picker: selecting a project changes the working directory
|
|
-- and restores that project's last session (open buffers/windows) via
|
|
-- persistence.nvim. It does NOT open a file search afterwards (the default
|
|
-- LazyVim `util.project` behaviour opens `find_files` on select).
|
|
local function pick_project_cd()
|
|
local pickers = require("telescope.pickers")
|
|
local finders = require("telescope.finders")
|
|
local conf = require("telescope.config").values
|
|
local actions = require("telescope.actions")
|
|
local state = require("telescope.actions.state")
|
|
local history = require("project_nvim.utils.history")
|
|
local project = require("project_nvim.project")
|
|
|
|
local results = history.get_recent_projects()
|
|
-- Show most recent projects first.
|
|
for i = 1, math.floor(#results / 2) do
|
|
results[i], results[#results - i + 1] = results[#results - i + 1], results[i]
|
|
end
|
|
|
|
pickers
|
|
.new({}, {
|
|
prompt_title = "Recent Projects (cd only)",
|
|
finder = finders.new_table({
|
|
results = results,
|
|
entry_maker = function(entry)
|
|
local name = vim.fn.fnamemodify(entry, ":t")
|
|
return {
|
|
value = entry,
|
|
display = string.format("%-30s %s", name, entry),
|
|
ordinal = name .. " " .. entry,
|
|
}
|
|
end,
|
|
}),
|
|
previewer = false,
|
|
sorter = conf.generic_sorter({}),
|
|
attach_mappings = function(prompt_bufnr)
|
|
actions.select_default:replace(function()
|
|
local entry = state.get_selected_entry()
|
|
actions.close(prompt_bufnr)
|
|
if not entry then
|
|
return
|
|
end
|
|
if project.set_pwd(entry.value, "telescope") then
|
|
vim.notify("Changed project dir to " .. entry.value)
|
|
-- Restore the last session (open buffers/windows) for this dir.
|
|
local ok, persistence = pcall(require, "persistence")
|
|
if ok then
|
|
persistence.load()
|
|
end
|
|
end
|
|
end)
|
|
return true
|
|
end,
|
|
})
|
|
:find()
|
|
end
|
|
|
|
return {
|
|
-- This overrides the repository source for the built-in LazyVim extra
|
|
{
|
|
"ahmedkhalf/project.nvim",
|
|
name = "project_nvim", -- Keeps internal naming intact
|
|
url = "https://github.com/DrKJeff16/project.nvim", -- Directs lazy.nvim to download the fork
|
|
opts = {
|
|
manual_mode = false, -- Prevents root changing unexpectedly on every single file open
|
|
},
|
|
},
|
|
|
|
-- Override <leader>fp so it only cd's into the project.
|
|
{
|
|
"nvim-telescope/telescope.nvim",
|
|
optional = true,
|
|
keys = {
|
|
{ "<leader>fp", pick_project_cd, desc = "Projects (cd only)" },
|
|
},
|
|
},
|
|
}
|