branch: externals/minuet commit 9cf3a6f5aa007633ec196d6a3d9181bbc1396162 Author: Milan Glacier <d...@milanglacier.com> Commit: Milan Glacier <d...@milanglacier.com>
refactor: clean up code according to emacs-devel suggestion. Clean up code according to suggestion from Stefan Monnier: https://lists.gnu.org/archive/html/emacs-devel/2025-03/msg00578.html --- .gitignore | 4 ++++ minuet.el | 61 ++++++++++++++++++++++++------------------------------------- 2 files changed, 28 insertions(+), 37 deletions(-) diff --git a/.gitignore b/.gitignore index cc7ca80deb..c22623ce67 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,7 @@ .DS_Store .aider* *.elc + +# ELPA-generated files +/minuet-autoloads.el +/minuet-pkg.el diff --git a/minuet.el b/minuet.el index afef558392..edfa887971 100644 --- a/minuet.el +++ b/minuet.el @@ -58,8 +58,7 @@ (defcustom minuet-auto-suggestion-debounce-delay 0.4 "Debounce delay in seconds for auto-suggestions." - :type 'number - :group 'minuet) + :type 'number) (defcustom minuet-auto-suggestion-block-functions '(minuet-evil-not-insert-state-p) "List of functions to determine whether auto-suggestions should be blocked. @@ -67,13 +66,11 @@ Each function should return non-nil if auto-suggestions should be blocked. If any function in this list returns non-nil, auto-suggestions will not be shown." - :type '(repeat function) - :group 'minuet) + :type '(repeat function)) (defcustom minuet-auto-suggestion-throttle-delay 1.0 "Minimum time in seconds between auto-suggestions." - :type 'number - :group 'minuet) + :type 'number) (defface minuet-suggestion-face '((t :inherit shadow)) @@ -117,36 +114,31 @@ Must be one of the supported providers: codestral, openai, claude, etc." (const :tag "Claude" claude) (const :tag "OpenAI Compatible" openai-compatible) (const :tag "OpenAI FIM Compatible" openai-fim-compatible) - (const :tag "Gemini" gemini)) - :group 'minuet) + (const :tag "Gemini" gemini))) (defcustom minuet-context-window 16000 "The maximum total characters of the context before and after cursor. This limits how much surrounding code is sent to the LLM for context. The default is 16000 characters which would roughly equate 4000 tokens." - :type 'integer - :group 'minuet) + :type 'integer) (defcustom minuet-context-ratio 0.75 "Ratio of context before cursor vs after cursor. When the total characters exceed the context window, this ratio determines how much context to keep before vs after the cursor. A larger ratio means more context before the cursor will be used." - :type 'float - :group 'minuet) + :type 'float) (defcustom minuet-request-timeout 3 "Maximum timeout in seconds for sending completion requests." - :type 'integer - :group 'minuet) + :type 'integer) (defcustom minuet-add-single-line-entry t "Whether to create additional single-line completion items. When non-nil and a completion item has multiple lines, create another completion item containing only its first line." - :type 'boolean - :group 'minuet) + :type 'boolean) (defcustom minuet-after-cursor-filter-length 15 "Length of context after cursor used to filter completion text. @@ -160,8 +152,7 @@ completion text contains \"fib\", then \"fib\" and subsequent text will be removed. This setting filters repeated text generated by the LLM. A large value (e.g., 15) is recommended to avoid false positives." - :type 'integer - :group 'minuet) + :type 'integer) (defcustom minuet-n-completions 3 "Number of completion items. @@ -171,8 +162,7 @@ that when `minuet-add-single-line-entry` is true, the actual number of returned items may exceed this value. Additionally, the LLM cannot guarantee the exact number of completion items specified, as this parameter serves only as a prompt guideline. The default is `3`." - :type 'integer - :group 'minuet) + :type 'integer) (defvar minuet-default-prompt "You are the backend of an AI-powered code completion engine. Your task is to @@ -512,17 +502,15 @@ Also print the MESSAGE when MESSAGE-P is t." (defun minuet--remove-spaces (items) "Remove trailing and leading spaces in each item in ITEMS." - ;; emacs use \\` and \\' to match the beginning/end of the string, + ;; Emacs use \\` and \\' to match the beginning/end of the string, ;; ^ and $ are used to match bol or eol (setq items (mapcar (lambda (x) (if (or (equal x "") (string-match "\\`[\s\t\n]+\\'" x)) nil - (setq x (replace-regexp-in-string "[\s\t\n]+\\'" "" x) - x (replace-regexp-in-string "\\`[\s\t\n]+" "" x)))) + (string-trim x))) items) - items (seq-filter #'identity items)) - items) + items (seq-filter #'identity items))) (defun minuet--get-context () "Get the context for minuet completion." @@ -631,25 +619,24 @@ The filter sequence is obtained from CONTEXT." (setq response (split-string response "[\r]?\n")) (let (result) (dolist (line response) - (if-let* ((json (condition-case _ - (json-parse-string - (replace-regexp-in-string "^data: " "" line) - :object-type 'plist :array-type 'list) - (error nil))) - (text (condition-case _ - (funcall get-text-fn json) - (error nil)))) + (if-let* ((json (ignore-errors + (json-parse-string + (replace-regexp-in-string "^data: " "" line) + :object-type 'plist :array-type 'list))) + (text (ignore-errors + (funcall get-text-fn json)))) (when (and (stringp text) (not (equal text ""))) - (setq result `(,@result ,text))))) - (setq result (apply #'concat result)) + (push text result)))) + (setq result (apply #'concat (nreverse result))) (if (equal result "") (progn (minuet--log (format "Minuet returns no text for streaming: %s" response)) nil) result))) (defmacro minuet--make-process-stream-filter (response) - "Store the data into RESPONSE which is a plain list." + "Store the data into RESPONSE which should hold a plain list." + (declare (debug (gv-place))) `(lambda (proc text) (funcall #'internal-default-process-filter proc text) ;; (setq ,response (append ,response (list text))) @@ -691,6 +678,7 @@ conjunction with `minuet--make-process-stream-filter'. The `--response--' variable is initialized as an empty list and can be used to accumulate text output from a process. After execution, `--response--' will contain the collected responses in reverse order." + (declare (debug t) (indent 0)) `(let (--response--) ,@body)) ;;;###autoload @@ -1182,7 +1170,6 @@ If context is incomplete, remove last line to avoid partial code." When enabled, Minuet will automatically show suggestions while you type." :init-value nil :lighter " Minuet" - :group 'minuet (if minuet-auto-suggestion-mode (minuet--setup-auto-suggestion) (minuet--cleanup-auto-suggestion)))