From 4943f940fe7b6c727dc95f12b759667fa4676e12 Mon Sep 17 00:00:00 2001 From: Huynh Duc Dung Date: Wed, 3 Jan 2024 21:27:25 +0800 Subject: [PATCH] chore: sync the change from https://github.com/gptlang/CopilotChat.nvim/pull/9 --- rplugin/python3/copilot-plugin.py | 51 +++++++++++++++++-------------- 1 file changed, 28 insertions(+), 23 deletions(-) diff --git a/rplugin/python3/copilot-plugin.py b/rplugin/python3/copilot-plugin.py index e26c4bd8..2c035284 100644 --- a/rplugin/python3/copilot-plugin.py +++ b/rplugin/python3/copilot-plugin.py @@ -63,28 +63,33 @@ def copilotChat(self, args: list[str]): # Set filetype as markdown and wrap with linebreaks self.nvim.command("setlocal filetype=markdown wrap linebreak") - # if self.nvim.current.line != "": - # Go to end of file and insert a new line - self.nvim.command("normal Go") - self.nvim.current.line += "### User" - self.nvim.command("normal o") - # TODO: How to handle the case with the large text in from neovim command - self.nvim.current.line += prompt - self.nvim.command("normal o") - self.nvim.current.line += "### Copilot" - self.nvim.command("normal o") + # Get the current buffer + buf = self.nvim.current.buffer + # Add start separator + start_separator = f"""### User +{prompt} + +### Copilot + +""" + buf.append(start_separator.split("\n"), -1) + + # Add chat messages for token in self.copilot.ask(prompt, code, language=file_type): - if "\n" not in token: - self.nvim.current.line += token - continue - lines = token.split("\n") - for i in range(len(lines)): - self.nvim.current.line += lines[i] - if i != len(lines) - 1: - self.nvim.command("normal o") - - self.nvim.command("normal o") - self.nvim.current.line += "" - self.nvim.command("normal o") - self.nvim.current.line += "---" + buffer_lines = self.nvim.api.buf_get_lines(buf, 0, -1, 0) + last_line_row = len(buffer_lines) - 1 + last_line_col = len(buffer_lines[-1]) + + self.nvim.api.buf_set_text( + buf, + last_line_row, + last_line_col, + last_line_row, + last_line_col, + token.split("\n"), + ) + + # Add end separator + end_separator = "\n---\n" + buf.append(end_separator.split("\n"), -1)