From d4e5fd65b08d27d52cfb382c4c377e96c7e4b638 Mon Sep 17 00:00:00 2001 From: ziontee113 Date: Wed, 3 Jan 2024 07:24:13 +0700 Subject: [PATCH 1/2] feat: use buf_set_lines() --- rplugin/python3/plugin.py | 53 ++++++++++++++++++++++++--------------- 1 file changed, 33 insertions(+), 20 deletions(-) diff --git a/rplugin/python3/plugin.py b/rplugin/python3/plugin.py index 8b448ca7..ce35ff63 100644 --- a/rplugin/python3/plugin.py +++ b/rplugin/python3/plugin.py @@ -55,26 +55,39 @@ 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 != "": - self.nvim.command("normal Go") - self.nvim.current.line += "### User" - self.nvim.command("normal o") - 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 + +""" + buffer_lines = self.nvim.api.buf_get_lines(buf, 0, -1, 0) + self.nvim.api.buf_set_lines( + 0, len(buffer_lines), -1, False, start_separator.split("\n") + ) + + # 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") + 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"), + ) - self.nvim.command("normal o") - self.nvim.current.line += "" - self.nvim.command("normal o") - self.nvim.current.line += "---" + # Add end separator + end_separator = "\n---\n" + buffer_lines = self.nvim.api.buf_get_lines(buf, 0, -1, 0) + self.nvim.api.buf_set_lines( + buf, len(buffer_lines), -1, False, end_separator.split("\n") + ) From 4742450dd59036232b35a3ecc9810ad9e09803e3 Mon Sep 17 00:00:00 2001 From: ziontee113 Date: Wed, 3 Jan 2024 07:36:32 +0700 Subject: [PATCH 2/2] ref: shorten code using buf.append() --- rplugin/python3/plugin.py | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/rplugin/python3/plugin.py b/rplugin/python3/plugin.py index ce35ff63..01941e87 100644 --- a/rplugin/python3/plugin.py +++ b/rplugin/python3/plugin.py @@ -65,10 +65,7 @@ def copilotChat(self, args: list[str]): ### Copilot """ - buffer_lines = self.nvim.api.buf_get_lines(buf, 0, -1, 0) - self.nvim.api.buf_set_lines( - 0, len(buffer_lines), -1, False, start_separator.split("\n") - ) + buf.append(start_separator.split("\n"), -1) # Add chat messages for token in self.copilot.ask(prompt, code, language=file_type): @@ -87,7 +84,4 @@ def copilotChat(self, args: list[str]): # Add end separator end_separator = "\n---\n" - buffer_lines = self.nvim.api.buf_get_lines(buf, 0, -1, 0) - self.nvim.api.buf_set_lines( - buf, len(buffer_lines), -1, False, end_separator.split("\n") - ) + buf.append(end_separator.split("\n"), -1)