emacs-elpa-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[nongnu] elpa/projectile 20e4444dea 1/4: Add helpers for dir-local-varia


From: ELPA Syncer
Subject: [nongnu] elpa/projectile 20e4444dea 1/4: Add helpers for dir-local-variables for 3rd party use (#1737)
Date: Sun, 30 Oct 2022 11:59:22 -0400 (EDT)

branch: elpa/projectile
commit 20e4444dea4baeba332c39ab3c401d9209c62e43
Author: 813gan <29867887+813gan@users.noreply.github.com>
Commit: GitHub <noreply@github.com>

    Add helpers for dir-local-variables for 3rd party use (#1737)
    
    Functions `projectile-add-dir-local-variable` and 
`projectile-delete-dir-local-variable`
    wraps their built-in counterparts. They always use `.dir-locals.el` from 
root of projectile project.
---
 CHANGELOG.md                         |  4 ++++
 doc/modules/ROOT/pages/projects.adoc |  3 ++-
 projectile.el                        | 26 +++++++++++++++++++++
 test/projectile-test.el              | 44 ++++++++++++++++++++++++++++++++++++
 4 files changed, 76 insertions(+), 1 deletion(-)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index e4cd9f9d2f..c53973328c 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -6,6 +6,9 @@
 
 * [#1591](https://github.com/bbatsov/projectile/issues/1591): Add `project.el` 
integration that will make Projectile the default provider for project lookup.
 * Add new command `projectile-find-references` (bound to `C-c C-p ?` and `C-c 
C-p s x`).
+* Add helpers for `dir-local-variables` for 3rd party use.
+  Functions `projectile-add-dir-local-variable` and 
`projectile-delete-dir-local-variable`
+  wraps their built-in counterparts. They always use `.dir-locals.el` from 
root of projectile project.
 
 ### Bug fixed
 
@@ -27,6 +30,7 @@
 * [#1797](https://github.com/bbatsov/projectile/pull/1797): Make all project 
type attributes locally overridable.
 * [#1803](https://github.com/bbatsov/projectile/pull/1803): Add support 
go-task/task.
 
+
 ### Bugs fixed
 
 * [#1781](https://github.com/bbatsov/projectile/pull/1781): Fix `rails-rspec` 
and `rails-test` to use `app` instead of `lib` as `src-dir`.
diff --git a/doc/modules/ROOT/pages/projects.adoc 
b/doc/modules/ROOT/pages/projects.adoc
index 1d71517233..2db20e74c1 100644
--- a/doc/modules/ROOT/pages/projects.adoc
+++ b/doc/modules/ROOT/pages/projects.adoc
@@ -701,7 +701,8 @@ function.  It could also be used to e.g. add such a 
function to a key
 map.
 
 TIP: You can also quickly visit or create the `dir-locals-file` with
-kbd:[s-p E] (kbd:[M-x] `projectile-edit-dir-locals` kbd:[RET]).
+kbd:[s-p E] (kbd:[M-x] `projectile-edit-dir-locals` kbd:[RET]). 3rd party 
packages may use functions `projectile-add-dir-local-variable`
+and `projectile-delete-dir-local-variable` to store their settings.
 
 Here are a few examples of how to use this feature with Projectile.
 
diff --git a/projectile.el b/projectile.el
index ac326e8ea8..0fcdcdaaab 100644
--- a/projectile.el
+++ b/projectile.el
@@ -2423,6 +2423,32 @@ With a prefix arg INVALIDATE-CACHE invalidates the cache 
first."
       (read-only-mode (if val +1 -1))
       (message "[%s] read-only-mode is %s" (projectile-project-name) (if val 
"on" "off")))))
 
+;;;###autoload
+(defun projectile-add-dir-local-variable (mode variable value)
+  "Run `add-dir-local-variable' with .dir-locals.el in root of project.
+
+Parameters MODE VARIABLE VALUE are passed directly to `add-dir-local-variable'"
+  (let ((inhibit-read-only t)
+        (default-directory (projectile-project-root)))
+    (unless default-directory
+      (error "Setting dir-local variable in non-existing projectile project 
was requested"))
+    (add-dir-local-variable mode variable value)
+    (save-buffer)
+    (kill-buffer) ))
+
+;;;###autoload
+(defun projectile-delete-dir-local-variable (mode variable)
+  "Run `delete-dir-local-variable' with .dir-locals.el in root of project.
+
+Parameters MODE VARIABLE VALUE are passed directly to 
`delete-dir-local-variable'"
+  (let ((inhibit-read-only t)
+        (default-directory (projectile-project-root)))
+    (unless default-directory
+      (error "Setting dir-local variable in non-existing projectile project 
was requested"))
+    (delete-dir-local-variable mode variable)
+    (save-buffer)
+    (kill-buffer) ))
+
 
 ;;;; Sorting project files
 (defun projectile-sort-files (files)
diff --git a/test/projectile-test.el b/test/projectile-test.el
index 875b01ec52..f277af14c6 100644
--- a/test/projectile-test.el
+++ b/test/projectile-test.el
@@ -990,6 +990,50 @@ Just delegates OPERATION and ARGS for all operations 
except for`shell-command`'.
     (let ((projectile-known-projects nil))
       (expect (projectile-switch-project) :to-throw))))
 
+(describe "projectile-delete-dir-local-variable"
+          (it "Deletes existing dir-local variables"
+              (projectile-test-with-sandbox
+               (projectile-test-with-files
+                ("project/"
+                 "project/.dir-locals.el"
+                 "project/.projectile")
+                (append-to-file
+                 "((nil . ((foo . bar))))" nil "project/.dir-locals.el")
+                (with-current-buffer (find-file-noselect "project/.projectile" 
t)
+                  (let ((enable-local-variables :all))
+                    (hack-dir-local-variables-non-file-buffer)
+                    (expect (boundp 'foo) :to-be 't)
+
+                    (projectile-delete-dir-local-variable nil 'foo)
+                    (expect (boundp 'foo) :to-be nil) ))))))
+
+(describe "projectile-add-dir-local-variable"
+          (it "Adds new dir-local variables"
+              (projectile-test-with-sandbox
+               (projectile-test-with-files
+                ("project/"
+                 "project/.projectile")
+                (with-current-buffer (find-file-noselect "project/.projectile" 
t)
+                  (let ((enable-local-variables :all))
+                    (expect (boundp 'fooo) :to-be nil)
+
+                    (projectile-add-dir-local-variable nil 'fooo 1)
+                    (hack-dir-local-variables-non-file-buffer)
+                    (expect (boundp 'fooo) :to-be 't)
+                    (expect fooo :to-be 1) ))))))
+
+(describe "projectile-add-dir-local-variable"
+          (it "Fails when there is no projectile project"
+              (projectile-test-with-sandbox
+                (let ((default-directory "/"))
+                  (expect (projectile-add-dir-local-variable nil 'fooo 1) 
:to-throw 'error)) )))
+
+(describe "projectile-delete-dir-local-variable"
+          (it "Fails when there is no projectile project"
+              (projectile-test-with-sandbox
+                (let ((default-directory "/"))
+                  (expect (projectile-delete-dir-local-variable nil 'fooo 1) 
:to-throw 'error)) )))
+
 (describe "projectile-switch-project-by-name"
   (it "calls the switch project action with project-to-switch's dir-locals 
loaded"
     (defvar switch-project-foo)



reply via email to

[Prev in Thread] Current Thread [Next in Thread]