Skip to content

Commit 5be6d1b

Browse files
monnierxuhdev
andauthored
Use delete-trailing-whitespace-mode when available (#382)
(editorconfig-trim-whitespaces-mode): Use `delete-trailing-whitespace-mode` when available. Improve `:type`. (editorconfig-properties-hash): Use `defvar-local`. Remove misleading statement in the docstring. * ert-tests/editorconfig-tests.el (test-trim-trailing-ws): Adjust test. * editorconfig-fnmatch.el (editorconfig-fnmatch--cache-hashtable): Use `defconst`. (editorconfig-fnmatch--do-translate): Don't backslash escape `-` and `,` in regexps (breaks new regexp extensions). Co-authored-by: Hong Xu <[email protected]>
1 parent 05b4224 commit 5be6d1b

File tree

3 files changed

+16
-21
lines changed

3 files changed

+16
-21
lines changed

editorconfig-fnmatch.el

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
;;; editorconfig-fnmatch.el --- Glob pattern matching -*- lexical-binding: t -*-
22

3-
;; Copyright (C) 2011-2024 Free Software Foundation, Inc.
3+
;; Copyright (C) 2011-2025 Free Software Foundation, Inc.
44

55
;; Author: EditorConfig Team <[email protected]>
66
;; Package: editorconfig
@@ -55,13 +55,9 @@
5555

5656
(require 'cl-lib)
5757

58-
(defvar editorconfig-fnmatch--cache-hashtable
59-
nil
58+
(defconst editorconfig-fnmatch--cache-hashtable ;; Clear cache on file reload.
59+
(make-hash-table :test 'equal)
6060
"Cache of shell pattern and its translation.")
61-
;; Clear cache on file reload
62-
(setq editorconfig-fnmatch--cache-hashtable
63-
(make-hash-table :test 'equal))
64-
6561

6662
(defconst editorconfig-fnmatch--left-brace-regexp
6763
"\\(^\\|[^\\]\\){"
@@ -147,7 +143,7 @@ translation is found for PATTERN."
147143

148144
(while (< index length)
149145
(if (and (not is-escaped)
150-
(string-match "[^]\\*?[{},/-]+"
146+
(string-match "[^]\\*?[{},/]+"
151147
;;(string-match "[^]\\*?[{},/\\-]+" "?.a")
152148
pattern
153149
index)
@@ -203,8 +199,6 @@ translation is found for PATTERN."
203199
"[^")
204200
"[")))))
205201

206-
(?- (if in-brackets "-" "\\-"))
207-
208202
(?\] (setq in-brackets nil) "]")
209203

210204
(?\{
@@ -249,7 +243,7 @@ translation is found for PATTERN."
249243
(?,
250244
(if (and (> brace-level 0)
251245
(not is-escaped))
252-
"\\|" "\\,"))
246+
"\\|" ","))
253247

254248
(?\}
255249
(if (and (> brace-level 0)

editorconfig.el

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ Used by `editorconfig--execute-editorconfig-exec'."
7575
'editorconfig-get-properties-function
7676
"0.5")
7777
(defcustom editorconfig-get-properties-function
78-
'editorconfig-core-get-properties-hash
78+
#'editorconfig-core-get-properties-hash
7979
"A function which gets EditorConfig properties for specified file.
8080
8181
This function will be called with one argument, full path of the target file,
@@ -363,18 +363,17 @@ When variable `buffer-file-name' matches any of the regexps, then
363363
(eval (file-truename (expand-file-name recentf-save-file))))
364364
t)))
365365

366-
(defcustom editorconfig-trim-whitespaces-mode nil
366+
(defcustom editorconfig-trim-whitespaces-mode
367+
(when (fboundp 'delete-trailing-whitespace-mode)
368+
#'delete-trailing-whitespace-mode)
367369
"Buffer local minor-mode to use to trim trailing whitespaces.
368370
369371
If set, enable that mode when `trim_trailing_whitespace` is set to true.
370372
Otherwise, use `delete-trailing-whitespace'."
371-
:type 'symbol)
373+
:type 'function)
372374

373-
(defvar editorconfig-properties-hash nil
374-
"Hash object of EditorConfig properties that was enabled for current buffer.
375-
Set by `editorconfig-apply' and nil if that is not invoked in
376-
current buffer yet.")
377-
(make-variable-buffer-local 'editorconfig-properties-hash)
375+
(defvar-local editorconfig-properties-hash nil
376+
"Hash object of EditorConfig properties that was enabled for current buffer.")
378377
(put 'editorconfig-properties-hash
379378
'permanent-local
380379
t)

ert-tests/editorconfig-tests.el

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,10 @@
102102
(ert-deftest test-trim-trailing-ws nil
103103
(editorconfig-mode 1)
104104
(with-visit-file (concat editorconfig-ert-dir "trim.txt")
105-
(should (memq #'editorconfig--delete-trailing-whitespace
106-
before-save-hook)))
105+
(if editorconfig-trim-whitespaces-mode
106+
(should (symbol-value editorconfig-trim-whitespaces-mode))
107+
(should (memq #'editorconfig--delete-trailing-whitespace
108+
before-save-hook))))
107109
;; FIXME: We don't hook into `read-only-mode', so instead we should
108110
;; make a more thorough test that saves the file after making the buffer
109111
;; read-only and makes sure it does not trim the trailing-ws of the

0 commit comments

Comments
 (0)