From 4b9a5eb674f0a076b59661c547fd85354c3f2d81 Mon Sep 17 00:00:00 2001 From: artawower Date: Sun, 5 Feb 2023 02:15:32 +0400 Subject: [PATCH] Add custom user predefined actions (#2) --- codegpt.el | 50 +++++++++++++++++++++++++++++++++----------------- 1 file changed, 33 insertions(+), 17 deletions(-) diff --git a/codegpt.el b/codegpt.el index 4bace53..44cd71c 100644 --- a/codegpt.el +++ b/codegpt.el @@ -47,6 +47,24 @@ (defconst codegpt-buffer-name "*CodeGPT*" "Buffer name to do completion task.") +(defcustom codegpt-action-alist + `(("custom" . "Write your own instruction") + ("doc" . "Automatically write documentation for your code") + ("fix" . "Find problems with it") + ("explain" . "Explain the selected code") + ("improve" . "Improve, refactor or optimize it")) + "Alist of code completion actions and its' description." + :type 'list + :group 'codegpt) + +(defconst codegpt--actions-functions + '(("custom" . codegpt-custom) + ("doc" . codegpt-doc) + ("fix" . codegpt-fix) + ("explain" . codegpt-explain) + ("improve" . codegpt-improve)) + "Alist of code completion actions and its functions.") + ;; ;;; Application @@ -139,17 +157,16 @@ that region in buffer." (read-string "Instruction: ") start end)) -(defconst codegpt-action-alist - `(("custom" . "Write your own instruction") - ("doc" . "Automatically write documentation for your code") - ("fix" . "Find problems with it") - ("explain" . "Explain the selected code") - ("improve" . "Improve, refactor or optimize it")) - "Alist of code completion actions and its' description.") +(defun codegept--execute-predefined-template (start end question) + "Ask predefined QUESTION for provided region. +the START and END are boundaries of that region in buffer." + (codegpt--internal + question + start end)) ;;;###autoload (defun codegpt (start end) - "Do completon with OpenAI to your code. + "Do completion with OpenAI to your code. This command is interactive region only, the START and END are boundaries of that region in buffer." @@ -168,15 +185,14 @@ that region in buffer." (concat (propertize " " 'display `((space :align-to (- right ,offset)))) (cdr (assoc cand codegpt-action-alist)))))) (complete-with-action action codegpt-action-alist string predicate))) - nil t))) - (funcall - (pcase action - ("custom" #'codegpt-custom) - ("doc" #'codegpt-doc) - ("fix" #'codegpt-fix) - ("explain" #'codegpt-explain) - ("improve" #'codegpt-improve)) - start end))) + nil t)) + (action-fn (cdr-safe (assoc action codegpt--actions-functions)))) + (if action-fn + (funcall action-fn start end) + (codegept--execute-predefined-template + start + end + (cdr (assoc action codegpt-action-alist)))))) (provide 'codegpt) ;;; codegpt.el ends here