mirror of
https://github.com/SqrtMinusOne/micromamba.el.git
synced 2025-12-10 13:23:02 +03:00
Optimize micromamba--get-name-from-env-yml
1. Use setf and alist-get instead of add-to-list 2. Store only env name for buffer in cache 3. Wrap string-match and match-string in save-match-data 4. Pull the string-matching into its own function
This commit is contained in:
parent
0fe1ed0c63
commit
10355eec33
1 changed files with 12 additions and 8 deletions
|
|
@ -186,17 +186,21 @@ Returns an alist with the following keys:
|
||||||
(insert-file-contents filename)
|
(insert-file-contents filename)
|
||||||
(buffer-string))))
|
(buffer-string))))
|
||||||
|
|
||||||
|
(defun micromamba--get-name-from-env-yml-contents (env-yml-contents)
|
||||||
|
"Pull the `name` property out of a stringified YAML file"
|
||||||
|
(save-match-data
|
||||||
|
(when (string-match "name:[ ]*\\([A-z0-9-_.]+\\)[ ]*$" env-yml-contents)
|
||||||
|
(match-string 1 env-yml-contents))))
|
||||||
|
|
||||||
(defun micromamba--get-name-from-env-yml (filename) ;; adapted from conda.el
|
(defun micromamba--get-name-from-env-yml (filename) ;; adapted from conda.el
|
||||||
"Pull the `name` property out of the YAML file at FILENAME."
|
"Pull the `name` property out of the YAML file at FILENAME."
|
||||||
(when filename
|
(when filename
|
||||||
(let ((env-yml-contents
|
(let ((filename (file-truename filename)))
|
||||||
(progn
|
(or (unless (file-has-changed-p filename))
|
||||||
(when (file-has-changed-p filename)
|
(cdr (assoc filename micromamba--yml-cache))
|
||||||
(add-to-list 'micromamba--yml-cache
|
(setf (alist-get filename micromamba--yml-cache)
|
||||||
`(,filename . ,(micromamba--read-file-into-string filename))))
|
(micromamba--get-name-from-env-yml-contents
|
||||||
(cdr (assoc filename micromamba--yml-cache)))))
|
(micromamba--read-file-into-string filename)))))))
|
||||||
(when (string-match "name:[ ]*\\([A-z0-9-_.]+\\)[ ]*$" env-yml-contents)
|
|
||||||
(match-string 1 env-yml-contents)))))
|
|
||||||
|
|
||||||
(defun micromamba--infer-env-from-buffer () ;; adapted from conda.el
|
(defun micromamba--infer-env-from-buffer () ;; adapted from conda.el
|
||||||
"Search up the project tree for an `environment.yml` defining a conda env.
|
"Search up the project tree for an `environment.yml` defining a conda env.
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue