forked from CopilotC-Nvim/CopilotChat.nvim
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcopilot_plugin.py
More file actions
90 lines (77 loc) · 3.68 KB
/
copilot_plugin.py
File metadata and controls
90 lines (77 loc) · 3.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import pynvim
from CopilotChat.handlers.inplace_chat_handler import InPlaceChatHandler
from CopilotChat.handlers.vsplit_chat_handler import VSplitChatHandler
from CopilotChat.mypynvim.core.nvim import MyNvim
PLUGIN_MAPPING_CMD = "CopilotChatMapping"
PLUGIN_AUTOCMD_CMD = "CopilotChatAutocmd"
@pynvim.plugin
class CopilotPlugin(object):
def __init__(self, nvim: pynvim.Nvim):
self.nvim: MyNvim = MyNvim(
nvim, PLUGIN_MAPPING_CMD, PLUGIN_AUTOCMD_CMD)
self.vsplit_chat_handler = None
self.inplace_chat_handler = None
def init_vsplit_chat_handler(self):
if self.vsplit_chat_handler is None:
self.vsplit_chat_handler = VSplitChatHandler(self.nvim)
@pynvim.command("CopilotChatVsplitToggle")
def copilot_chat_toggle_cmd(self):
self.init_vsplit_chat_handler()
if self.vsplit_chat_handler:
self.vsplit_chat_handler.toggle_vsplit()
@pynvim.command("CopilotChatBuffer", nargs="1")
def copilot_agent_buffer_cmd(self, args: list[str]):
self.init_vsplit_chat_handler()
current_buffer = self.nvim.current.buffer
lines = current_buffer[:]
# Get code from the current infocus buffer
code = "\n".join(lines)
if self.vsplit_chat_handler:
file_type = self.nvim.current.buffer.options["filetype"]
self.vsplit_chat_handler.vsplit()
self.vsplit_chat_handler.chat(args[0], file_type, code)
@pynvim.command("CopilotChat", nargs="1")
def copilot_agent_cmd(self, args: list[str]):
self.init_vsplit_chat_handler()
if self.vsplit_chat_handler:
file_type = self.nvim.current.buffer.options["filetype"]
self.vsplit_chat_handler.vsplit()
# Get code from the unnamed register
code = self.nvim.eval("getreg('\"')")
self.vsplit_chat_handler.chat(args[0], file_type, code)
@pynvim.command("CopilotChatReset")
def copilot_agent_reset_cmd(self):
if self.vsplit_chat_handler:
self.vsplit_chat_handler.copilot.reset()
self.vsplit_chat_handler.reset_buffer()
@pynvim.command("CopilotChatVisual", nargs="1", range="")
def copilot_agent_visual_cmd(self, args: list[str], range: list[int]):
self.init_vsplit_chat_handler()
if self.vsplit_chat_handler:
file_type = self.nvim.current.buffer.options["filetype"]
code_lines = self.nvim.current.buffer[range[0] - 1: range[1]]
code = "\n".join(code_lines)
self.vsplit_chat_handler.vsplit()
self.vsplit_chat_handler.chat(args[0], file_type, code)
def init_inplace_chat_handler(self):
if self.inplace_chat_handler is None:
self.inplace_chat_handler = InPlaceChatHandler(self.nvim)
# Those commands are used by the plugin, internal use only
@pynvim.command(PLUGIN_MAPPING_CMD, nargs="*")
def plugin_mapping_cmd(self, args):
bufnr, mapping = args
self.nvim.key_mapper.execute(bufnr, mapping)
@pynvim.command(PLUGIN_AUTOCMD_CMD, nargs="*")
def plugin_autocmd_cmd(self, args):
event, id, bufnr = args
self.nvim.autocmd_mapper.execute(event, id, bufnr)
@pynvim.command("CopilotChatInPlace", nargs="*", range="")
def inplace_cmd(self, args: list[str], range: list[int]):
self.init_inplace_chat_handler()
if self.inplace_chat_handler:
file_type = self.nvim.current.buffer.options["filetype"]
code_lines = self.nvim.current.buffer[range[0] - 1: range[1]]
code = "\n".join(code_lines)
user_buffer = self.nvim.current.buffer
self.inplace_chat_handler.mount(
code, file_type, range, user_buffer)