Skip to content

Commit 9e57208

Browse files
committed
add src
1 parent d06f4d6 commit 9e57208

File tree

1 file changed

+155
-0
lines changed

1 file changed

+155
-0
lines changed

codegpt.el

Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
;;; codegpt.el --- Use GPT-3 tp help you write code -*- lexical-binding: t; -*-
2+
3+
;; Copyright (C) 2023 Shen, Jen-Chieh
4+
5+
;; Author: Shen, Jen-Chieh <jcs090218@gmail.com>
6+
;; Maintainer: Shen, Jen-Chieh <jcs090218@gmail.com>
7+
;; URL: https://github.com/emacs-openai/codegpt
8+
;; Version: 0.1.0
9+
;; Package-Requires: ((emacs "26.1"))
10+
;; Keywords: convenience
11+
12+
;; This file is not part of GNU Emacs.
13+
14+
;; This program is free software: you can redistribute it and/or modify
15+
;; it under the terms of the GNU General Public License as published by
16+
;; the Free Software Foundation, either version 3 of the License, or
17+
;; (at your option) any later version.
18+
19+
;; This program is distributed in the hope that it will be useful,
20+
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
21+
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22+
;; GNU General Public License for more details.
23+
24+
;; You should have received a copy of the GNU General Public License
25+
;; along with this program. If not, see <https://www.gnu.org/licenses/>.
26+
27+
;;; Commentary:
28+
;;
29+
;; Use GPT-3 tp help you write code
30+
;;
31+
32+
;;; Code:
33+
34+
(require 'openai-completion)
35+
36+
(defgroup codegpt nil
37+
"Use GPT-3 tp help you write code."
38+
:prefix "codegpt-"
39+
:group 'comm
40+
:link '(url-link :tag "Repository" "https://github.com/emacs-openai/codegpt"))
41+
42+
(defun codegpt-code--internal (instruction start end)
43+
"Do INSTRUCTION with partial code.
44+
45+
The partial code is defined in with the region, and the START nad END are
46+
boundaries of that region in buffer."
47+
(let ((text (string-trim (buffer-substring start end))))
48+
(openai-completon--ask-in-buffer
49+
instruction
50+
(insert text "\n\n")
51+
(openai-completion
52+
(buffer-string)
53+
(lambda (data)
54+
(openai--with-buffer openai-completion-buffer-name
55+
(openai--pop-to-buffer openai-completion-buffer-name)
56+
(let* ((choices (openai-completion--data-choices data))
57+
(result (openai-completion--get-choice choices)))
58+
(insert result "\n"))))))))
59+
60+
;;;###autoload
61+
(defun codegpt-doc (start end)
62+
"Automatically write documentation for your code.
63+
64+
This command is interactive region only, the START and END are boundaries of
65+
that region in buffer."
66+
(interactive "r")
67+
(openai-completion-code--internal
68+
"Please write the documentation for the following function."
69+
start end))
70+
71+
;;;###autoload
72+
(defun codegpt-fix (start end)
73+
"Fix your code.
74+
75+
This command is interactive region only, the START and END are boundaries of
76+
that region in buffer."
77+
(interactive "r")
78+
(openai-completion-code--internal
79+
"There is a bug in the following function, please help me fix it."
80+
start end))
81+
82+
;;;###autoload
83+
(defun codegpt-explain (start end)
84+
"Explain the selected code.
85+
86+
This command is interactive region only, the START and END are boundaries of
87+
that region in buffer."
88+
(interactive "r")
89+
(openai-completion-code--internal
90+
"What is the following?"
91+
start end))
92+
93+
;;;###autoload
94+
(defun codegpt-improve (start end)
95+
"Improve, refactor or optimize your code.
96+
97+
This command is interactive region only, the START and END are boundaries of
98+
that region in buffer."
99+
(interactive "r")
100+
(openai-completion-code--internal
101+
"Please improve the following."
102+
start end))
103+
104+
;;;###autoload
105+
(defun codegpt-custom (start end)
106+
"Do completion with custom instruction.
107+
108+
This command is interactive region only, the START and END are boundaries of
109+
that region in buffer."
110+
(interactive "r")
111+
(openai-completion-code--internal
112+
(read-string "Instruction: ")
113+
start end))
114+
115+
(defconst codegpt-action-alist
116+
`(("custom" . "Write your own instruction")
117+
("doc" . "Automatically write documentation for your code")
118+
("fix" . "Find problems with it")
119+
("explain" . "Explain the selected code")
120+
("improve" . "Improve, refactor or optimize it"))
121+
"Alist of code completion actions and its' description.")
122+
123+
;;;###autoload
124+
(defun codegpt (start end)
125+
"Do completon with OpenAI to your code.
126+
127+
This command is interactive region only, the START and END are boundaries of
128+
that region in buffer."
129+
(interactive "r")
130+
(let*
131+
((offset (openai--completing-frame-offset openai-completion-code-action-alist))
132+
(action
133+
(completing-read
134+
"Select completion action: "
135+
(lambda (string predicate action)
136+
(if (eq action 'metadata)
137+
`(metadata
138+
(display-sort-function . ,#'identity)
139+
(annotation-function
140+
. ,(lambda (cand)
141+
(concat (propertize " " 'display `((space :align-to (- right ,offset))))
142+
(cdr (assoc cand openai-completion-code-action-alist))))))
143+
(complete-with-action action openai-completion-code-action-alist string predicate)))
144+
nil t)))
145+
(funcall
146+
(pcase action
147+
("custom" #'codegpt-custom)
148+
("doc" #'codegpt-doc)
149+
("fix" #'codegpt-fix)
150+
("explain" #'codegpt-explain)
151+
("improve" #'codegpt-improve))
152+
start end)))
153+
154+
(provide 'codegpt)
155+
;;; codegpt.el ends here

0 commit comments

Comments
 (0)