Skip to content

Commit 57226f2

Browse files
feat: New Command so that CopilotChat reads from current in-focus buffer when answering questions (#67)
* Adding a chat to read the current infocus buffer * Adding the Readmme * chore: fix the usage for new command --------- Co-authored-by: Dung Duc Huynh (Kaka) <870029+jellydn@users.noreply.github.com>
1 parent f448632 commit 57226f2

3 files changed

Lines changed: 34 additions & 10 deletions

File tree

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,12 @@ For further reference, you can view @jellydn's [configuration](https://github.co
264264

265265
[![Toggle](https://i.gyazo.com/db5af9e5d88cd2fd09f58968914fa521.gif)](https://gyazo.com/db5af9e5d88cd2fd09f58968914fa521)
266266

267+
### Chat with Copilot with all contents of InFocus buffer
268+
269+
1. Run the command `:CopilotChatBuffer` and type your prompt. For example, `What does this code do?`
270+
2. Press `Enter` to send your question to Github Copilot.
271+
3. Copilot will pull the content of the infocus buffer and chat with you.
272+
267273
## Tips
268274

269275
### Integration with `telescope.nvim`

rplugin/python3/CopilotChat/copilot.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,8 @@ def poll_auth(self, device_code: str) -> bool:
7979
def authenticate(self):
8080
if self.github_token is None:
8181
raise Exception("No token found")
82-
self.vscode_sessionid = str(uuid.uuid4()) + str(round(time.time() * 1000))
82+
self.vscode_sessionid = str(
83+
uuid.uuid4()) + str(round(time.time() * 1000))
8384
url = "https://api.github.com/copilot_internal/v2/token"
8485
headers = {
8586
"authorization": f"token {self.github_token}",
@@ -105,7 +106,7 @@ def ask(
105106
if not self.token:
106107
self.authenticate()
107108
# If expired, reauthenticate
108-
if self.token.get("expires_at") <= round(time.time()):
109+
if self.token.get("expires_at", 0) <= round(time.time()):
109110
self.authenticate()
110111

111112
if not system_prompt:
@@ -147,7 +148,8 @@ def ask(
147148

148149
raise Exception(
149150
error_messages.get(
150-
response.status_code, f"Unknown error: {response.status_code}"
151+
response.status_code, f"Unknown error: {
152+
response.status_code}"
151153
)
152154
)
153155
for line in response.iter_lines():
@@ -183,8 +185,9 @@ def _get_embeddings(self, inputs: list[typings.FileExtract]):
183185
if i + 18 > len(inputs):
184186
data = utilities.generate_embedding_request(inputs[i:])
185187
else:
186-
data = utilities.generate_embedding_request(inputs[i : i + 18])
187-
response = self.session.post(url, headers=self._headers(), json=data).json()
188+
data = utilities.generate_embedding_request(inputs[i: i + 18])
189+
response = self.session.post(
190+
url, headers=self._headers(), json=data).json()
188191
if "data" not in response:
189192
raise Exception(f"Error fetching embeddings: {response}")
190193
for embedding in response["data"]:
@@ -217,7 +220,8 @@ def main():
217220
copilot = Copilot(token)
218221
if copilot.github_token is None:
219222
req = copilot.request_auth()
220-
print("Please visit", req["verification_uri"], "and enter", req["user_code"])
223+
print("Please visit", req["verification_uri"],
224+
"and enter", req["user_code"])
221225
while not copilot.poll_auth(req["device_code"]):
222226
time.sleep(req["interval"])
223227
print("Successfully authenticated")

rplugin/python3/CopilotChat/copilot_plugin.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
@pynvim.plugin
1111
class CopilotPlugin(object):
1212
def __init__(self, nvim: pynvim.Nvim):
13-
self.nvim: MyNvim = MyNvim(nvim, PLUGIN_MAPPING_CMD, PLUGIN_AUTOCMD_CMD)
13+
self.nvim: MyNvim = MyNvim(
14+
nvim, PLUGIN_MAPPING_CMD, PLUGIN_AUTOCMD_CMD)
1415
self.vsplit_chat_handler = None
1516
self.inplace_chat_handler = None
1617

@@ -24,6 +25,18 @@ def copilot_chat_toggle_cmd(self):
2425
if self.vsplit_chat_handler:
2526
self.vsplit_chat_handler.toggle_vsplit()
2627

28+
@pynvim.command("CopilotChatBuffer", nargs="1")
29+
def copilot_agent_buffer_cmd(self, args: list[str]):
30+
self.init_vsplit_chat_handler()
31+
current_buffer = self.nvim.current.buffer
32+
lines = current_buffer[:]
33+
# Get code from the current infocus buffer
34+
code = "\n".join(lines)
35+
if self.vsplit_chat_handler:
36+
file_type = self.nvim.current.buffer.options["filetype"]
37+
self.vsplit_chat_handler.vsplit()
38+
self.vsplit_chat_handler.chat(args[0], file_type, code)
39+
2740
@pynvim.command("CopilotChat", nargs="1")
2841
def copilot_agent_cmd(self, args: list[str]):
2942
self.init_vsplit_chat_handler()
@@ -45,7 +58,7 @@ def copilot_agent_visual_cmd(self, args: list[str], range: list[int]):
4558
self.init_vsplit_chat_handler()
4659
if self.vsplit_chat_handler:
4760
file_type = self.nvim.current.buffer.options["filetype"]
48-
code_lines = self.nvim.current.buffer[range[0] - 1 : range[1]]
61+
code_lines = self.nvim.current.buffer[range[0] - 1: range[1]]
4962
code = "\n".join(code_lines)
5063
self.vsplit_chat_handler.vsplit()
5164
self.vsplit_chat_handler.chat(args[0], file_type, code)
@@ -70,7 +83,8 @@ def inplace_cmd(self, args: list[str], range: list[int]):
7083
self.init_inplace_chat_handler()
7184
if self.inplace_chat_handler:
7285
file_type = self.nvim.current.buffer.options["filetype"]
73-
code_lines = self.nvim.current.buffer[range[0] - 1 : range[1]]
86+
code_lines = self.nvim.current.buffer[range[0] - 1: range[1]]
7487
code = "\n".join(code_lines)
7588
user_buffer = self.nvim.current.buffer
76-
self.inplace_chat_handler.mount(code, file_type, range, user_buffer)
89+
self.inplace_chat_handler.mount(
90+
code, file_type, range, user_buffer)

0 commit comments

Comments
 (0)