diff options
| author | Craig Jennings <c@cjennings.net> | 2026-05-03 21:17:30 -0500 |
|---|---|---|
| committer | Craig Jennings <c@cjennings.net> | 2026-05-03 21:17:30 -0500 |
| commit | 2f8d8989856073cbed5f9159d02089903bc5343e (patch) | |
| tree | 1ab81336390846105c92debcc8e788d0d5905e22 /modules | |
| parent | 31edc86a54d20c3c73d0ebad247fde2c35e6a964 (diff) | |
| download | dotemacs-2f8d8989856073cbed5f9159d02089903bc5343e.tar.gz dotemacs-2f8d8989856073cbed5f9159d02089903bc5343e.zip | |
refactor: defer projectile revert advice until projectile loads
`dev-fkeys.el` was wiring its three Projectile cache-revert advices via top-level `advice-add` calls using `apply-partially #'cj/--projectile-around-revert <map-symbol>`. That had three problems. The advice values were anonymous closures, so `advice-member-p` couldn't find them and a re-load would silently double-install. The implicit dependency on Projectile was load-ordered by accident. If `dev-fkeys.el` happened to require before Projectile loaded, the advice still attached to unbound symbols. And a fresh batch require of `dev-fkeys.el` for tests would always force the advice attempt regardless of whether Projectile was around.
I gave each Projectile target a named advice wrapper (`cj/--projectile-compile-around-revert`, `cj/--projectile-test-around-revert`, `cj/--projectile-run-around-revert`) and put the (target . advice) pairs in a `cj/--projectile-revert-advice-specs` defconst. `cj/--projectile-install-revert-advice` walks the specs, checks `fboundp` plus `advice-member-p`, and only adds advice that's missing. The installer is idempotent on reload, and the named wrappers make it easy to tear down later by symbol name.
`cj/--projectile-register-revert-advice` is the entry point at module load time. It installs immediately when Projectile is already a `featurep`, otherwise it schedules the installer through `eval-after-load 'projectile`. Either way the advice is in place once Projectile is available, and `dev-fkeys.el` no longer relies on a particular load order.
Tests in the new `tests/test-dev-fkeys--projectile-advice-install.el` cover four cases. Registration defers via `eval-after-load` when Projectile isn't a feature yet. Registration installs immediately when it is. Install skips unbound Projectile functions. Install advises each bound Projectile command runner with the matching named wrapper. 23 projectile-related tests pass together.
Diffstat (limited to 'modules')
| -rw-r--r-- | modules/dev-fkeys.el | 64 |
1 files changed, 38 insertions, 26 deletions
diff --git a/modules/dev-fkeys.el b/modules/dev-fkeys.el index 0b973470..c9a5fc13 100644 --- a/modules/dev-fkeys.el +++ b/modules/dev-fkeys.el @@ -179,11 +179,6 @@ a single Compile entry that calls plain `compile'." ;; buffer-local compilation finish hook so overlapping compiles cannot ;; overwrite or consume one another's revert metadata. -(defvar cj/--projectile-revert-state nil - "Legacy dynamic state used by direct tests of `cj/--projectile-revert-on-fail'. -The around advice no longer stores live compile metadata here; it closes -over the plist returned by `cj/--projectile-capture-cmd' instead.") - (defun cj/--projectile-capture-cmd (map-symbol) "Capture the cached cmd at the project root in MAP-SYMBOL. MAP-SYMBOL is the symbol of a projectile cmd-map (e.g. @@ -210,18 +205,6 @@ cache alone." (not (equal prior current))) (puthash root prior (symbol-value map)))))) -(defun cj/--projectile-revert-on-fail (_buf status) - "Compilation-finish hook: revert projectile cache on failed-and-modified. -Always self-removes from `compilation-finish-functions' and clears -`cj/--projectile-revert-state'. Reverts the cmd-map entry only when the -compile failed AND the cmd was modified from the captured prior value -AND that prior was non-nil. The unchanged-and-failed case (test fails -because of a real bug) leaves the cache alone." - (remove-hook 'compilation-finish-functions #'cj/--projectile-revert-on-fail) - (let ((state cj/--projectile-revert-state)) - (setq cj/--projectile-revert-state nil) - (cj/--projectile-revert-state-on-fail state status))) - (defun cj/--projectile-make-revert-on-fail-hook (state) "Return a one-shot buffer-local finish hook for projectile revert STATE." (let (hook) @@ -497,15 +480,44 @@ message." ;; ---------- Projectile advice ---------- -(advice-add 'projectile-compile-project :around - (apply-partially #'cj/--projectile-around-revert - 'projectile-compile-cmd-map)) -(advice-add 'projectile-test-project :around - (apply-partially #'cj/--projectile-around-revert - 'projectile-test-cmd-map)) -(advice-add 'projectile-run-project :around - (apply-partially #'cj/--projectile-around-revert - 'projectile-run-cmd-map)) +(defun cj/--projectile-compile-around-revert (orig-fn &rest args) + "Around advice for `projectile-compile-project' command-cache revert." + (apply #'cj/--projectile-around-revert + 'projectile-compile-cmd-map orig-fn args)) + +(defun cj/--projectile-test-around-revert (orig-fn &rest args) + "Around advice for `projectile-test-project' command-cache revert." + (apply #'cj/--projectile-around-revert + 'projectile-test-cmd-map orig-fn args)) + +(defun cj/--projectile-run-around-revert (orig-fn &rest args) + "Around advice for `projectile-run-project' command-cache revert." + (apply #'cj/--projectile-around-revert + 'projectile-run-cmd-map orig-fn args)) + +(defconst cj/--projectile-revert-advice-specs + '((projectile-compile-project . cj/--projectile-compile-around-revert) + (projectile-test-project . cj/--projectile-test-around-revert) + (projectile-run-project . cj/--projectile-run-around-revert)) + "Projectile command runners and their command-cache revert advice.") + +(defun cj/--projectile-install-revert-advice () + "Install Projectile command-cache revert advice when Projectile is available." + (dolist (spec cj/--projectile-revert-advice-specs) + (let ((target (car spec)) + (advice (cdr spec))) + (when (and (fboundp target) + (not (advice-member-p advice target))) + (advice-add target :around advice))))) + +(defun cj/--projectile-register-revert-advice () + "Install Projectile revert advice now, or after Projectile loads." + (if (featurep 'projectile) + (cj/--projectile-install-revert-advice) + (eval-after-load 'projectile + (list 'cj/--projectile-install-revert-advice)))) + +(cj/--projectile-register-revert-advice) ;; ---------- Bindings ---------- |
