Skip to content

Commit

Permalink
Merge branch 'main' of github.com:CopilotC-Nvim/CopilotChat.nvim
Browse files Browse the repository at this point in the history
  • Loading branch information
cjoke committed Feb 17, 2024
2 parents 0c3586e + fe1808e commit 5970301
Show file tree
Hide file tree
Showing 8 changed files with 324 additions and 6 deletions.
18 changes: 18 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
# Changelog

## [1.5.0](https://github.com/CopilotC-Nvim/CopilotChat.nvim/compare/v1.4.0...v1.5.0) (2024-02-17)


### Features

* add options to hide system prompts ([98a6191](https://github.com/CopilotC-Nvim/CopilotChat.nvim/commit/98a61913f4cd798fb042f4b21f6a3e1a457c3959))
* add prompt actions support in Telescope integration ([f124645](https://github.com/CopilotC-Nvim/CopilotChat.nvim/commit/f124645d4b48df59790c9763687b94cf7dd3f5bf))
* integrate CopilotChat with telescope.nvim for code actions ([0cabac6](https://github.com/CopilotC-Nvim/CopilotChat.nvim/commit/0cabac6af8c838d4984b766f5d985a04259d3a4d))

## [1.4.0](https://github.com/CopilotC-Nvim/CopilotChat.nvim/compare/v1.3.0...v1.4.0) (2024-02-16)


### Features

* add diagnostic troubleshooting command ([0e5eced](https://github.com/CopilotC-Nvim/CopilotChat.nvim/commit/0e5ecedda4d7a9cc6eeef1424889d8d9550bf4f3))
* add toggle command for vertical split in CopilotChat ([48209d6](https://github.com/CopilotC-Nvim/CopilotChat.nvim/commit/48209d6b98cb50c9dae59da70ebda351282cf8f7))
* **integration:** set filetype to 'copilot-chat' for support edgy.nvim ([60718ed](https://github.com/CopilotC-Nvim/CopilotChat.nvim/commit/60718ed6e806fa86fd78cb3bf55a05f1a74b257e))

## [1.3.0](https://github.com/CopilotC-Nvim/CopilotChat.nvim/compare/v1.2.0...v1.3.0) (2024-02-14)


Expand Down
48 changes: 48 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,9 @@ You have the ability to tailor this plugin to your specific needs using the conf
{
debug = false, -- Enable or disable debug mode
show_help = 'yes', -- Show help text for CopilotChatInPlace
disable_extra_info = 'no', -- Disable extra information in the response
hide_system_prompt = 'yes', -- Hide system prompts in the response
proxy = '', -- Proxies requests via https or socks
prompts = { -- Set dynamic prompts for CopilotChat commands
Explain = 'Explain how it works.',
Tests = 'Briefly explain how the selected code works, then generate unit tests.',
Expand Down Expand Up @@ -212,6 +215,51 @@ For further reference, you can view @jellydn's [configuration](https://github.co

## Tips

### Integration with `telescope.nvim`

To integrate CopilotChat with Telescope, you can add the following configuration to your keymap:

```lua
{
"CopilotC-Nvim/CopilotChat.nvim",
event = "VeryLazy",
dependencies = {
{ "nvim-telescope/telescope.nvim" }, -- Use telescope for help actions
{ "nvim-lua/plenary.nvim" },
},
keys = {
-- Show help actions with telescope
{
"<leader>cch",
function()
require("CopilotChat.code_actions").show_help_actions()
end,
desc = "CopilotChat - Help actions",
},
-- Show prompts actions with telescope
{
"<leader>ccp",
function()
require("CopilotChat.code_actions").show_prompt_actions()
end,
desc = "CopilotChat - Help actions",
},
{
"<leader>ccp",
":lua require('CopilotChat.code_actions').show_prompt_actions(true)<CR>",
mode = "x",
desc = "CopilotChat - Prompt actions",
},
}
}
```

1. Select help actions base the diagnostic message under the cursor.
[![Help action with Copilot Chat](https://i.gyazo.com/146dc35368592ba9f5de047ddc4728ad.gif)](https://gyazo.com/146dc35368592ba9f5de047ddc4728ad)

2. Select action base on user prompts.
[![Select action base on user prompts](https://i.gyazo.com/a9c41e6398591c2f1d1d872fd58a2c63.gif)](https://gyazo.com/a9c41e6398591c2f1d1d872fd58a2c63)

### Integration with `edgy.nvim`

Consider integrating this plugin with [`edgy.nvim`](https://github.com/folke/edgy.nvim). This will allow you to create a chat window on the right side of your screen, occupying 40% of the width, as illustrated below.
Expand Down
65 changes: 60 additions & 5 deletions doc/CopilotChat.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
*CopilotChat.txt* For NVIM v0.8.0 Last change: 2024 February 16
*CopilotChat.txt* For NVIM v0.8.0 Last change: 2024 February 17

==============================================================================
Table of Contents *CopilotChat-table-of-contents*
Expand Down Expand Up @@ -148,6 +148,9 @@ configuration options outlined below:
{
debug = false, -- Enable or disable debug mode
show_help = 'yes', -- Show help text for CopilotChatInPlace
disable_extra_info = 'no', -- Disable extra information in the response
hide_system_prompt = 'yes', -- Hide system prompts in the response
proxy = '', -- Proxies requests via https or socks
prompts = { -- Set dynamic prompts for CopilotChat commands
Explain = 'Explain how it works.',
Tests = 'Briefly explain how the selected code works, then generate unit tests.',
Expand Down Expand Up @@ -242,6 +245,56 @@ TOGGLE VERTICAL SPLIT WITH :COPILOTCHATVSPLITTOGGLE ~
TIPS *CopilotChat-copilot-chat-for-neovim-tips*


INTEGRATION WITH TELESCOPE.NVIM ~

To integrate CopilotChat with Telescope, you can add the following
configuration to your keymap:

>lua
{
"CopilotC-Nvim/CopilotChat.nvim",
event = "VeryLazy",
dependencies = {
{ "nvim-telescope/telescope.nvim" }, -- Use telescope for help actions
{ "nvim-lua/plenary.nvim" },
},
keys = {
-- Show help actions with telescope
{
"<leader>cch",
function()
require("CopilotChat.code_actions").show_help_actions()
end,
desc = "CopilotChat - Help actions",
},
-- Show prompts actions with telescope
{
"<leader>ccp",
function()
require("CopilotChat.code_actions").show_prompt_actions()
end,
desc = "CopilotChat - Help actions",
},
{
"<leader>ccp",
":lua require('CopilotChat.code_actions').show_prompt_actions(true)<CR>",
mode = "x",
desc = "CopilotChat - Prompt actions",
},
}
}
<

1. Select help actions base the diagnostic message under the cursor.
<https://gyazo.com/146dc35368592ba9f5de047ddc4728ad>


2. Select action base on user prompts.
<https://gyazo.com/a9c41e6398591c2f1d1d872fd58a2c63>




INTEGRATION WITH EDGY.NVIM ~

Consider integrating this plugin with `edgy.nvim`
Expand Down Expand Up @@ -427,10 +480,12 @@ STARGAZERS OVER TIME ~
7. *Fold Demo*: https://i.gyazo.com/766fb3b6ffeb697e650fc839882822a8.gif
8. *In-place Demo*: https://i.gyazo.com/4a5badaa109cd483c1fc23d296325cb0.gif
9. *Toggle*: https://i.gyazo.com/db5af9e5d88cd2fd09f58968914fa521.gif
10. *Layout*: https://i.gyazo.com/550daf6cbb729027ca9bd703c21af53e.png
11. *Debug Info*: https://i.gyazo.com/bf00e700bcee1b77bcbf7b516b552521.gif
12. *@ecosse3*:
13. *Stargazers over time*: https://starchart.cc/CopilotC-Nvim/CopilotChat.nvim.svg
10. *Help action with Copilot Chat*: https://i.gyazo.com/146dc35368592ba9f5de047ddc4728ad.gif
11. *Select action base on user prompts*: https://i.gyazo.com/a9c41e6398591c2f1d1d872fd58a2c63.gif
12. *Layout*: https://i.gyazo.com/550daf6cbb729027ca9bd703c21af53e.png
13. *Debug Info*: https://i.gyazo.com/bf00e700bcee1b77bcbf7b516b552521.gif
14. *@ecosse3*:
15. *Stargazers over time*: https://starchart.cc/CopilotC-Nvim/CopilotChat.nvim.svg

Generated by panvimdoc <https://github.com/kdheepak/panvimdoc>

Expand Down
173 changes: 173 additions & 0 deletions lua/CopilotChat/code_actions.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
local actions = require('telescope.actions')
local action_state = require('telescope.actions.state')
local telescope_pickers = require('telescope.pickers')
local finders = require('telescope.finders')
local themes = require('telescope.themes')
local conf = require('telescope.config').values
local utils = require('CopilotChat.utils')

local help_actions = {}
local user_prompt_actions = {}

local function generate_fix_diagnostic_prompt()
local diagnostic = utils.get_diagnostics()
local file_name = vim.fn.expand('%:t')
local line_number = vim.fn.line('.')
return 'Please assist with fixing the following diagnostic issue in file: "'
.. file_name
.. ':'
.. line_number
.. '". '
.. diagnostic
end

local function generate_explain_diagnostic_prompt()
local diagnostic = utils.get_diagnostics()
local file_name = vim.fn.expand('%:t')
local line_number = vim.fn.line('.')
return 'Please explain the following diagnostic issue in file: "'
.. file_name
.. ':'
.. line_number
.. '". '
.. diagnostic
end

--- Help command for telescope picker
--- This will copy all the lines in the buffer to the unnamed register
--- Then will send the diagnostic to copilot chat
---@param prefix string
local function diagnostic_help_command(prefix)
if prefix == nil then
prefix = ''
else
prefix = prefix .. ' '
end

return function(prompt_bufnr, _)
actions.select_default:replace(function()
actions.close(prompt_bufnr)
local selection = action_state.get_selected_entry()

-- Select all the lines in the buffer to uname register
vim.cmd('normal! ggVG"*y')

-- Get value from the help_actions and execute the command
local value = ''
for _, action in pairs(help_actions) do
if action.name == selection[1] then
value = action.label
break
end
end

vim.cmd(prefix .. value)
end)
return true
end
end

--- Prompt command for telescope picker
--- This will show all the user prompts in the telescope picker
--- Then will execute the command selected by the user
---@param prefix string
local function generate_prompt_command(prefix)
if prefix == nil then
prefix = ''
else
prefix = prefix .. ' '
end

return function(prompt_bufnr, _)
actions.select_default:replace(function()
actions.close(prompt_bufnr)
local selection = action_state.get_selected_entry()

-- Get value from the prompt_actions and execute the command
local value = ''
for _, action in pairs(user_prompt_actions) do
if action.name == selection[1] then
value = action.label
break
end
end

vim.cmd(prefix .. value)
end)
return true
end
end

local function show_help_actions()
help_actions = {
{
label = generate_fix_diagnostic_prompt(),
name = 'Fix diagnostic',
},
{
label = generate_explain_diagnostic_prompt(),
name = 'Explain diagnostic',
},
}

-- Filter all no diagnostics available actions
help_actions = vim.tbl_filter(function(value)
return value.label ~= 'No diagnostics available'
end, help_actions)

-- Show the menu with telescope pickers
local opts = themes.get_dropdown({})
local action_names = {}
for _, value in pairs(help_actions) do
table.insert(action_names, value.name)
end
telescope_pickers
.new(opts, {
prompt_title = 'Copilot Chat Help Actions',
finder = finders.new_table({
results = action_names,
}),
sorter = conf.generic_sorter(opts),
attach_mappings = diagnostic_help_command('CopilotChat'),
})
:find()
end

--- Show prompt actions
---@param is_in_visual_mode boolean?
local function show_prompt_actions(is_in_visual_mode)
-- Convert user prompts to a table of actions
user_prompt_actions = {}

for key, prompt in pairs(vim.g.copilot_chat_user_prompts) do
table.insert(user_prompt_actions, { name = key, label = prompt })
end

local cmd = 'CopilotChat'

if is_in_visual_mode then
cmd = "'<,'>CopilotChatVisual"
end

-- Show the menu with telescope pickers
local opts = themes.get_dropdown({})
local action_names = {}
for _, value in pairs(user_prompt_actions) do
table.insert(action_names, value.name)
end
telescope_pickers
.new(opts, {
prompt_title = 'Copilot Chat Actions',
finder = finders.new_table({
results = action_names,
}),
sorter = conf.generic_sorter(opts),
attach_mappings = generate_prompt_command(cmd),
})
:find()
end

return {
show_help_actions = show_help_actions,
show_prompt_actions = show_prompt_actions,
}
8 changes: 8 additions & 0 deletions lua/CopilotChat/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,14 @@ _COPILOT_CHAT_GLOBAL_CONFIG = {}
---@param options (table | nil)
-- - show_help: ('yes' | 'no') default: 'yes'.
-- - disable_extra_info: ('yes' | 'no') default: 'yes'.
-- - hide_system_prompt: ('yes' | 'no') default: 'yes'.
-- - proxy: (string?) default: ''.
-- - prompts: (table?) default: default_prompts.
-- - debug: (boolean?) default: false.
M.setup = function(options)
vim.g.copilot_chat_show_help = options and options.show_help or 'yes'
vim.g.copilot_chat_disable_separators = options and options.disable_extra_info or 'yes'
vim.g.copilot_chat_hide_system_prompt = options and options.hide_system_prompt or 'yes'
vim.g.copilot_chat_proxy = options and options.proxy or ''
local debug = options and options.debug or false
_COPILOT_CHAT_GLOBAL_CONFIG.debug = debug
Expand All @@ -36,6 +39,11 @@ M.setup = function(options)
-- Troubleshoot and fix the diagnostic issue at the current cursor position.
utils.create_cmd('CopilotChatFixDiagnostic', function()
local diagnostic = utils.get_diagnostics()
if diagnostic == 'No diagnostics available' then
vim.notify('No diagnostic issue found at the current cursor position.', vim.log.levels.INFO)
return
end

local file_name = vim.fn.expand('%:t')
local line_number = vim.fn.line('.')
-- Copy all the lines from current buffer to unnamed register
Expand Down
Loading

0 comments on commit 5970301

Please sign in to comment.