LazyVim.LazyVim/lua/lazyvim/plugins/extras/util/project.lua
Iordanis Petkakis c09735594a
fix(project.nvim): correctly delete projects (#4314)
## Description
Unfortunately, the upstream Github repo hasn't been active for over a
year. This overwrites the `history.delete_project` function to correctly
be able to delete projects. Because of `ipairs` being used, when you
delete the first key from the table the `for` loop iteration will just
stop, since there will be a gap between the available indices.
> will iterate over the pairs (`1,t[1]`), (`2,t[2]`), ..., up to the
first integer key absent from the table.

There's also a
[PR](https://github.com/ahmedkhalf/project.nvim/pull/106/files) for this
upstream, but like I mentioned the Github repo hasn't been active for
over a year.

Feel free to disregard, since this is obviously something that should be
rather fixed upstream under normal circumstances. I just created this
PR, since it's such a basic functionality with little change in
LazyVim's codebase, that I thought it might be acceptable.
<!-- Describe the big picture of your changes to communicate to the
maintainers
  why we should accept this pull request. -->

## Related Issue(s)
No, rather a discussion #4310
<!--
  If this PR fixes any issues, please link to the issue here.
  - Fixes #<issue_number>
-->

## Screenshots

<!-- Add screenshots of the changes if applicable. -->

## Checklist

- [x] I've read the
[CONTRIBUTING](https://github.com/LazyVim/LazyVim/blob/main/CONTRIBUTING.md)
guidelines.
2024-11-08 13:43:11 +01:00

168 lines
4.4 KiB
Lua

local pick = nil
pick = function()
if LazyVim.pick.picker.name == "telescope" then
return vim.cmd("Telescope projects")
elseif LazyVim.pick.picker.name == "fzf" then
local fzf_lua = require("fzf-lua")
local project = require("project_nvim.project")
local history = require("project_nvim.utils.history")
local results = history.get_recent_projects()
local utils = require("fzf-lua.utils")
local function hl_validate(hl)
return not utils.is_hl_cleared(hl) and hl or nil
end
local function ansi_from_hl(hl, s)
return utils.ansi_from_hl(hl_validate(hl), s)
end
local opts = {
fzf_opts = {
["--header"] = string.format(
":: <%s> to %s | <%s> to %s | <%s> to %s | <%s> to %s | <%s> to %s",
ansi_from_hl("FzfLuaHeaderBind", "ctrl-t"),
ansi_from_hl("FzfLuaHeaderText", "tabedit"),
ansi_from_hl("FzfLuaHeaderBind", "ctrl-s"),
ansi_from_hl("FzfLuaHeaderText", "live_grep"),
ansi_from_hl("FzfLuaHeaderBind", "ctrl-r"),
ansi_from_hl("FzfLuaHeaderText", "oldfiles"),
ansi_from_hl("FzfLuaHeaderBind", "ctrl-w"),
ansi_from_hl("FzfLuaHeaderText", "change_dir"),
ansi_from_hl("FzfLuaHeaderBind", "ctrl-d"),
ansi_from_hl("FzfLuaHeaderText", "delete")
),
},
fzf_colors = true,
actions = {
["default"] = {
function(selected)
fzf_lua.files({ cwd = selected[1] })
end,
},
["ctrl-t"] = {
function(selected)
vim.cmd("tabedit")
fzf_lua.files({ cwd = selected[1] })
end,
},
["ctrl-s"] = {
function(selected)
fzf_lua.live_grep({ cwd = selected[1] })
end,
},
["ctrl-r"] = {
function(selected)
fzf_lua.oldfiles({ cwd = selected[1] })
end,
},
["ctrl-w"] = {
function(selected)
local path = selected[1]
local ok = project.set_pwd(path)
if ok then
vim.api.nvim_win_close(0, false)
LazyVim.info("Change project dir to " .. path)
end
end,
},
["ctrl-d"] = function(selected)
local path = selected[1]
local choice = vim.fn.confirm("Delete '" .. path .. "' project? ", "&Yes\n&No")
if choice == 1 then
history.delete_project({ value = path })
end
pick()
end,
},
}
fzf_lua.fzf_exec(results, opts)
end
end
return {
{
"ahmedkhalf/project.nvim",
opts = {
manual_mode = true,
},
event = "VeryLazy",
config = function(_, opts)
require("project_nvim").setup(opts)
local history = require("project_nvim.utils.history")
history.delete_project = function(project)
for k, v in pairs(history.recent_projects) do
if v == project.value then
history.recent_projects[k] = nil
return
end
end
end
LazyVim.on_load("telescope.nvim", function()
require("telescope").load_extension("projects")
end)
end,
},
{
"nvim-telescope/telescope.nvim",
optional = true,
keys = {
{ "<leader>fp", pick, desc = "Projects" },
},
},
{
"ibhagwan/fzf-lua",
optional = true,
keys = {
{ "<leader>fp", pick, desc = "Projects" },
},
},
{
"goolord/alpha-nvim",
optional = true,
opts = function(_, dashboard)
local button = dashboard.button("p", "" .. " Projects", pick)
button.opts.hl = "AlphaButtons"
button.opts.hl_shortcut = "AlphaShortcut"
table.insert(dashboard.section.buttons.val, 4, button)
end,
},
{
"echasnovski/mini.starter",
optional = true,
opts = function(_, opts)
local items = {
{
name = "Projects",
action = pick,
section = string.rep(" ", 22) .. "Telescope",
},
}
vim.list_extend(opts.items, items)
end,
},
{
"nvimdev/dashboard-nvim",
optional = true,
opts = function(_, opts)
local projects = {
action = pick,
desc = " Projects",
icon = "",
key = "p",
}
projects.desc = projects.desc .. string.rep(" ", 43 - #projects.desc)
projects.key_format = " %s"
table.insert(opts.config.center, 3, projects)
end,
},
}