branch: elpa/editorconfig
commit 150f8d711203b91a7ddac3ed79a65289b0f87364
Author: 10sr <[email protected]>
Commit: GitHub <[email protected]>
Add switch not to override local variable values (#338)
* Add tests
* Define variables
* Update --should-set
* Add fixes to work
* Update test and fix
* Fix variables
* Use defcustom for switch variables
* Remove comment
---
editorconfig.el | 80 ++++++++++++++++++++++++--------
ert-tests/editorconfig.el | 24 ++++++++++
ert-tests/local_variables/.dir-locals.el | 2 +
ert-tests/local_variables/.editorconfig | 9 ++++
ert-tests/local_variables/dir_locals.c | 3 ++
ert-tests/local_variables/file_locals.rb | 7 +++
6 files changed, 106 insertions(+), 19 deletions(-)
diff --git a/editorconfig.el b/editorconfig.el
index ce780b41f3..71aa515ca9 100644
--- a/editorconfig.el
+++ b/editorconfig.el
@@ -182,7 +182,7 @@ This hook will be run even when there are no matching
sections in
;; For contributors: Sort modes in alphabetical order
'((apache-mode apache-indent-level)
(awk-mode c-basic-offset)
- (bash-ts-mode sh-basic-offset
+ (bash-ts-mode sh-basic-offset
sh-indentation)
(bpftrace-mode c-basic-offset)
(c++-mode c-basic-offset)
@@ -398,6 +398,16 @@ number - `lisp-indent-offset' is not set only if
indent_size is
equal to this number. For example, if this is set to 2,
`lisp-indent-offset' will not be set only if indent_size is 2.")
+(defcustom editorconfig-override-file-local-variables t
+ "Non-nil means editorconfig will override file local variable values."
+ :type 'boolean
+ :group 'editorconfig)
+
+(defcustom editorconfig-override-dir-local-variables t
+ "Non-nil means editorconfig will override values defined in dir-locals.el ."
+ :type 'boolean
+ :group 'editorconfig)
+
(define-error 'editorconfig-error
"Error thrown from editorconfig lib")
@@ -449,34 +459,66 @@ Make a message by passing ARGS to `format-message'."
(when (boundp 'LaTeX-item-indent)
(setq-local LaTeX-item-indent (- size))))
-(defun editorconfig--should-set (size symbol)
- "Determines if editorconfig should set SYMBOL using SIZE."
- (if (eq symbol 'lisp-indent-offset)
- (cond
- ((null editorconfig-lisp-use-default-indent) t)
- ((eql t editorconfig-lisp-use-default-indent) nil)
- ((numberp editorconfig-lisp-use-default-indent)
- (not (eql size editorconfig-lisp-use-default-indent)))
- (t t))
- t))
+(cl-defun editorconfig--should-set (symbol &optional size)
+ "Determine if editorconfig should set SYMBOL.
+
+Optional arg SIZE is used when symbol is `lisp-indent-offset'.
+See `editorconfig-lisp-use-default-indent' for details."
+ (display-warning '(editorconfig editorconfig--should-set)
+ (format "symbol: %S | size: %S"
+ symbol
+ size)
+ :debug)
+ (when (and (not editorconfig-override-file-local-variables)
+ (assq symbol file-local-variables-alist))
+ (cl-return-from editorconfig--should-set
+ nil))
+
+ (when (and (not editorconfig-override-dir-local-variables)
+ (assq symbol dir-local-variables-alist))
+ (cl-return-from editorconfig--should-set
+ nil))
+
+ (when (eq symbol 'lisp-indent-offset)
+ (cl-return-from editorconfig--should-set
+ (cond ((null editorconfig-lisp-use-default-indent) t)
+ ((eql t editorconfig-lisp-use-default-indent) nil)
+ ((numberp editorconfig-lisp-use-default-indent)
+ (not (eql size editorconfig-lisp-use-default-indent)))
+ (t t))))
+
+ t)
(defun editorconfig-set-indentation (style &optional size tab_width)
"Set indentation type from STYLE, SIZE and TAB_WIDTH."
- (if (editorconfig-string-integer-p size)
- (setq size (string-to-number size))
- (unless (equal size "tab") (setq size nil)))
- (cond (tab_width
+ (setq size
+ (cond ((editorconfig-string-integer-p size)
+ (string-to-number size))
+ ((equal size "tab")
+ "tab")
+ (t
+ nil)))
+
+ (cond ((not (editorconfig--should-set 'tab-width))
+ nil)
+ (tab_width
(setq tab-width (string-to-number tab_width)))
((numberp size)
(setq tab-width size)))
+
(when (equal size "tab")
(setq size tab-width))
- (cond ((equal style "space")
+
+ (cond ((not (editorconfig--should-set 'indent-tabs-mode))
+ nil)
+ ((equal style "space")
(setq indent-tabs-mode nil))
((equal style "tab")
(setq indent-tabs-mode t)))
+
(when size
- (when (featurep 'evil)
+ (when (and (featurep 'evil)
+ (editorconfig--should-set 'evil-shift-width))
(setq-local evil-shift-width size))
(let ((parent major-mode)
entry)
@@ -490,10 +532,10 @@ Make a message by passing ARGS to `format-message'."
((listp fn-or-list)
(dolist (elem fn-or-list)
(cond ((and (symbolp elem)
- (editorconfig--should-set size elem))
+ (editorconfig--should-set elem size))
(set (make-local-variable elem) size))
((and (consp elem)
- (editorconfig--should-set size (car elem)))
+ (editorconfig--should-set (car elem) size))
(let ((spec (cdr elem)))
(set (make-local-variable (car elem))
(cond ((functionp spec) (funcall spec size))
diff --git a/ert-tests/editorconfig.el b/ert-tests/editorconfig.el
index 16561430fd..8a7a1b965b 100644
--- a/ert-tests/editorconfig.el
+++ b/ert-tests/editorconfig.el
@@ -63,6 +63,9 @@
(defvar editorconfig-secondary-ert-dir
(concat default-directory "ert-tests/test_files_secondary/"))
+(defvar editorconfig-local-variables-ert-dir
+ (concat default-directory "ert-tests/local_variables/"))
+
(ert-deftest test-editorconfig nil
"Check if properties are applied."
(editorconfig-mode 1)
@@ -107,6 +110,27 @@
write-file-functions))))
(editorconfig-mode -1))
+(ert-deftest test-local-variables nil
+ (editorconfig-mode 1)
+ (with-visit-file (concat editorconfig-local-variables-ert-dir
"file_locals.rb")
+ (should (eq tab-width 9))
+ (should (eq ruby-indent-level 7)))
+
+ (with-visit-file (concat editorconfig-local-variables-ert-dir "dir_locals.c")
+ (should (eq tab-width 9))
+ (should (eq c-basic-offset 7)))
+
+ (let ((editorconfig-override-file-local-variables nil))
+ (with-visit-file (concat editorconfig-local-variables-ert-dir
"file_locals.rb")
+ (should (eq tab-width 5))
+ (should (eq ruby-indent-level 3))))
+
+ (let ((editorconfig-override-dir-local-variables nil))
+ (with-visit-file (concat editorconfig-local-variables-ert-dir
"dir_locals.c")
+ (should (eq tab-width 5))
+ (should (eq c-basic-offset 3))))
+ (editorconfig-mode -1))
+
(ert-deftest test-file-type-emacs nil
:expected-result t ;; Ignore failure
(editorconfig-mode 1)
diff --git a/ert-tests/local_variables/.dir-locals.el
b/ert-tests/local_variables/.dir-locals.el
new file mode 100644
index 0000000000..7ff1903af9
--- /dev/null
+++ b/ert-tests/local_variables/.dir-locals.el
@@ -0,0 +1,2 @@
+((c-mode . ((tab-width . 5)
+ (c-basic-offset . 3))))
diff --git a/ert-tests/local_variables/.editorconfig
b/ert-tests/local_variables/.editorconfig
new file mode 100644
index 0000000000..e7ffcfe133
--- /dev/null
+++ b/ert-tests/local_variables/.editorconfig
@@ -0,0 +1,9 @@
+root = true
+
+[*.rb]
+tab_width = 9
+indent_size = 7
+
+[*.c]
+tab_width = 9
+indent_size = 7
diff --git a/ert-tests/local_variables/dir_locals.c
b/ert-tests/local_variables/dir_locals.c
new file mode 100644
index 0000000000..9451c8f623
--- /dev/null
+++ b/ert-tests/local_variables/dir_locals.c
@@ -0,0 +1,3 @@
+int f(){
+ return 1;
+}
diff --git a/ert-tests/local_variables/file_locals.rb
b/ert-tests/local_variables/file_locals.rb
new file mode 100755
index 0000000000..e40800b209
--- /dev/null
+++ b/ert-tests/local_variables/file_locals.rb
@@ -0,0 +1,7 @@
+# -*- tab-width: 5; -*-
+def f
+ return 1
+end
+# Local Variables:
+# ruby-indent-level: 3
+# End: