|
3 | 3 |
|
4 | 4 | import copilot |
5 | 5 | import dotenv |
| 6 | +import prompts |
6 | 7 | import pynvim |
7 | 8 |
|
8 | 9 | dotenv.load_dotenv() |
@@ -34,41 +35,69 @@ def copilotChat(self, args: list[str]): |
34 | 35 | if self.copilot.github_token is None: |
35 | 36 | self.nvim.out_write("Please authenticate with Copilot first\n") |
36 | 37 | return |
| 38 | + |
| 39 | + # Start the spinner |
| 40 | + self.nvim.exec_lua('require("CopilotChat.spinner").show()') |
| 41 | + |
37 | 42 | prompt = " ".join(args) |
| 43 | + if prompt == "/fix": |
| 44 | + prompt = prompts.FIX_SHORTCUT |
| 45 | + elif prompt == "/test": |
| 46 | + prompt = prompts.TEST_SHORTCUT |
| 47 | + elif prompt == "/explain": |
| 48 | + prompt = prompts.EXPLAIN_SHORTCUT |
38 | 49 |
|
39 | 50 | # Get code from the unnamed register |
40 | 51 | code = self.nvim.eval("getreg('\"')") |
41 | 52 | file_type = self.nvim.eval("expand('%')").split(".")[-1] |
| 53 | + |
| 54 | + # Get the view option from the command |
| 55 | + view_option = self.nvim.eval("g:copilot_chat_view_option") |
| 56 | + |
42 | 57 | # Check if we're already in a chat buffer |
43 | 58 | if self.nvim.eval("getbufvar(bufnr(), '&buftype')") != "nofile": |
44 | 59 | # Create a new scratch buffer to hold the chat |
45 | | - self.nvim.command("enew") |
| 60 | + if view_option == "split": |
| 61 | + self.nvim.command("vnew") |
| 62 | + else: |
| 63 | + self.nvim.command("enew") |
| 64 | + # Set the buffer type to nofile and hide it when it's not active |
46 | 65 | self.nvim.command("setlocal buftype=nofile bufhidden=hide noswapfile") |
47 | 66 | # Set filetype as markdown and wrap with linebreaks |
48 | 67 | self.nvim.command("setlocal filetype=markdown wrap linebreak") |
49 | 68 |
|
50 | | - if self.nvim.current.line != "": |
51 | | - # Go to end of file and insert a new line |
52 | | - self.nvim.command("normal Go") |
53 | | - self.nvim.current.line += "### User" |
54 | | - self.nvim.command("normal o") |
55 | | - # TODO: How to handle the case with the large text in from neovim command |
56 | | - self.nvim.current.line += prompt |
57 | | - self.nvim.command("normal o") |
58 | | - self.nvim.current.line += "### Copilot" |
59 | | - self.nvim.command("normal o") |
| 69 | + # Get the current buffer |
| 70 | + buf = self.nvim.current.buffer |
| 71 | + self.nvim.api.buf_set_option(buf, "fileencoding", "utf-8") |
| 72 | + |
| 73 | + # Add start separator |
| 74 | + start_separator = f"""### User |
| 75 | +{prompt} |
| 76 | +
|
| 77 | +### Copilot |
60 | 78 |
|
| 79 | +""" |
| 80 | + buf.append(start_separator.split("\n"), -1) |
| 81 | + |
| 82 | + # Add chat messages |
61 | 83 | for token in self.copilot.ask(prompt, code, language=file_type): |
62 | | - if "\n" not in token: |
63 | | - self.nvim.current.line += token |
64 | | - continue |
65 | | - lines = token.split("\n") |
66 | | - for i in range(len(lines)): |
67 | | - self.nvim.current.line += lines[i] |
68 | | - if i != len(lines) - 1: |
69 | | - self.nvim.command("normal o") |
70 | | - |
71 | | - self.nvim.command("normal o") |
72 | | - self.nvim.current.line += "" |
73 | | - self.nvim.command("normal o") |
74 | | - self.nvim.current.line += "---" |
| 84 | + buffer_lines = self.nvim.api.buf_get_lines(buf, 0, -1, 0) |
| 85 | + last_line_row = len(buffer_lines) - 1 |
| 86 | + last_line = buffer_lines[-1] |
| 87 | + last_line_col = len(last_line.encode("utf-8")) |
| 88 | + |
| 89 | + self.nvim.api.buf_set_text( |
| 90 | + buf, |
| 91 | + last_line_row, |
| 92 | + last_line_col, |
| 93 | + last_line_row, |
| 94 | + last_line_col, |
| 95 | + token.split("\n"), |
| 96 | + ) |
| 97 | + |
| 98 | + # Stop the spinner |
| 99 | + self.nvim.exec_lua('require("CopilotChat.spinner").hide()') |
| 100 | + |
| 101 | + # Add end separator |
| 102 | + end_separator = "\n---\n" |
| 103 | + buf.append(end_separator.split("\n"), -1) |
0 commit comments