Skip to content

Commit 615ad75

Browse files
committed
[clojure-emacs/cider#2281] Cache the results of clojure-project-dir
1 parent 2fddd43 commit 615ad75

File tree

3 files changed

+25
-3
lines changed

3 files changed

+25
-3
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22

33
## master (unreleased)
44

5+
### Changes
6+
57
* Indent `fdef` (clojure.spec) like a `def`.
8+
* The results of `clojure-project-dir` are cached by default to optimize performance.
69

710
## 5.7.0 (2018-04-29)
811

clojure-mode.el

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1629,12 +1629,30 @@ nil."
16291629

16301630

16311631

1632+
(defcustom clojure-cache-project-dir t
1633+
"Whether to cache the results of `clojure-project-dir'."
1634+
:type 'boolean
1635+
:safe #'booleanp
1636+
:package-version '(clojure-mode . "5.8.0"))
1637+
1638+
(defvar-local clojure-cached-project-dir nil
1639+
"A project dir cache used to speed up related operations.")
1640+
16321641
(defun clojure-project-dir (&optional dir-name)
16331642
"Return the absolute path to the project's root directory.
16341643
16351644
Call is delegated down to `clojure-project-root-function' with
1636-
optional DIR-NAME as argument."
1637-
(funcall clojure-project-root-function dir-name))
1645+
optional DIR-NAME as argument.
1646+
1647+
When `clojure-cache-project-dir' is t the results of the command
1648+
are cached in a buffer local variable (`clojure-cached-project-dir')."
1649+
(let ((project-dir (or clojure-cached-project-dir
1650+
(funcall clojure-project-root-function dir-name))))
1651+
(when (and clojure-cache-project-dir
1652+
(derived-mode-p 'clojure-mode)
1653+
(not clojure-cached-project-dir))
1654+
(setq clojure-cached-project-dir project-dir))
1655+
project-dir))
16381656

16391657
(defun clojure-project-root-path (&optional dir-name)
16401658
"Return the absolute path to the project's root directory.

test/clojure-mode-util-test.el

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@
2929
(let ((project-dir "/home/user/projects/my-project/")
3030
(clj-file-path "/home/user/projects/my-project/src/clj/my_project/my_ns/my_file.clj")
3131
(project-relative-clj-file-path "src/clj/my_project/my_ns/my_file.clj")
32-
(clj-file-ns "my-project.my-ns.my-file"))
32+
(clj-file-ns "my-project.my-ns.my-file")
33+
(clojure-cache-project nil))
3334

3435
(ert-deftest project-relative-path ()
3536
:tags '(utils)

0 commit comments

Comments
 (0)