branch: externals/ellama
commit 20a78f49ba4e65c5fb4e8e8457c9d791f11f09d1
Author: Sergey Kostyaev <sskosty...@gmail.com>
Commit: Sergey Kostyaev <sskosty...@gmail.com>

    Refactor ellama-code-add function for better efficiency
    
    Remove redundant context handling in `ellama-code-add`. Simplify the
    prompt template by removing unnecessary context instructions. Improve
    function documentation for clarity. Add region selection handling
    outside of ellama-stream call.
---
 ellama.el | 30 +++++++++++++-----------------
 1 file changed, 13 insertions(+), 17 deletions(-)

diff --git a/ellama.el b/ellama.el
index 2ad36970f7..9c6da67554 100644
--- a/ellama.el
+++ b/ellama.el
@@ -305,7 +305,7 @@ Write text, based on provided context and instruction. Do 
not add any explanatio
   :group 'ellama
   :type 'string)
 
-(defcustom ellama-code-add-prompt-template "Context: \n```\n%s\n```\nBased on 
this context, %s, only output the result in format ```\n...\n```\nWrite all the 
code in single code block."
+(defcustom ellama-code-add-prompt-template "Based on context, %s, only output 
the result in format ```\n...\n```\nWrite all the code in single code block."
   "Prompt template for `ellama-code-add'."
   :group 'ellama
   :type 'string)
@@ -2330,23 +2330,19 @@ prefix (\\[universal-argument]), prompt the user to 
amend the template."
 
 ;;;###autoload
 (defun ellama-code-add (description)
-  "Add new code according to DESCRIPTION.
-Code will be generated with provided context from selected region or current
-buffer."
+  "Generate and insert new code based on DESCRIPTION.
+This function prompts the user to describe the code they want to generate.
+If a region is active, it includes the selected text as context for code
+generation."
   (interactive "sDescribe the code to be generated: ")
-  (let* ((beg (if (region-active-p)
-                 (region-beginning)
-               (point-min)))
-        (end (if (region-active-p)
-                 (region-end)
-               (point-max)))
-        (text (buffer-substring-no-properties beg end)))
-    (ellama-stream
-     (format
-      ellama-code-add-prompt-template
-      text description)
-     :provider ellama-coding-provider
-     :filter #'ellama--code-filter)))
+  (when (region-active-p)
+    (ellama-context-add-selection))
+  (ellama-stream
+   (format
+    ellama-code-add-prompt-template
+    description)
+   :provider ellama-coding-provider
+   :filter #'ellama--code-filter))
 
 
 ;;;###autoload

Reply via email to