Skip to content

Commit

Permalink
attempt to buffer till new line
Browse files Browse the repository at this point in the history
  • Loading branch information
gptlang committed Feb 5, 2024
1 parent 9e88f87 commit 163ccfd
Showing 1 changed file with 20 additions and 16 deletions.
36 changes: 20 additions & 16 deletions rplugin/python3/handlers/chat_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,25 +212,29 @@ def _add_chat_messages(
return
self.nvim.out_write("Successfully authenticated with Copilot\n")
self.copilot.authenticate()

buffer = b""
for token in self.copilot.ask(
system_prompt, prompt, code, language=cast(str, file_type), model=model
):
self.nvim.exec_lua(
'require("CopilotChat.utils").log_info(...)', f"Token: {token}"
)
buffer_lines = cast(list[str], self.buffer.lines())
last_line_row = len(buffer_lines) - 1
last_line_col = len(buffer_lines[-1])

self.nvim.api.buf_set_text(
self.buffer.number,
last_line_row,
last_line_col,
last_line_row,
last_line_col,
[tok.encode("utf-8") for tok in token.split("\n")],
)
buffer += token
if "\n" in buffer.decode("utf-8"):
token = buffer.decode("utf-8").split("\n")[0]
buffer = buffer[len(token) + 1:]
self.nvim.exec_lua(
'require("CopilotChat.utils").log_info(...)', f"Token: {token}"
)
buffer_lines = cast(list[str], self.buffer.lines())
last_line_row = len(buffer_lines) - 1
last_line_col = len(buffer_lines[-1])

self.nvim.api.buf_set_text(
self.buffer.number,
last_line_row,
last_line_col,
last_line_row,
last_line_col,
[tok.encode("utf-8") for tok in token.split()],
)

def _add_end_separator(self, model: str, no_annoyance: bool = False):
current_datetime = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
Expand Down

0 comments on commit 163ccfd

Please sign in to comment.