From 13dfbba39e2202ad6bae5b4806ce7e42f75c94a0 Mon Sep 17 00:00:00 2001 From: Huynh Duc Dung Date: Sun, 18 Feb 2024 02:35:23 +0800 Subject: [PATCH 01/11] feat: add support for visual mode in show_prompt_actions function --- README.md | 6 ++++ lua/CopilotChat/code_actions.lua | 48 +++++++++++++++++++------------- 2 files changed, 34 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index b6cf11bc..65adc371 100644 --- a/README.md +++ b/README.md @@ -244,6 +244,12 @@ To integrate CopilotChat with Telescope, you can add the following configuration end, desc = "CopilotChat - Help actions", }, + { + "ccp", + ":lua require('CopilotChat.code_actions').show_prompt_actions(true)", + mode = "x", + desc = "CopilotChat - Prompt actions", + }, } } ``` diff --git a/lua/CopilotChat/code_actions.lua b/lua/CopilotChat/code_actions.lua index 1c19e551..561477f5 100644 --- a/lua/CopilotChat/code_actions.lua +++ b/lua/CopilotChat/code_actions.lua @@ -7,9 +7,9 @@ local conf = require('telescope.config').values local utils = require('CopilotChat.utils') local help_actions = {} -local prompt_actions = {} +local user_prompt_actions = {} -local function fix_diagnostic() +local function generate_fix_diagnostic_prompt() local diagnostic = utils.get_diagnostics() local file_name = vim.fn.expand('%:t') local line_number = vim.fn.line('.') @@ -21,7 +21,7 @@ local function fix_diagnostic() .. diagnostic end -local function explain_diagnostic() +local function generate_explain_diagnostic_prompt() local diagnostic = utils.get_diagnostics() local file_name = vim.fn.expand('%:t') local line_number = vim.fn.line('.') @@ -37,7 +37,7 @@ end --- 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 help_command(prefix) +local function diagnostic_help_command(prefix) if prefix == nil then prefix = '' else @@ -71,7 +71,7 @@ end --- 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 prompt_command(prefix) +local function generate_prompt_command(prefix) if prefix == nil then prefix = '' else @@ -85,7 +85,7 @@ local function prompt_command(prefix) -- Get value from the prompt_actions and execute the command local value = '' - for _, action in pairs(prompt_actions) do + for _, action in pairs(user_prompt_actions) do if action.name == selection[1] then value = action.label break @@ -101,11 +101,11 @@ end local function show_help_actions() help_actions = { { - label = fix_diagnostic(), + label = generate_fix_diagnostic_prompt(), name = 'Fix diagnostic', }, { - label = explain_diagnostic(), + label = generate_explain_diagnostic_prompt(), name = 'Explain diagnostic', }, } @@ -117,44 +117,52 @@ local function show_help_actions() -- Show the menu with telescope pickers local opts = themes.get_dropdown({}) - local picker_names = {} + local action_names = {} for _, value in pairs(help_actions) do - table.insert(picker_names, value.name) + table.insert(action_names, value.name) end telescope_pickers .new(opts, { prompt_title = 'Copilot Chat Help Actions', finder = finders.new_table({ - results = picker_names, + results = action_names, }), sorter = conf.generic_sorter(opts), - attach_mappings = help_command('CopilotChat'), + attach_mappings = diagnostic_help_command('CopilotChat'), }) :find() end -local function show_prompt_actions() +--- 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 - prompt_actions = {} + user_prompt_actions = {} for key, prompt in pairs(vim.g.copilot_chat_user_prompts) do - table.insert(prompt_actions, { name = key, label = prompt }) + 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 picker_names = {} - for _, value in pairs(prompt_actions) do - table.insert(picker_names, value.name) + 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 = picker_names, + results = action_names, }), sorter = conf.generic_sorter(opts), - attach_mappings = prompt_command('CopilotChat'), + attach_mappings = generate_prompt_command(cmd), }) :find() end From cb8c8c9ceb34c4ced3379a1499866a2b89adfa51 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" Date: Sat, 17 Feb 2024 18:35:53 +0000 Subject: [PATCH 02/11] chore(doc): auto generate docs --- doc/CopilotChat.txt | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/doc/CopilotChat.txt b/doc/CopilotChat.txt index cc4df58b..4b4e38f6 100644 --- a/doc/CopilotChat.txt +++ b/doc/CopilotChat.txt @@ -275,6 +275,12 @@ configuration to your keymap: end, desc = "CopilotChat - Help actions", }, + { + "ccp", + ":lua require('CopilotChat.code_actions').show_prompt_actions(true)", + mode = "x", + desc = "CopilotChat - Prompt actions", + }, } } < From 81c506027e6a638973e3187dd98fc70cae024719 Mon Sep 17 00:00:00 2001 From: Huynh Duc Dung Date: Sun, 18 Feb 2024 02:45:25 +0800 Subject: [PATCH 03/11] fix: add validation before call FixDiagnostic command --- lua/CopilotChat/init.lua | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/lua/CopilotChat/init.lua b/lua/CopilotChat/init.lua index f1ba227f..e80fd184 100644 --- a/lua/CopilotChat/init.lua +++ b/lua/CopilotChat/init.lua @@ -39,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 From fe1808e51760c2fa71ca4176551161c73b2f2f73 Mon Sep 17 00:00:00 2001 From: Huynh Duc Dung Date: Sun, 18 Feb 2024 03:23:49 +0800 Subject: [PATCH 04/11] feat: disable vim diagnostics on chat buffer for vsplit handler --- rplugin/python3/CopilotChat/handlers/vsplit_chat_handler.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/rplugin/python3/CopilotChat/handlers/vsplit_chat_handler.py b/rplugin/python3/CopilotChat/handlers/vsplit_chat_handler.py index 70284b61..e959debb 100644 --- a/rplugin/python3/CopilotChat/handlers/vsplit_chat_handler.py +++ b/rplugin/python3/CopilotChat/handlers/vsplit_chat_handler.py @@ -36,6 +36,9 @@ def vsplit(self): ) self.nvim.current.window.vars[var_key] = True + """ Disable vim diagnostics on the chat buffer """ + self.nvim.command(":lua vim.diagnostic.disable()") + def toggle_vsplit(self): """Toggle vsplit chat window.""" var_key = "copilot_chat" From 8e40e41c5bdabe675b2e54c80347dd85f1a9d550 Mon Sep 17 00:00:00 2001 From: neutrinoA4 <122616073+neutrinoA4@users.noreply.github.com> Date: Sun, 18 Feb 2024 08:37:41 +0900 Subject: [PATCH 05/11] feat: add language settings for copilot answers * added answer language setting * Update rplugin/python3/CopilotChat/handlers/chat_handler.py Co-authored-by: Dung Duc Huynh (Kaka) <870029+jellydn@users.noreply.github.com> * added document to README and setup function --------- Co-authored-by: neutrinoA4 Co-authored-by: Dung Duc Huynh (Kaka) <870029+jellydn@users.noreply.github.com> --- README.md | 1 + lua/CopilotChat/init.lua | 2 ++ rplugin/python3/CopilotChat/handlers/chat_handler.py | 3 +++ rplugin/python3/CopilotChat/handlers/inplace_chat_handler.py | 1 + rplugin/python3/CopilotChat/handlers/vsplit_chat_handler.py | 1 + rplugin/python3/CopilotChat/prompts.py | 3 +++ 6 files changed, 11 insertions(+) diff --git a/README.md b/README.md index 65adc371..6a63af8e 100644 --- a/README.md +++ b/README.md @@ -36,6 +36,7 @@ return { show_help = "yes", -- Show help text for CopilotChatInPlace, default: yes debug = false, -- Enable or disable debug mode, the log file will be in ~/.local/state/nvim/CopilotChat.nvim.log disable_extra_info = 'no', -- Disable extra information (e.g: system prompt) in the response. + language = "English" -- Copilot answer language settings when using default prompts. Default language is English. -- proxy = "socks5://127.0.0.1:3000", -- Proxies requests via https or socks. }, build = function() diff --git a/lua/CopilotChat/init.lua b/lua/CopilotChat/init.lua index e80fd184..99994646 100644 --- a/lua/CopilotChat/init.lua +++ b/lua/CopilotChat/init.lua @@ -15,6 +15,7 @@ _COPILOT_CHAT_GLOBAL_CONFIG = {} -- - disable_extra_info: ('yes' | 'no') default: 'yes'. -- - hide_system_prompt: ('yes' | 'no') default: 'yes'. -- - proxy: (string?) default: ''. +-- - language: (string?) default: ''. -- - prompts: (table?) default: default_prompts. -- - debug: (boolean?) default: false. M.setup = function(options) @@ -22,6 +23,7 @@ M.setup = function(options) 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 '' + vim.g.copilot_chat_language = options and options.language or '' local debug = options and options.debug or false _COPILOT_CHAT_GLOBAL_CONFIG.debug = debug diff --git a/rplugin/python3/CopilotChat/handlers/chat_handler.py b/rplugin/python3/CopilotChat/handlers/chat_handler.py index 4d3e88fc..f643614a 100644 --- a/rplugin/python3/CopilotChat/handlers/chat_handler.py +++ b/rplugin/python3/CopilotChat/handlers/chat_handler.py @@ -26,6 +26,7 @@ def __init__(self, nvim: MyNvim, buffer: MyBuffer): self.copilot: Copilot = None self.buffer: MyBuffer = buffer self.proxy: str = os.getenv("HTTPS_PROXY") or os.getenv("ALL_PROXY") or "" + self.language = self.nvim.eval("g:copilot_chat_language") # public @@ -79,6 +80,8 @@ def _construct_system_prompt(self, prompt: str): system_prompt = system_prompts.COPILOT_TESTS elif prompt == system_prompts.EXPLAIN_SHORTCUT: system_prompt = system_prompts.COPILOT_EXPLAIN + if self.language != "": + system_prompt = system_prompt + "\n" + system_prompts.PROMPT_ANSWER_LANGUAGE_TEMPLATE.substitute(language=self.language) return system_prompt def _add_start_separator( diff --git a/rplugin/python3/CopilotChat/handlers/inplace_chat_handler.py b/rplugin/python3/CopilotChat/handlers/inplace_chat_handler.py index ed938c30..1f206527 100644 --- a/rplugin/python3/CopilotChat/handlers/inplace_chat_handler.py +++ b/rplugin/python3/CopilotChat/handlers/inplace_chat_handler.py @@ -20,6 +20,7 @@ def __init__(self, nvim: MyNvim): self.diff_mode: bool = False self.model: str = MODEL_GPT4 self.system_prompt: str = "SENIOR_DEVELOPER_PROMPT" + self.language = self.nvim.eval("g:copilot_chat_language") # Add user prompts collection self.user_prompts = self.nvim.eval("g:copilot_chat_user_prompts") diff --git a/rplugin/python3/CopilotChat/handlers/vsplit_chat_handler.py b/rplugin/python3/CopilotChat/handlers/vsplit_chat_handler.py index e959debb..16dce5cd 100644 --- a/rplugin/python3/CopilotChat/handlers/vsplit_chat_handler.py +++ b/rplugin/python3/CopilotChat/handlers/vsplit_chat_handler.py @@ -14,6 +14,7 @@ def __init__(self, nvim: MyNvim): "filetype": "copilot-chat", }, ) + self.language = self.nvim.eval("g:copilot_chat_language") def vsplit(self): self.buffer.option("filetype", "copilot-chat") diff --git a/rplugin/python3/CopilotChat/prompts.py b/rplugin/python3/CopilotChat/prompts.py index 1290cbb0..a42a0c5f 100644 --- a/rplugin/python3/CopilotChat/prompts.py +++ b/rplugin/python3/CopilotChat/prompts.py @@ -1,3 +1,5 @@ +from string import Template + # pylint: disable=locally-disabled, multiple-statements, fixme, line-too-long COPILOT_INSTRUCTIONS = """You are an AI programming assistant. When asked for you name, you must respond with "GitHub Copilot". @@ -249,3 +251,4 @@ """ PROMPT_SIMPLE_DOCSTRING = "add simple docstring to this code" PROMPT_SEPARATE = "add comments separating the code into sections" +PROMPT_ANSWER_LANGUAGE_TEMPLATE = Template("Please answer in ${language}") From 09795200807a7a4c7c2532d8f457385e0e5596e8 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" Date: Sat, 17 Feb 2024 23:38:01 +0000 Subject: [PATCH 06/11] chore(doc): auto generate docs --- doc/CopilotChat.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/doc/CopilotChat.txt b/doc/CopilotChat.txt index 4b4e38f6..3a7e752d 100644 --- a/doc/CopilotChat.txt +++ b/doc/CopilotChat.txt @@ -55,6 +55,7 @@ LAZY.NVIM ~ show_help = "yes", -- Show help text for CopilotChatInPlace, default: yes debug = false, -- Enable or disable debug mode, the log file will be in ~/.local/state/nvim/CopilotChat.nvim.log disable_extra_info = 'no', -- Disable extra information (e.g: system prompt) in the response. + language = "English" -- Copilot answer language settings when using default prompts. Default language is English. -- proxy = "socks5://127.0.0.1:3000", -- Proxies requests via https or socks. }, build = function() From 4c60021f2f3befb7c81e1ca083c5902e7995847c Mon Sep 17 00:00:00 2001 From: Huynh Duc Dung Date: Sun, 18 Feb 2024 07:39:59 +0800 Subject: [PATCH 07/11] docs: add neutrinoA4 for code and doc --- .all-contributorsrc | 7 +++++++ README.md | 5 +++-- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/.all-contributorsrc b/.all-contributorsrc index fc6bbd7e..e734b8a4 100644 --- a/.all-contributorsrc +++ b/.all-contributorsrc @@ -81,6 +81,13 @@ "avatar_url": "https://avatars.githubusercontent.com/u/4156525?v=4", "profile": "https://github.com/shaungarwood", "contributions": ["code"] + }, + { + "login": "neutrinoA4", + "name": "neutrinoA4", + "avatar_url": "https://avatars.githubusercontent.com/u/122616073?v=4", + "profile": "https://github.com/neutrinoA4", + "contributions": ["code", "doc"] } ], "contributorsPerLine": 7, diff --git a/README.md b/README.md index 6a63af8e..6edf20c0 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ -[![All Contributors](https://img.shields.io/badge/all_contributors-11-orange.svg?style=flat-square)](#contributors-) +[![All Contributors](https://img.shields.io/badge/all_contributors-12-orange.svg?style=flat-square)](#contributors-) @@ -416,7 +416,7 @@ Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/d gptlang
gptlang

💻 📖 - Dung Duc Huynh (Kaka)
Dung Duc Huynh (Kaka)

💻 📖 + Dung Duc Huynh (Kaka)
Dung Duc Huynh (Kaka)

💻 📖 Ahmed Haracic
Ahmed Haracic

💻 Trí Thiện Nguyễn
Trí Thiện Nguyễn

💻 He Zhizhou
He Zhizhou

💻 @@ -428,6 +428,7 @@ Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/d Katsuhiko Nishimra
Katsuhiko Nishimra

💻 Erno Hopearuoho
Erno Hopearuoho

💻 Shaun Garwood
Shaun Garwood

💻 + neutrinoA4
neutrinoA4

💻 📖 From ea383a4f06fc066efa087ccfb9b20bab9e4eae2a Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" Date: Sat, 17 Feb 2024 23:40:19 +0000 Subject: [PATCH 08/11] chore(doc): auto generate docs --- doc/CopilotChat.txt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/doc/CopilotChat.txt b/doc/CopilotChat.txt index 3a7e752d..64dd6f30 100644 --- a/doc/CopilotChat.txt +++ b/doc/CopilotChat.txt @@ -460,7 +460,7 @@ CONTRIBUTORS ✨ *CopilotChat-copilot-chat-for-neovim-contributors-✨* Thanks goes to these wonderful people (emoji key ): -gptlang💻 📖Dung Duc Huynh (Kaka)💻 📖Ahmed Haracic💻Trí Thiện Nguyễn💻He Zhizhou💻Guruprakash Rajakkannu💻kristofka💻PostCyberPunk📖Katsuhiko Nishimra💻Erno Hopearuoho💻Shaun Garwood💻This project follows the all-contributors +gptlang💻 📖Dung Duc Huynh (Kaka)💻 📖Ahmed Haracic💻Trí Thiện Nguyễn💻He Zhizhou💻Guruprakash Rajakkannu💻kristofka💻PostCyberPunk📖Katsuhiko Nishimra💻Erno Hopearuoho💻Shaun Garwood💻neutrinoA4💻 📖This project follows the all-contributors specification. Contributions of any kind are welcome! @@ -472,7 +472,7 @@ STARGAZERS OVER TIME ~ ============================================================================== 2. Links *CopilotChat-links* -1. *All Contributors*: https://img.shields.io/badge/all_contributors-11-orange.svg?style=flat-square +1. *All Contributors*: https://img.shields.io/badge/all_contributors-12-orange.svg?style=flat-square 2. *@jellydn*: 3. *Chat Demo*: https://i.gyazo.com/10fbd1543380d15551791c1a6dcbcd46.gif 4. *Explain Code Demo*: https://i.gyazo.com/e5031f402536a1a9d6c82b2c38d469e3.gif From 0d474a14b3bf67469946aa639e3de1a42b016373 Mon Sep 17 00:00:00 2001 From: Huynh Duc Dung Date: Sun, 18 Feb 2024 09:27:42 +0800 Subject: [PATCH 09/11] fix: reorder system prompt and language prompt The system prompt and language prompt in the ChatHandler class have been reordered for better readability and consistency. The language prompt is now displayed first, followed by the system prompt. --- rplugin/python3/CopilotChat/handlers/chat_handler.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/rplugin/python3/CopilotChat/handlers/chat_handler.py b/rplugin/python3/CopilotChat/handlers/chat_handler.py index f643614a..bd5ff58d 100644 --- a/rplugin/python3/CopilotChat/handlers/chat_handler.py +++ b/rplugin/python3/CopilotChat/handlers/chat_handler.py @@ -81,7 +81,13 @@ def _construct_system_prompt(self, prompt: str): elif prompt == system_prompts.EXPLAIN_SHORTCUT: system_prompt = system_prompts.COPILOT_EXPLAIN if self.language != "": - system_prompt = system_prompt + "\n" + system_prompts.PROMPT_ANSWER_LANGUAGE_TEMPLATE.substitute(language=self.language) + system_prompt = ( + system_prompts.PROMPT_ANSWER_LANGUAGE_TEMPLATE.substitute( + language=self.language + ) + + "\n" + + system_prompt + ) return system_prompt def _add_start_separator( From 4886837cbcfdba7cc84c27038ce8032cdaab82fd Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" Date: Sun, 18 Feb 2024 01:28:35 +0000 Subject: [PATCH 10/11] chore(doc): auto generate docs --- doc/CopilotChat.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/CopilotChat.txt b/doc/CopilotChat.txt index 64dd6f30..440f4577 100644 --- a/doc/CopilotChat.txt +++ b/doc/CopilotChat.txt @@ -1,4 +1,4 @@ -*CopilotChat.txt* For NVIM v0.8.0 Last change: 2024 February 17 +*CopilotChat.txt* For NVIM v0.8.0 Last change: 2024 February 18 ============================================================================== Table of Contents *CopilotChat-table-of-contents* From c9b7dcae12cab57586769977e50280c324bec5b1 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Sun, 18 Feb 2024 12:48:22 +0800 Subject: [PATCH 11/11] chore(main): release 1.6.0 (#52) --- CHANGELOG.md | 15 +++++++++++++++ version.txt | 2 +- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8fea78a3..c630af62 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,20 @@ # Changelog +## [1.6.0](https://github.com/CopilotC-Nvim/CopilotChat.nvim/compare/v1.5.0...v1.6.0) (2024-02-18) + + +### Features + +* add language settings for copilot answers ([8e40e41](https://github.com/CopilotC-Nvim/CopilotChat.nvim/commit/8e40e41c5bdabe675b2e54c80347dd85f1a9d550)) +* add support for visual mode in show_prompt_actions function ([13dfbba](https://github.com/CopilotC-Nvim/CopilotChat.nvim/commit/13dfbba39e2202ad6bae5b4806ce7e42f75c94a0)) +* disable vim diagnostics on chat buffer for vsplit handler ([fe1808e](https://github.com/CopilotC-Nvim/CopilotChat.nvim/commit/fe1808e51760c2fa71ca4176551161c73b2f2f73)) + + +### Bug Fixes + +* add validation before call FixDiagnostic command ([81c5060](https://github.com/CopilotC-Nvim/CopilotChat.nvim/commit/81c506027e6a638973e3187dd98fc70cae024719)) +* reorder system prompt and language prompt ([0d474a1](https://github.com/CopilotC-Nvim/CopilotChat.nvim/commit/0d474a14b3bf67469946aa639e3de1a42b016373)) + ## [1.5.0](https://github.com/CopilotC-Nvim/CopilotChat.nvim/compare/v1.4.0...v1.5.0) (2024-02-17) diff --git a/version.txt b/version.txt index bc80560f..dc1e644a 100644 --- a/version.txt +++ b/version.txt @@ -1 +1 @@ -1.5.0 +1.6.0