nvim-setup/nvim/lua/popequer/pickers.lua

141 lines
4 KiB
Lua

local actions = require "telescope.actions"
local action_state = require "telescope.actions.state"
local finders = require "telescope.finders"
local make_entry = require "telescope.make_entry"
local operators = require "telescope.operators"
local pickers = require "telescope.pickers"
local previewers = require "telescope.previewers"
local utils = require "telescope.utils"
local entry_display = require "telescope.pickers.entry_display"
local strings = require "plenary.strings"
local Path = require "plenary.path"
local conf = require("telescope.config").values
local git_command = utils.__git_command
local ppq_pickers = {}
local get_git_command_output = function(args, opts)
return utils.get_os_command_output(git_command(args, opts), opts.cwd)
end
ppq_pickers.files = function(opts)
if opts.is_bare then
utils.notify("builtin.git_files", {
msg = "This operation must be run in a work tree",
level = "ERROR",
})
return
end
local show_untracked = vim.F.if_nil(opts.show_untracked, false)
local recurse_submodules = vim.F.if_nil(opts.recurse_submodules, false)
if show_untracked and recurse_submodules then
utils.notify("builtin.git_files", {
msg = "Git does not support both --others and --recurse-submodules",
level = "ERROR",
})
return
end
-- By creating the entry maker after the cwd options,
-- we ensure the maker uses the cwd options when being created.
opts.entry_maker = vim.F.if_nil(opts.entry_maker, make_entry.gen_from_file(opts))
opts.git_command = vim.F.if_nil(opts.git_command, git_command({ "ls-files", "--exclude-standard", "--cached" }, opts))
pickers
.new(opts, {
prompt_title = "Git Files",
finder = finders.new_oneshot_job(
vim.tbl_flatten {
opts.git_command,
show_untracked and "--others" or nil,
recurse_submodules and "--recurse-submodules" or nil,
},
opts
),
previewer = conf.file_previewer(opts),
sorter = conf.file_sorter(opts),
})
:find()
end
local get_current_buf_line = function(winnr)
local lnum = vim.api.nvim_win_get_cursor(winnr)[1]
return vim.trim(vim.api.nvim_buf_get_lines(vim.api.nvim_win_get_buf(winnr), lnum - 1, lnum, false)[1])
end
local try_worktrees = function(opts)
local worktrees = conf.git_worktrees
if vim.tbl_isarray(worktrees) then
for _, wt in ipairs(worktrees) do
if vim.startswith(opts.cwd, wt.toplevel) then
opts.toplevel = wt.toplevel
opts.gitdir = wt.gitdir
if opts.use_git_root then
opts.cwd = wt.toplevel
end
return
end
end
end
error(opts.cwd .. " is not a git directory")
end
local current_path_toplevel = function()
local gitdir = vim.fn.finddir(".git", vim.fn.expand "%:p" .. ";")
if gitdir == "" then
return nil
end
return Path:new(gitdir):parent():absolute()
end
local set_opts_cwd = function(opts)
opts.use_git_root = vim.F.if_nil(opts.use_git_root, true)
if opts.cwd then
opts.cwd = vim.fn.expand(opts.cwd)
elseif opts.use_file_path then
opts.cwd = current_path_toplevel()
if not opts.cwd then
opts.cwd = vim.fn.expand "%:p:h"
try_worktrees(opts)
return
end
else
opts.cwd = vim.loop.cwd()
end
local toplevel, ret = utils.get_os_command_output({ "git", "rev-parse", "--show-toplevel" }, opts.cwd)
if ret ~= 0 then
local in_worktree = utils.get_os_command_output({ "git", "rev-parse", "--is-inside-work-tree" }, opts.cwd)
local in_bare = utils.get_os_command_output({ "git", "rev-parse", "--is-bare-repository" }, opts.cwd)
if in_worktree[1] ~= "true" and in_bare[1] ~= "true" then
try_worktrees(opts)
elseif in_worktree[1] ~= "true" and in_bare[1] == "true" then
opts.is_bare = true
end
else
if opts.use_git_root then
opts.cwd = toplevel[1]
end
end
end
local function apply_checks(mod)
for k, v in pairs(mod) do
mod[k] = function(opts)
opts = vim.F.if_nil(opts, {})
set_opts_cwd(opts)
v(opts)
end
end
return mod
end
return apply_checks(ppq_pickers)