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 1 commit
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
61 changes: 53 additions & 8 deletions lua/astrocommunity/editing-support/copilotchat-nvim/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -67,17 +67,62 @@ return {
desc = "Load Chat",
}

-- Determine the currently active picker
local function get_active_picker()
-- Snacks integration check
-- this is tricky because we can't just assume that if snacks.nvim is
-- installed, it's being used as the picker, too
local snacks = require "snacks.picker"
if snacks.config and snacks.config.ui_select then return "snacks" end

-- Check other CopilotChat compatibel pickers
-- this is also a mapping between picker module names and their
-- counterparts in the CopilotChat integrations
if require("astrocore").is_available "fzf-lua" then
return "fzflua"
elseif require("astrocore").is_available "telescope.nvim" then
return "telescope"
end
mehalter marked this conversation as resolved.
Show resolved Hide resolved

-- 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")

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],
}
)
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

-- Use the selected picker module
picker_module.pick(items)
end
end

Expand Down
Loading