branch: scratch/editorconfig
commit 0d207c59e18e7eb2a20ac857ad3055f2fdcb350a
Author: Stefan Monnier <monn...@iro.umontreal.ca>
Commit: Stefan Monnier <monn...@iro.umontreal.ca>

    WiP: Fix test regressions
---
 Makefile                  |  2 +-
 editorconfig.el           | 56 ++++++++++++++++++++++++++++-------------------
 ert-tests/editorconfig.el | 23 +++++++++++++++----
 3 files changed, 54 insertions(+), 27 deletions(-)

diff --git a/Makefile b/Makefile
index 963f6de2ed..a4e5338c50 100644
--- a/Makefile
+++ b/Makefile
@@ -14,7 +14,7 @@ ERT_TESTS = $(wildcard $(PROJECT_ROOT_DIR)/ert-tests/*.el)
 BATCHFLAGS = -batch -q --no-site-file -L $(PROJECT_ROOT_DIR)
 
 MAIN_SRC = editorconfig.el
-SRCS = $(wildcard $(PROJECT_ROOT_DIR)/*.el)
+SRCS = $(filter-out %-autoloads.el %-pkg.el, $(wildcard 
$(PROJECT_ROOT_DIR)/*.el))
 OBJS = $(SRCS:.el=.elc)
 
 .PHONY: check-unix check-dos \
diff --git a/editorconfig.el b/editorconfig.el
index cb21cad977..2bab3e92fa 100644
--- a/editorconfig.el
+++ b/editorconfig.el
@@ -209,7 +209,7 @@ This hook will be run even when there are no matching 
sections in
     (css-ts-mode css-indent-offset)
     (d-mode c-basic-offset)
     (elixir-ts-mode elixir-ts-indent-offset)
-    (emacs-lisp-mode lisp-indent-offset)
+    (emacs-lisp-mode . editorconfig--get-indentation-lisp)
     (enh-ruby-mode enh-ruby-indent-level)
     (erlang-mode erlang-indent-level)
     (ess-mode ess-indent-offset)
@@ -258,7 +258,7 @@ This hook will be run even when there are no matching 
sections in
     (kotlin-mode kotlin-tab-width)
     (kotlin-ts-mode kotlin-ts-mode-indent-offset)
     (latex-mode . editorconfig--get-indentation-latex-mode)
-    (lisp-mode lisp-indent-offset)
+    (lisp-mode . editorconfig--get-indentation-lisp)
     (livescript-mode livescript-tab-width)
     (lua-mode lua-indent-level)
     (lua-ts-mode lua-ts-indent-offset)
@@ -460,15 +460,19 @@ Make a message by passing ARGS to `format-message'."
     (LaTeX-indent-level . ,size)
     (LaTeX-item-indent . ,(- size))))
 
-(cl-defun editorconfig--should-set (symbol &optional size)
-  "Determine if editorconfig should set SYMBOL.
+(defun editorconfig--get-indentation-lisp (size)
+  (when (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))
+    `((lisp-indent-offset . ,size))))
 
-Optional arg SIZE is used when symbol is `lisp-indent-offset'.
-See `editorconfig-lisp-use-default-indent' for details."
+(cl-defun editorconfig--should-set (symbol)
+  "Determine if editorconfig should set SYMBOL."
   (display-warning '(editorconfig editorconfig--should-set)
-                   (format "symbol: %S | size: %S"
-                           symbol
-                           size)
+                   (format "symbol: %S"
+                           symbol)
                    :debug)
   (when (and (not editorconfig-override-file-local-variables)
              (assq symbol file-local-variables-alist))
@@ -480,14 +484,6 @@ See `editorconfig-lisp-use-default-indent' for details."
     (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--get-indentation (style &optional size tab_width)
@@ -619,24 +615,37 @@ This function will revert buffer when the coding-system 
has been changed."
      `((require-final-newline . nil)
        (mode-require-final-newline . nil)))))
 
+(defun editorconfig--add-hook-safe (exp)
+  (equal exp '(add-hook 'before-save-hook
+                        #'editorconfig--delete-trailing-whitespace nil t)))
+
+(let ((predicates (get 'add-hook 'safe-local-eval-function)))
+  (when (functionp predicates)
+    (setq predicates (list predicates)))
+  (unless (memq #'editorconfig--add-hook-safe predicates)
+    (put 'add-hook 'safe-local-eval-function #'editorconfig--add-hook-safe)))
+
+(defun editorconfig--delete-trailing-whitespace ()
+  "Call `delete-trailing-whitespace' unless the buffer is read-only."
+  (unless buffer-read-only (delete-trailing-whitespace)))
+
 (defun editorconfig--get-trailing-ws (trim-trailing-ws)
   "Get vars to trim of trailing whitespace according to TRIM-TRAILING-WS."
   `(,@(when (and (equal trim-trailing-ws "true")
-                 ;; FIXME: Test this in `before-save-hook'?
                  (not buffer-read-only))
         `((eval
            . ,(if editorconfig-trim-whitespaces-mode
                   `(,editorconfig-trim-whitespaces-mode 1)
-                ;; Don't use #' because this specific form is recognized
-                ;; by Emacs's `safe-local-eval-forms'.
-                '(add-hook 'before-save-hook 'delete-trailing-whitespace nil 
t)))))
+                '(add-hook 'before-save-hook
+                           #'editorconfig--delete-trailing-whitespace nil 
t)))))
     ,@(when (or (equal trim-trailing-ws "false")
                 buffer-read-only)
         ;; Just do it right away rather than return a (VAR . VAL), which
         ;; would be probably more trouble than it's worth.
         (when editorconfig-trim-whitespaces-mode
           (funcall editorconfig-trim-whitespaces-mode 0))
-        (remove-hook 'before-save-hook #'delete-trailing-whitespace t)
+        (remove-hook 'before-save-hook
+                     #'editorconfig--delete-trailing-whitespace t)
         nil)))
 
 (defun editorconfig--get-line-length (length)
@@ -895,6 +904,9 @@ Meant to be used on 
`hack-dir-local-get-variables-functions'."
              (not (editorconfig--disabled-for-filename buffer-file-name))
              (not (editorconfig--disabled-for-majormode major-mode)))
     (let* ((props (editorconfig-call-get-properties-function buffer-file-name))
+           (_ (with-demoted-errors "editorconfig-hack-properties-functions: %S"
+                (run-hook-with-args
+                 'editorconfig-hack-properties-functions props)))
            (alist (editorconfig--get-local-variables props)))
       ;; FIXME: Actually, we should loop over the "editorconfig-core-handles"
       ;; since each one comes from a different directory.
diff --git a/ert-tests/editorconfig.el b/ert-tests/editorconfig.el
index 4fcfe13f4e..8fd5241083 100644
--- a/ert-tests/editorconfig.el
+++ b/ert-tests/editorconfig.el
@@ -102,12 +102,17 @@
 (ert-deftest test-trim-trailing-ws nil
   (editorconfig-mode 1)
   (with-visit-file (concat editorconfig-ert-dir "trim.txt")
-    (should (memq 'delete-trailing-whitespace
-                  write-file-functions)))
+    (should (memq 'editorconfig--delete-trailing-whitespace
+             before-save-hook)))
+  ;; With the new hook, settings aren't re-applied when calling
+  ;; `read-only-mode', so instead the hook function only trims
+  ;; whitespace when the buffer is not read-only.
+  ;; FIXME: Functions use in `editorconfig-trim-whitespaces-mode'
+  ;; may need to be adjusted accordingly.
   (with-visit-file (concat editorconfig-ert-dir "trim.txt")
     (read-only-mode 1)
-    (should (not (memq 'delete-trailing-whitespace
-                       write-file-functions))))
+    (should (not (memq 'editorconfig--delete-trailing-whitespace
+                       before-save-hook))))
   (editorconfig-mode -1))
 
 (ert-deftest test-charset nil
@@ -118,6 +123,14 @@
                 'iso-latin-1-unix)))
   (with-visit-file (concat editorconfig-ert-dir "utf-16be.txt")
     (set-buffer-file-coding-system 'undecided-unix)
+    ;; FIXME: It seems this test uses a non-existing file, and it seems that
+    ;; when using `auto-coding-functions', the coding system is not set in
+    ;; `buffer-file-coding-system' is not set in that case, instead
+    ;; `auto-coding-functions' will be (re)consulted when writing the file.
+    ;; So I think this test needs to be updated.
+    ;; FIXME: Maybe it could also be considered as an Emacs bug that
+    ;; `buffer-file-coding-system' is not set according to `auto-coding-*'
+    ;; when visiting a missing file, but it's not specific to EditorConfig.
     (should (eq buffer-file-coding-system
                 'utf-16be-with-signature-unix)))
   (editorconfig-mode -1))
@@ -125,6 +138,8 @@
 
 (ert-deftest test-local-variables nil
   (editorconfig-mode 1)
+  ;; FIXME: This test fails with the new Emacs-30 hook because we can't
+  ;; really obey `editorconfig-override-file/dir-local-variables' in that case.
   (with-visit-file (concat editorconfig-local-variables-ert-dir 
"file_locals.rb")
     (should (eq tab-width 9))
     (should (eq ruby-indent-level 7)))

Reply via email to