-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathcopilot-chat-shell-maker.el
186 lines (156 loc) · 9.62 KB
/
copilot-chat-shell-maker.el
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
;;; copilot-chat --- copilot-chat-shell-maker.el --- copilot chat interface, shell-maker frontend -*- indent-tabs-mode: nil; lisp-indent-offset: 2; lexical-binding: t -*-
;; Copyright (C) 2024 copilot-chat maintainers
;; The MIT License (MIT)
;; Permission is hereby granted, free of charge, to any person obtaining a copy
;; of this software and associated documentation files (the "Software"), to deal
;; in the Software without restriction, including without limitation the rights
;; to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
;; copies of the Software, and to permit persons to whom the Software is
;; furnished to do so, subject to the following conditions:
;; The above copyright notice and this permission notice shall be included in all
;; copies or substantial portions of the Software.
;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
;; IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
;; FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
;; AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
;; LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
;; OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
;; SOFTWARE.
;;; Commentary:
;;; Code:
(require 'shell-maker)
(require 'copilot-chat-copilot)
(declare-function copilot-chat-reset "copilot-chat")
;; Variables
(defvar copilot-chat--shell-cb-fn nil)
(defvar copilot-chat--shell-config
(make-shell-maker-config
:name "Copilot-Chat"
:execute-command 'copilot-chat--shell-cb))
(defvar copilot-chat--shell-maker-answer-point 0
"Start of the current answer.")
;; Constants
(defconst copilot-chat--shell-maker-temp-buffer "*copilot-chat-shell-maker-temp*")
;; Functions
(defun copilot-chat--shell-maker-custom-prompt-selection()
"Send to Copilot a custom prompt followed by the current selected code."
(unless (copilot-chat--ready-p)
(copilot-chat-reset))
(let* ((prompt (read-from-minibuffer "Copilot prompt: "))
(code (buffer-substring-no-properties (region-beginning) (region-end)))
(formatted-prompt (concat prompt "\n" code)))
(copilot-chat--shell-maker-insert-and-send-prompt formatted-prompt)))
(defun copilot-chat--shell-maker-insert-and-send-prompt(prompt)
"Helper function to prepare buffers and send PROMPT to Copilot."
(with-current-buffer (copilot-chat--shell-maker-prepare-buffers)
(insert prompt)
(shell-maker-submit)
(display-buffer (current-buffer))))
(defun copilot-chat--shell-maker-prepare-buffers ()
(let ((buffer (get-buffer copilot-chat--buffer))
(tempb (get-buffer-create copilot-chat--shell-maker-temp-buffer))
(inhibit-read-only t))
(unless buffer
(setq buffer (copilot-chat--shell)))
(with-current-buffer tempb
(markdown-view-mode))
buffer))
(defun copilot-chat--shell-maker-display ()
"Display copilot chat buffer."
(unless (copilot-chat--ready-p)
(copilot-chat-reset))
(pop-to-buffer (copilot-chat--shell-maker-prepare-buffers)))
(defun copilot-chat--shell-maker-font-lock-faces ()
"Replace faces by font-lock-faces."
(with-current-buffer copilot-chat--shell-maker-temp-buffer
(let ((inhibit-read-only t))
(font-lock-ensure)
(goto-char (point-min))
(while (not (eobp))
(let ((next-change (or (next-property-change (point) nil (point-max)) (point-max)))
(face (get-text-property (point) 'face)))
(when face
(font-lock-append-text-property (point) next-change 'font-lock-face face))
(goto-char next-change))))))
(defun copilot-chat--shell-maker-copy-faces()
"Apply faces to the copilot chat buffer."
(with-current-buffer copilot-chat--shell-maker-temp-buffer
(save-restriction
(widen)
(font-lock-ensure)
(copilot-chat--shell-maker-font-lock-faces)
(let ((content (buffer-substring (point-min) (point-max))))
(with-current-buffer copilot-chat--buffer
(goto-char (1+ copilot-chat--shell-maker-answer-point))
(insert content)
(delete-region (point) (+ (point) (length content)))
(goto-char (point-max)))))))
(defun copilot-chat--shell-cb-prompt (shell content)
"Callback for Copilot Chat shell-maker.
Argument SHELL is the shell-maker instance.
Argument CONTENT is copilot chat answer."
(with-current-buffer copilot-chat--buffer
(goto-char (point-max))
(when copilot-chat--first-word-answer
(setq copilot-chat--first-word-answer nil)
(let ((str (format-time-string "# [%T] Copilot:\n"))
(inhibit-read-only t))
(with-current-buffer copilot-chat--shell-maker-temp-buffer
(insert str))
(funcall (map-elt shell :write-output) str)))
(if (string= content copilot-chat--magic)
(progn
(funcall (map-elt shell :finish-output) t); the end
(copilot-chat--shell-maker-copy-faces)
(setq copilot-chat--first-word-answer t))
(progn
(with-current-buffer copilot-chat--shell-maker-temp-buffer
(goto-char (point-max))
(let ((inhibit-read-only t))
(insert content)))
(funcall (map-elt shell :write-output) content)))))
(defun copilot-chat--shell-cb-prompt-wrapper (shell content)
"Wrapper around copilot-chat--shell-cb-prompt.
When copilot-chat-follow is nil, copilot-chat--shell-cb-prompt is called in `save-excursion` block.
Argument SHELL is the shell-maker instance.
Argument CONTENT is copilot chat answer."
(if copilot-chat-follow
(copilot-chat--shell-cb-prompt shell content)
(save-excursion
(copilot-chat--shell-cb-prompt shell content))))
(defun copilot-chat--shell-cb (command shell)
"Callback for Copilot Chat shell-maker.
Argument COMMAND is the command to send to Copilot.
Argument CALLBACK is the callback function to call.
Argument ERROR-CALLBACK is the error callback function to call."
(setq
copilot-chat--shell-cb-fn
(apply-partially #'copilot-chat--shell-cb-prompt-wrapper shell)
copilot-chat--shell-maker-answer-point (point))
(let ((inhibit-read-only t))
(with-current-buffer copilot-chat--shell-maker-temp-buffer
(erase-buffer)))
(copilot-chat--ask command copilot-chat--shell-cb-fn))
(defun copilot-chat--shell ()
"Start a Copilot Chat shell."
(shell-maker-start
copilot-chat--shell-config
t nil t
copilot-chat--buffer))
(defun copilot-chat--shell-maker-clean()
"Clean the copilot chat shell-maker frontend."
(advice-remove 'copilot-chat--insert-and-send-prompt #'copilot-chat--shell-maker-insert-and-send-prompt)
(advice-remove 'copilot-chat--custom-prompt-selection #'copilot-chat--shell-maker-custom-prompt-selection)
(advice-remove 'copilot-chat--display #'copilot-chat--shell-maker-display)
(advice-remove 'copilot-chat--prepare-buffers #'copilot-chat--shell-maker-prepare-buffers)
(advice-remove 'copilot-chat--clean #'copilot-chat--shell-maker-clean))
(defun copilot-chat-shell-maker-init()
"Initialize the copilot chat shell-maker frontend."
(setq copilot-chat-prompt "You are a world-class coding tutor. Your code explanations perfectly balance high-level concepts and granular details. Your approach ensures that students not only understand how to write code, but also grasp the underlying principles that guide effective programming.\nWhen asked for your name, you must respond with \"GitHub Copilot\".\nFollow the user's requirements carefully & to the letter.\nYour expertise is strictly limited to software development topics.\nFollow Microsoft content policies.\nAvoid content that violates copyrights.\nFor questions not related to software development, simply give a reminder that you are an AI programming assistant.\nKeep your answers short and impersonal.\nUse Markdown formatting in your answers.\nMake sure to include the programming language name at the start of the Markdown code blocks.\nAvoid wrapping the whole response in triple backticks.\nThe user works in an IDE called Neovim which has a concept for editors with open files, integrated unit test support, an output pane that shows the output of running the code as well as an integrated terminal.\nThe active document is the source code the user is looking at right now.\nYou can only give one reply for each conversation turn.\n\nAdditional Rules\nThink step by step:\n1. Examine the provided code selection and any other context like user question, related errors, project details, class definitions, etc.\n2. If you are unsure about the code, concepts, or the user's question, ask clarifying questions.\n3. If the user provided a specific question or error, answer it based on the selected code and additional provided context. Otherwise focus on explaining the selected code.\n4. Provide suggestions if you see opportunities to improve code readability, performance, etc.\n\nFocus on being clear, helpful, and thorough without assuming extensive prior knowledge.\nUse developer-friendly terms and analogies in your explanations.\nIdentify 'gotchas' or less obvious parts of the code that might trip up someone new.\nProvide clear and relevant examples aligned with any provided context.\n")
(advice-add 'copilot-chat--insert-and-send-prompt :override #'copilot-chat--shell-maker-insert-and-send-prompt)
(advice-add 'copilot-chat--custom-prompt-selection :override #'copilot-chat--shell-maker-custom-prompt-selection)
(advice-add 'copilot-chat--display :override #'copilot-chat--shell-maker-display)
(advice-add 'copilot-chat--prepare-buffers :override #'copilot-chat--shell-maker-prepare-buffers)
(advice-add 'copilot-chat--clean :after #'copilot-chat--shell-maker-clean))
(provide 'copilot-chat-shell-maker)
;;; copilot-chat-shell-maker.el ends here