From 83992f01016d3e81e2cfe109c758f57cc076d4e2 Mon Sep 17 00:00:00 2001 From: "Tony Fischer (tku137)" Date: Sun, 26 Jan 2025 16:57:11 +0100 Subject: [PATCH 1/2] refactor(copilotchat-nvim): improve picker selection logic and error 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 --- .../editing-support/copilotchat-nvim/init.lua | 61 ++++++++++++++++--- 1 file changed, 53 insertions(+), 8 deletions(-) diff --git a/lua/astrocommunity/editing-support/copilotchat-nvim/init.lua b/lua/astrocommunity/editing-support/copilotchat-nvim/init.lua index 27c59dc08..effbdb914 100644 --- a/lua/astrocommunity/editing-support/copilotchat-nvim/init.lua +++ b/lua/astrocommunity/editing-support/copilotchat-nvim/init.lua @@ -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 From 7b9a2675d2b28ca480b228ce06eaa911ed1ff34e Mon Sep 17 00:00:00 2001 From: "Tony Fischer (tku137)" Date: Tue, 4 Feb 2025 20:46:06 +0100 Subject: [PATCH 2/2] refactor(copilotchat-nvim): improve snacks picker detection --- .../editing-support/copilotchat-nvim/init.lua | 20 +++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/lua/astrocommunity/editing-support/copilotchat-nvim/init.lua b/lua/astrocommunity/editing-support/copilotchat-nvim/init.lua index effbdb914..dc69c9a1e 100644 --- a/lua/astrocommunity/editing-support/copilotchat-nvim/init.lua +++ b/lua/astrocommunity/editing-support/copilotchat-nvim/init.lua @@ -69,16 +69,20 @@ return { -- 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 + -- Check 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 + -- 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)() + then + return "snacks" + elseif require("astrocore").is_available "fzf-lua" then return "fzflua" elseif require("astrocore").is_available "telescope.nvim" then return "telescope"