branch: scratch/editorconfig-cc commit fe98f81020e5fd7591a5a300223becb01385f1dd Author: 10sr <8.slas...@gmail.com> Commit: Stefan Monnier <monn...@iro.umontreal.ca>
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 | 69 ++++++++++++++++++++++++++------ 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, 101 insertions(+), 13 deletions(-) diff --git a/editorconfig.el b/editorconfig.el index 79b229e36a..681ba115c9 100644 --- a/editorconfig.el +++ b/editorconfig.el @@ -339,6 +339,16 @@ number - `lisp-indent-offset' is not set only if indent_size is (defconst editorconfig-unset-value "unset" "String of value used to unset properties in .editorconfig .") +(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") @@ -390,22 +400,55 @@ 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))) + (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) + ) + + (cond ((not (editorconfig--should-set 'indent-tabs-mode)) + nil) + ) + + (when (editorconfig--should-set 'evil-shift-width) + ) ) (defvar-local editorconfig--apply-coding-system-currently nil diff --git a/ert-tests/editorconfig.el b/ert-tests/editorconfig.el index e413a5939a..b281f8014b 100644 --- a/ert-tests/editorconfig.el +++ b/ert-tests/editorconfig.el @@ -48,6 +48,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) @@ -92,6 +95,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: