|
| 1 | +;;; cider-ns-state.el --- Namespace load-state tracking -*- lexical-binding: t -*- |
| 2 | + |
| 3 | +;; Copyright © 2026 Bozhidar Batsov and CIDER contributors |
| 4 | + |
| 5 | +;; Author: Bozhidar Batsov <bozhidar@batsov.dev> |
| 6 | + |
| 7 | +;; This program is free software: you can redistribute it and/or modify |
| 8 | +;; it under the terms of the GNU General Public License as published by |
| 9 | +;; the Free Software Foundation, either version 3 of the License, or |
| 10 | +;; (at your option) any later version. |
| 11 | + |
| 12 | +;; This program is distributed in the hope that it will be useful, |
| 13 | +;; but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | +;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | +;; GNU General Public License for more details. |
| 16 | + |
| 17 | +;; You should have received a copy of the GNU General Public License |
| 18 | +;; along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 19 | + |
| 20 | +;; This file is not part of GNU Emacs. |
| 21 | + |
| 22 | +;;; Commentary: |
| 23 | + |
| 24 | +;; Track whether a source buffer's namespace has been loaded into the running |
| 25 | +;; REPL, and whether the buffer has been edited since it was last loaded. The |
| 26 | +;; state is surfaced via the `cider-mode' mode-line lighter, so it's obvious |
| 27 | +;; when you're about to operate on a namespace that the runtime doesn't know |
| 28 | +;; about (or that is out of sync). |
| 29 | +;; |
| 30 | +;; This is intentionally coarse (namespace granularity). Per-form staleness |
| 31 | +;; (extending the fringe indicators) is a separate, future effort. |
| 32 | + |
| 33 | +;;; Code: |
| 34 | + |
| 35 | +(require 'subr-x) |
| 36 | + |
| 37 | +(require 'cider-client) |
| 38 | +(require 'cider-eval) |
| 39 | +(require 'nrepl-dict) |
| 40 | + |
| 41 | +(defcustom cider-show-ns-load-state t |
| 42 | + "Whether to show the namespace load-state in the `cider-mode' lighter. |
| 43 | +When non-nil, a marker appears in the mode line when the current buffer's |
| 44 | +namespace has not been loaded into the REPL, or has been edited since it was |
| 45 | +last loaded." |
| 46 | + :type 'boolean |
| 47 | + :group 'cider |
| 48 | + :package-version '(cider . "1.23.0")) |
| 49 | + |
| 50 | +(defface cider-ns-load-state-face |
| 51 | + '((t :inherit warning)) |
| 52 | + "Face for the namespace load-state marker in the mode line." |
| 53 | + :group 'cider |
| 54 | + :package-version '(cider . "1.23.0")) |
| 55 | + |
| 56 | +(defvar-local cider-ns-state--loaded nil |
| 57 | + "Cached load state of the current buffer's namespace. |
| 58 | +One of nil (unknown / not checked yet), t (loaded), or the symbol |
| 59 | +`not-loaded'. Updated asynchronously by `cider-ns-state--refresh' and |
| 60 | +on `cider-file-loaded-hook'.") |
| 61 | + |
| 62 | +(defvar-local cider-ns-state--loaded-tick nil |
| 63 | + "Value of `buffer-chars-modified-tick' when the buffer was last loaded. |
| 64 | +Used to detect that the buffer has been edited since (out of sync). Only set |
| 65 | +when CIDER itself loaded the buffer, since otherwise there is no reliable sync |
| 66 | +baseline.") |
| 67 | + |
| 68 | +(defun cider-ns-load-state () |
| 69 | + "Return the load/sync state of the current buffer's namespace. |
| 70 | +One of `loaded', `not-loaded', `out-of-sync', or nil when the state is unknown |
| 71 | +or not applicable (no connection, not a Clojure source buffer, or no |
| 72 | +namespace). |
| 73 | +
|
| 74 | +Cheap by design (it never queries the REPL) so it is safe to call from the |
| 75 | +mode line on every redisplay: when the state hasn't been determined yet |
| 76 | +`cider-ns-state--loaded' is nil and we bail out before any connection lookup." |
| 77 | + (when (and cider-ns-state--loaded |
| 78 | + (derived-mode-p 'clojure-mode 'clojure-ts-mode) |
| 79 | + (cider-connected-p)) |
| 80 | + (cond |
| 81 | + ((eq cider-ns-state--loaded 'not-loaded) 'not-loaded) |
| 82 | + ((and cider-ns-state--loaded-tick |
| 83 | + (/= cider-ns-state--loaded-tick (buffer-chars-modified-tick))) |
| 84 | + 'out-of-sync) |
| 85 | + (t 'loaded)))) |
| 86 | + |
| 87 | +(defun cider-ns-state--lighter () |
| 88 | + "Return the mode-line marker for the current buffer's namespace state. |
| 89 | +Empty unless the namespace is not loaded or is out of sync. Any error is |
| 90 | +swallowed and rendered as an empty marker, since this runs during redisplay." |
| 91 | + (if (not cider-show-ns-load-state) |
| 92 | + "" |
| 93 | + (pcase (ignore-errors (cider-ns-load-state)) |
| 94 | + ('not-loaded |
| 95 | + (propertize " not-loaded" 'face 'cider-ns-load-state-face |
| 96 | + 'help-echo "This namespace has not been loaded into the REPL. |
| 97 | +Load the buffer with `cider-load-buffer' (C-c C-k).")) |
| 98 | + ('out-of-sync |
| 99 | + (propertize " stale" 'face 'cider-ns-load-state-face |
| 100 | + 'help-echo "This buffer has been edited since it was last loaded. |
| 101 | +Reload it with `cider-load-buffer' (C-c C-k).")) |
| 102 | + (_ "")))) |
| 103 | + |
| 104 | +(defun cider-ns-state--list-async (callback) |
| 105 | + "Fetch the list of namespaces loaded in the REPL and pass it to CALLBACK. |
| 106 | +CALLBACK is invoked only when the response actually carried a namespace list, |
| 107 | +so a failed or unsupported op leaves the load-state unknown rather than |
| 108 | +falsely reporting every namespace as not loaded." |
| 109 | + (let ((loaded-nss nil)) |
| 110 | + (cider-nrepl-send-request |
| 111 | + `("op" "cider/ns-list" |
| 112 | + "exclude-regexps" ,cider-filtered-namespaces-regexps) |
| 113 | + (lambda (response) |
| 114 | + (nrepl-dbind-response response (ns-list status) |
| 115 | + (when ns-list |
| 116 | + (setq loaded-nss ns-list)) |
| 117 | + (when (and loaded-nss (member "done" status)) |
| 118 | + (funcall callback loaded-nss))))))) |
| 119 | + |
| 120 | +(defun cider-ns-state--set-from-list (buffer loaded-nss) |
| 121 | + "Update BUFFER's cached load-state from LOADED-NSS, the loaded namespaces." |
| 122 | + (when (buffer-live-p buffer) |
| 123 | + (with-current-buffer buffer |
| 124 | + (when-let* ((ns (cider-current-ns 'no-default))) |
| 125 | + (setq-local cider-ns-state--loaded |
| 126 | + (if (member ns loaded-nss) t 'not-loaded)))))) |
| 127 | + |
| 128 | +(defun cider-ns-state--refresh (&optional buffer) |
| 129 | + "Asynchronously refresh the cached namespace load-state for BUFFER. |
| 130 | +BUFFER defaults to the current buffer. Does nothing when there is no |
| 131 | +connection or the buffer has no namespace." |
| 132 | + (let ((buf (or buffer (current-buffer)))) |
| 133 | + (when (with-current-buffer buf |
| 134 | + (and (cider-connected-p) |
| 135 | + (cider-nrepl-op-supported-p "cider/ns-list" nil 'skip-ensure) |
| 136 | + (cider-current-ns 'no-default))) |
| 137 | + (cider-ns-state--list-async |
| 138 | + (lambda (loaded-nss) (cider-ns-state--set-from-list buf loaded-nss)))))) |
| 139 | + |
| 140 | +(defun cider-ns-refresh-load-state () |
| 141 | + "Re-check whether the current buffer's namespace is loaded in the REPL." |
| 142 | + (interactive) |
| 143 | + (cider-ns-state--refresh)) |
| 144 | + |
| 145 | +(defun cider-ns-state--refresh-on-focus (&optional _frame) |
| 146 | + "Refresh the load-state of the focused Clojure buffer if it's unknown. |
| 147 | +Intended for `window-selection-change-functions': when you select a connected |
| 148 | +Clojure buffer whose state hasn't been determined yet, check it once. This |
| 149 | +deliberately avoids querying at connection time (which is fragile)." |
| 150 | + (when cider-show-ns-load-state |
| 151 | + (ignore-errors |
| 152 | + (let ((buf (window-buffer (selected-window)))) |
| 153 | + (with-current-buffer buf |
| 154 | + (when (and (derived-mode-p 'clojure-mode 'clojure-ts-mode) |
| 155 | + (null cider-ns-state--loaded) |
| 156 | + (cider-connected-p)) |
| 157 | + (cider-ns-state--refresh buf))))))) |
| 158 | + |
| 159 | +(defun cider-ns-state--mark-loaded () |
| 160 | + "Mark the current buffer's namespace as loaded and in sync. |
| 161 | +Intended for `cider-file-loaded-hook'." |
| 162 | + (when (derived-mode-p 'clojure-mode 'clojure-ts-mode) |
| 163 | + (setq-local cider-ns-state--loaded t |
| 164 | + cider-ns-state--loaded-tick (buffer-chars-modified-tick)))) |
| 165 | + |
| 166 | +(defun cider-ns-state-setup () |
| 167 | + "Enable namespace load-state tracking in the current buffer. |
| 168 | +Registers a buffer-local focus hook so the state is checked when you switch |
| 169 | +to the buffer. Intended to be called when `cider-mode' is enabled." |
| 170 | + (add-hook 'window-selection-change-functions |
| 171 | + #'cider-ns-state--refresh-on-focus nil 'local)) |
| 172 | + |
| 173 | +(defun cider-ns-state-teardown () |
| 174 | + "Disable namespace load-state tracking in the current buffer. |
| 175 | +Intended to be called when `cider-mode' is disabled." |
| 176 | + (remove-hook 'window-selection-change-functions |
| 177 | + #'cider-ns-state--refresh-on-focus 'local) |
| 178 | + (kill-local-variable 'cider-ns-state--loaded) |
| 179 | + (kill-local-variable 'cider-ns-state--loaded-tick)) |
| 180 | + |
| 181 | +;; `cider-file-loaded-hook' is cheap and only fires on CIDER loads, so it stays |
| 182 | +;; global; the focus hook is per-buffer (see `cider-ns-state-setup') to avoid |
| 183 | +;; running on every window selection change in unrelated buffers. |
| 184 | +(add-hook 'cider-file-loaded-hook #'cider-ns-state--mark-loaded) |
| 185 | + |
| 186 | +(provide 'cider-ns-state) |
| 187 | + |
| 188 | +;;; cider-ns-state.el ends here |
0 commit comments