Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a visual progress for when evaluating a directory recursively #3615

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

### Changes

- Show progress status when loading `all-files` recursively.
- Bump the injected nREPL to 1.1.1.

## 1.13.1 (2024-02-01)
Expand Down
38 changes: 28 additions & 10 deletions cider-eval.el
Original file line number Diff line number Diff line change
Expand Up @@ -1789,14 +1789,15 @@ passing arguments."
(widen)
(substring-no-properties (buffer-string)))))

(defun cider-load-buffer (&optional buffer callback undef-all)
(defun cider-load-buffer (&optional buffer callback undef-all progress)
"Load (eval) BUFFER's file in nREPL.
If no buffer is provided the command acts on the current buffer. If the
buffer is for a cljc file, and both a Clojure and ClojureScript REPL exists
for the project, it is evaluated in both REPLs.
Optional argument CALLBACK will override the default ‘cider-load-file-handler’.
When UNDEF-ALL is non-nil or called with \\[universal-argument], removes
all ns aliases and var mappings from the namespace before reloading it."
all ns aliases and var mappings from the namespace before reloading it.
PROGRESS is a formatted progress status thats shown on the loading message."
(interactive (list (current-buffer) nil (equal current-prefix-arg '(4))))
(setq buffer (or buffer (current-buffer)))
;; When cider-load-buffer or cider-load-file are called in programs the
Expand Down Expand Up @@ -1831,33 +1832,50 @@ all ns aliases and var mappings from the namespace before reloading it."
(file-name-nondirectory filename)
repl
callback)))
(message "Loading %s..." filename))))))
(message (concat "Loading " progress "%s...") filename))))))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should not use concat in message, as you can just use format specifiers like %s, %d, etc. It's similar to Clojure's format.


(defun cider-load-file (filename &optional undef-all)
(defun cider-load-file (filename &optional undef-all progress)
"Load (eval) the Clojure file FILENAME in nREPL.
If the file is a cljc file, and both a Clojure and ClojureScript REPL
exists for the project, it is evaluated in both REPLs. The heavy lifting
is done by `cider-load-buffer'.
When UNDEF-ALL is non-nil or called with \\[universal-argument], removes
all ns aliases and var mappings from the namespace before reloading it."
all ns aliases and var mappings from the namespace before reloading it.
PROGRESS is a formatted progress status thats shown on the loading message."
(interactive (list
(read-file-name "Load file: " nil nil nil
(read-file-name "Load file:"
nil nil nil
(when (buffer-file-name)
(file-name-nondirectory
(buffer-file-name))))
(equal current-prefix-arg '(4))))
(if-let* ((buffer (find-buffer-visiting filename)))
(cider-load-buffer buffer nil undef-all)
(cider-load-buffer (find-file-noselect filename) nil undef-all)))
(cider-load-buffer (find-file-noselect filename) nil undef-all progress)))

(defun index-files (files)
"Index each file in FILES with its formatted progress."
(let* ((file-count (length files))
(indexes (number-sequence 1 file-count)))
(cl-mapcar (lambda (index file)
(list file (format "[%s/%s] " index file-count)))
indexes
files)))

(defun cider-load-all-files (directory undef-all)
"Load all files in DIRECTORY (recursively).
Useful when the running nREPL on remote host.
When UNDEF-ALL is non-nil or called with \\[universal-argument], removes
all ns aliases and var mappings from the namespaces being reloaded"
all ns aliases and var mappings from the namespaces being reloaded."
(interactive "DLoad files beneath directory: \nP")
(mapcar (lambda (file) (cider-load-file file undef-all))
(directory-files-recursively directory "\\.clj[cs]?$")))
(let* ((files (directory-files-recursively directory "\\.clj[cs]?$"))
(indexed-files (index-files files)))
(mapcar (lambda (indexed-file)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While number-sequence is a nice function, I'd prefer it you used local mutation as it would seem easier to follow here.

An example:

(let* ((files '("a" "b" "c"))
       (i 0))
        (mapcar (lambda (file)
                  (message (format "%s %s" file i))
                  (setq i (inc i)))
                files))

So, you can use the existing mapcar to mutate a local counter, so we have one mapcar instead of two (plus the helper function).

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for pointing out. My elisp knowledge is really limited and that helped a lot. Thanks!

(let ((filename (car indexed-file))
(progress (car (cdr indexed-file))))
(cider-load-file filename undef-all progress)))
indexed-files)))


(defalias 'cider-eval-file #'cider-load-file
"A convenience alias as some people are confused by the load-* names.")
Expand Down