Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(copilotchat-nvim): improve picker selection logic and error handling #1337

Merged
merged 2 commits into from
Feb 4, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 56 additions & 7 deletions lua/astrocommunity/editing-support/copilotchat-nvim/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -67,17 +67,66 @@ return {
desc = "Load Chat",
}

-- Determine the currently active picker
local function get_active_picker()
-- Check CopilotChat compatibel pickers
-- this is also a mapping between picker module names and their
-- counterparts in the CopilotChat integrations
-- snacks.picker is tricky because we can't just assume that if snacks.nvim is
-- installed, it's being used as the picker, too
if
require("astrocore").is_available "snacks.nvim"
and (function()
local snacks = require "snacks.picker"
return snacks.config and snacks.config.ui_select
end)()
mehalter marked this conversation as resolved.
Show resolved Hide resolved
then
return "snacks"
elseif require("astrocore").is_available "fzf-lua" then
return "fzflua"
elseif require("astrocore").is_available "telescope.nvim" then
return "telescope"
end

-- Default fallback
return nil
end

-- Helper function to create mappings
local function create_mapping(action_type, selection_type)
return function()
local fzf_ok = pcall(require, "fzf-lua")
local snacks_ok = pcall(require, "snacks")
local actions = require "CopilotChat.actions"
local items = actions[action_type] { selection = require("CopilotChat.select")[selection_type] }
if not items then
vim.notify("No " .. action_type:gsub("_", " ") .. " found for the current selection", vim.log.levels.WARN)
return
end

-- Determine the active picker
local picker = get_active_picker()
if not picker then
vim.notify(
"No valid picker is enabled. Please enable one of telescope, fzf-lua, or snacks.",
vim.log.levels.ERROR
)
return
end

-- Attempt to load the picker module
local ok, picker_module = pcall(require, "CopilotChat.integrations." .. picker)
if not ok then
vim.notify(
("Integration module '%s' for picker '%s' is not available. Ensure it is installed and enabled."):format(
picker,
picker
),
vim.log.levels.WARN
)
return
end

require("CopilotChat.integrations." .. (fzf_ok and "fzflua" or snacks_ok and "snacks" or "telescope")).pick(
require("CopilotChat.actions")[action_type] {
selection = require("CopilotChat.select")[selection_type],
}
)
-- Use the selected picker module
picker_module.pick(items)
end
end

Expand Down