Skip to content

Commit

Permalink
refactor(copilotchat-nvim): improve picker selection logic and error …
Browse files Browse the repository at this point in the history
…handling

The change improves how CopilotChat selects and validates UI pickers by:
- Adding a dedicated function to determine the active picker
- Attempt at checking if snacks.nvim is configured as the UI selector
- Adding more robust error handling with descriptive messages
  • Loading branch information
Tony Fischer (tku137) committed Jan 26, 2025
1 parent 0e1cf11 commit 83992f0
Showing 1 changed file with 53 additions and 8 deletions.
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

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

0 comments on commit 83992f0

Please sign in to comment.