diff --git a/README.md b/README.md index 7730b412..241c6f92 100644 --- a/README.md +++ b/README.md @@ -264,6 +264,12 @@ For further reference, you can view @jellydn's [configuration](https://github.co [![Toggle](https://i.gyazo.com/db5af9e5d88cd2fd09f58968914fa521.gif)](https://gyazo.com/db5af9e5d88cd2fd09f58968914fa521) +### Chat with Copilot with all contents of InFocus buffer + +1. Run the command `:CopilotChatBuffer` and type your prompt. For example, `What does this code do?` +2. Press `Enter` to send your question to Github Copilot. +3. Copilot will pull the content of the infocus buffer and chat with you. + ## Tips ### Integration with `telescope.nvim` diff --git a/rplugin/python3/CopilotChat/copilot.py b/rplugin/python3/CopilotChat/copilot.py index 100d41e2..d4945ab7 100644 --- a/rplugin/python3/CopilotChat/copilot.py +++ b/rplugin/python3/CopilotChat/copilot.py @@ -79,7 +79,8 @@ def poll_auth(self, device_code: str) -> bool: def authenticate(self): if self.github_token is None: raise Exception("No token found") - self.vscode_sessionid = str(uuid.uuid4()) + str(round(time.time() * 1000)) + self.vscode_sessionid = str( + uuid.uuid4()) + str(round(time.time() * 1000)) url = "https://api.github.com/copilot_internal/v2/token" headers = { "authorization": f"token {self.github_token}", @@ -105,7 +106,7 @@ def ask( if not self.token: self.authenticate() # If expired, reauthenticate - if self.token.get("expires_at") <= round(time.time()): + if self.token.get("expires_at", 0) <= round(time.time()): self.authenticate() if not system_prompt: @@ -147,7 +148,8 @@ def ask( raise Exception( error_messages.get( - response.status_code, f"Unknown error: {response.status_code}" + response.status_code, f"Unknown error: { + response.status_code}" ) ) for line in response.iter_lines(): @@ -183,8 +185,9 @@ def _get_embeddings(self, inputs: list[typings.FileExtract]): if i + 18 > len(inputs): data = utilities.generate_embedding_request(inputs[i:]) else: - data = utilities.generate_embedding_request(inputs[i : i + 18]) - response = self.session.post(url, headers=self._headers(), json=data).json() + data = utilities.generate_embedding_request(inputs[i: i + 18]) + response = self.session.post( + url, headers=self._headers(), json=data).json() if "data" not in response: raise Exception(f"Error fetching embeddings: {response}") for embedding in response["data"]: @@ -217,7 +220,8 @@ def main(): copilot = Copilot(token) if copilot.github_token is None: req = copilot.request_auth() - print("Please visit", req["verification_uri"], "and enter", req["user_code"]) + print("Please visit", req["verification_uri"], + "and enter", req["user_code"]) while not copilot.poll_auth(req["device_code"]): time.sleep(req["interval"]) print("Successfully authenticated") diff --git a/rplugin/python3/CopilotChat/copilot_plugin.py b/rplugin/python3/CopilotChat/copilot_plugin.py index c3b48cce..f7c0c3a1 100644 --- a/rplugin/python3/CopilotChat/copilot_plugin.py +++ b/rplugin/python3/CopilotChat/copilot_plugin.py @@ -10,7 +10,8 @@ @pynvim.plugin class CopilotPlugin(object): def __init__(self, nvim: pynvim.Nvim): - self.nvim: MyNvim = MyNvim(nvim, PLUGIN_MAPPING_CMD, PLUGIN_AUTOCMD_CMD) + self.nvim: MyNvim = MyNvim( + nvim, PLUGIN_MAPPING_CMD, PLUGIN_AUTOCMD_CMD) self.vsplit_chat_handler = None self.inplace_chat_handler = None @@ -24,6 +25,18 @@ def copilot_chat_toggle_cmd(self): if self.vsplit_chat_handler: self.vsplit_chat_handler.toggle_vsplit() + @pynvim.command("CopilotChatBuffer", nargs="1") + def copilot_agent_buffer_cmd(self, args: list[str]): + self.init_vsplit_chat_handler() + current_buffer = self.nvim.current.buffer + lines = current_buffer[:] + # Get code from the current infocus buffer + code = "\n".join(lines) + if self.vsplit_chat_handler: + file_type = self.nvim.current.buffer.options["filetype"] + self.vsplit_chat_handler.vsplit() + self.vsplit_chat_handler.chat(args[0], file_type, code) + @pynvim.command("CopilotChat", nargs="1") def copilot_agent_cmd(self, args: list[str]): self.init_vsplit_chat_handler() @@ -45,7 +58,7 @@ def copilot_agent_visual_cmd(self, args: list[str], range: list[int]): self.init_vsplit_chat_handler() if self.vsplit_chat_handler: file_type = self.nvim.current.buffer.options["filetype"] - code_lines = self.nvim.current.buffer[range[0] - 1 : range[1]] + code_lines = self.nvim.current.buffer[range[0] - 1: range[1]] code = "\n".join(code_lines) self.vsplit_chat_handler.vsplit() self.vsplit_chat_handler.chat(args[0], file_type, code) @@ -70,7 +83,8 @@ def inplace_cmd(self, args: list[str], range: list[int]): self.init_inplace_chat_handler() if self.inplace_chat_handler: file_type = self.nvim.current.buffer.options["filetype"] - code_lines = self.nvim.current.buffer[range[0] - 1 : range[1]] + code_lines = self.nvim.current.buffer[range[0] - 1: range[1]] code = "\n".join(code_lines) user_buffer = self.nvim.current.buffer - self.inplace_chat_handler.mount(code, file_type, range, user_buffer) + self.inplace_chat_handler.mount( + code, file_type, range, user_buffer)