4747(defconst codegpt-buffer-name " *CodeGPT*"
4848 " Buffer name to do completion task." )
4949
50+ (defcustom codegpt-action-alist
51+ `((" custom" . " Write your own instruction" )
52+ (" doc" . " Automatically write documentation for your code" )
53+ (" fix" . " Find problems with it" )
54+ (" explain" . " Explain the selected code" )
55+ (" improve" . " Improve, refactor or optimize it" ))
56+ " Alist of code completion actions and its' description."
57+ :type 'list
58+ :group 'codegpt )
59+
60+ (defconst codegpt--actions-functions
61+ '((" custom" . codegpt-custom)
62+ (" doc" . codegpt-doc)
63+ (" fix" . codegpt-fix)
64+ (" explain" . codegpt-explain)
65+ (" improve" . codegpt-improve))
66+ " Alist of code completion actions and its functions." )
67+
5068; ;
5169; ;; Application
5270
@@ -139,17 +157,16 @@ that region in buffer."
139157 (read-string " Instruction: " )
140158 start end))
141159
142- (defconst codegpt-action-alist
143- `((" custom" . " Write your own instruction" )
144- (" doc" . " Automatically write documentation for your code" )
145- (" fix" . " Find problems with it" )
146- (" explain" . " Explain the selected code" )
147- (" improve" . " Improve, refactor or optimize it" ))
148- " Alist of code completion actions and its' description." )
160+ (defun codegept--execute-predefined-template (start end question )
161+ " Ask predefined QUESTION for provided region.
162+ the START and END are boundaries of that region in buffer."
163+ (codegpt--internal
164+ question
165+ start end))
149166
150167;;;### autoload
151168(defun codegpt (start end )
152- " Do completon with OpenAI to your code.
169+ " Do completion with OpenAI to your code.
153170
154171This command is interactive region only, the START and END are boundaries of
155172that region in buffer."
@@ -168,15 +185,14 @@ that region in buffer."
168185 (concat (propertize " " 'display `((space :align-to (- right , offset ))))
169186 (cdr (assoc cand codegpt-action-alist))))))
170187 (complete-with-action action codegpt-action-alist string predicate)))
171- nil t )))
172- (funcall
173- (pcase action
174- (" custom" #'codegpt-custom )
175- (" doc" #'codegpt-doc )
176- (" fix" #'codegpt-fix )
177- (" explain" #'codegpt-explain )
178- (" improve" #'codegpt-improve ))
179- start end)))
188+ nil t ))
189+ (action-fn (cdr-safe (assoc action codegpt--actions-functions))))
190+ (if action-fn
191+ (funcall action-fn start end)
192+ (codegept--execute-predefined-template
193+ start
194+ end
195+ (cdr (assoc action codegpt-action-alist))))))
180196
181197(provide 'codegpt )
182198; ;; codegpt.el ends here
0 commit comments