-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
le-gpt-transform.el
49 lines (42 loc) · 2.25 KB
/
le-gpt-transform.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
;;; le-gpt-transform.el --- Transform region functionality for le-gpt.el -*- lexical-binding: t; -*-
;; License: MIT
;; SPDX-License-Identifier: MIT
;;; Commentary:
;;
;;; Code:
(require 'le-gpt-core)
(require 'le-gpt-project)
(defcustom le-gpt-transform-region-instructions
"You should transform what is inside <region>. Only change what was requested without anything else, e.g., no explanatory comments, no triple backticks. Your response will replace what is inside region as is."
"The instruction to give gpt so that it performs the transformation as intended."
:type 'string
:group 'le-gpt)
(defun le-gpt-transform-region-with-prompt (temp-context-files)
"Transform the selected region.
Ask user for the transformation command and replace region with response.
If TEMP-CONTEXT-FILES is non-nil, select context files interactively."
(let* ((start (if (use-region-p) (region-beginning) (point-min)))
(end (if (use-region-p) (region-end) (point-max)))
(region-content (buffer-substring-no-properties start end))
(buffer-before (buffer-substring-no-properties (point-min) start))
(buffer-after (buffer-substring-no-properties end (point-max)))
(project-context (le-gpt--get-project-context temp-context-files))
(command (le-gpt--read-command))
(prompt (concat (when project-context (concat "User:\n\n" project-context))
"User: " command "\n"
"<region>" region-content "<region>" "\n"
le-gpt-transform-region-instructions "\n"
"GPTContext: " buffer-before "\n" buffer-after))
(prompt-file (le-gpt--create-prompt-file prompt))
(insertion-marker (make-marker))
(process (le-gpt--make-process prompt-file nil)))
(delete-region start end)
(set-marker insertion-marker (point))
(set-process-filter process (lambda (proc string)
(ignore proc)
(save-excursion
(goto-char insertion-marker)
(insert string)
(set-marker insertion-marker (point)))))))
(provide 'le-gpt-transform)
;;; le-gpt-transform.el ends here