[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[nongnu] scratch/editorconfig-cc f2d4539bae 228/351: Refactor handle lib
From: |
Stefan Monnier |
Subject: |
[nongnu] scratch/editorconfig-cc f2d4539bae 228/351: Refactor handle lib |
Date: |
Thu, 13 Jun 2024 18:38:56 -0400 (EDT) |
branch: scratch/editorconfig-cc
commit f2d4539bae2f439545be2eaff43af62f522e0fb6
Author: 10sr <8.slashes@gmail.com>
Commit: Stefan Monnier <monnier@iro.umontreal.ca>
Refactor handle lib
---
editorconfig-core-handle.el | 104 ++++++++++++++++++++++++--------------------
1 file changed, 58 insertions(+), 46 deletions(-)
diff --git a/editorconfig-core-handle.el b/editorconfig-core-handle.el
index e14eaf213b..e36fb69683 100644
--- a/editorconfig-core-handle.el
+++ b/editorconfig-core-handle.el
@@ -35,28 +35,36 @@
(require 'editorconfig-fnmatch)
-;; For cl-defstruct
-(require 'cl-lib)
-
(defvar editorconfig-core-handle--cache-hash
(make-hash-table :test 'equal)
"Hash of EditorConfig filename and its `editorconfig-core-handle' instance.")
+(cl-defstruct editorconfig-core-handle-section
+ ;; String of section name (glob string)
+ (name nil)
+ ;; Alist of properties
+ ;; (KEY . VALUE)
+ (props nil))
+
+(cl-defmethod editorconfig-core-handle-section-get-properties
+ ((section editorconfig-core-handle-section) file dir)
+ "Return properties alist when SECTION name matches FILE.
+
+DIR should be where the directory where .editorconfig which has SECTION exists.
+IF not match, return nil."
+ (when (editorconfig-core-handle--fnmatch-p
+ file
+ (editorconfig-core-handle-section-name section)
+ dir)
+ (editorconfig-core-handle-section-props section)))
+
(cl-defstruct editorconfig-core-handle
;; Alist of top propetties
;; e.g. (("root" . "true"))
- (top-prop nil)
+ (top-props nil)
- ;; TODO: Define struct for section
- ;; Alist of properties
- ;; Key: Section name
- ;; Value: Alist of properties for each section name
- ;; e.g.
- ;; (
- ;; ("*" ("end_of_line" . "lf") ("indent_style" . "space"))
- ;; ("Makefile" ("indent_style" . "tab"))
- ;; )
- (prop nil)
+ ;; List of editorconfig-core-handle-section
+ (sections nil)
;; e.g. (22310 59113 203882 991000)
(mtime nil)
@@ -80,52 +88,50 @@ If CONF does not exist return nil."
cached
(let ((parsed (editorconfig-core-handle--parse-file conf)))
(puthash conf
- (make-editorconfig-core-handle :top-prop (car parsed)
- :prop (cdr parsed)
+ (make-editorconfig-core-handle :top-props (plist-get parsed
:top-props)
+ :sections (plist-get parsed
:sections)
:mtime mtime
:path conf)
editorconfig-core-handle--cache-hash))))))
-(defun editorconfig-core-handle-root-p (handle)
+(cl-defmethod editorconfig-core-handle-root-p ((handle
editorconfig-core-handle))
"Return non-nil if HANDLE represent root EditorConfig file.
If HANDLE is nil return nil."
(when handle
(string-equal "true"
(downcase (or (cdr (assoc "root"
- (editorconfig-core-handle-top-prop
handle)))
+
(editorconfig-core-handle-top-props handle)))
"")))))
-(defun editorconfig-core-handle-get-properties (handle file)
+(cl-defmethod editorconfig-core-handle-get-properties ((handle
editorconfig-core-handle) file)
"Return list of alist of properties from HANDLE for FILE.
The list returned will be ordered by the lines they appear.
If HANDLE is nil return nil."
(when handle
- (mapcar (lambda (prop) (copy-alist (cdr prop)))
- (cl-remove-if-not (lambda (prop)
- (editorconfig-core-handle--fnmatch-p file
- (car prop)
-
(file-name-directory (editorconfig-core-handle-path handle))))
- (editorconfig-core-handle-prop handle)))))
-(make-obsolete 'editorconfig-core-handle-get-properties
- 'editorconfig-core-handle-get-properties-hash
- "0.8.0")
-
-
-(defun editorconfig-core-handle-get-properties-hash (handle file)
+ (let ((dir (file-name-directory (editorconfig-core-handle-path handle))))
+ (cl-loop for section in (editorconfig-core-handle-sections handle)
+ for props = (editorconfig-core-handle-section-get-properties
section
+
file
+
dir)
+ when props collect (copy-alist props)))))
+ (make-obsolete 'editorconfig-core-handle-get-properties
+ 'editorconfig-core-handle-get-properties-hash
+ "0.8.0")
+
+
+(cl-defmethod editorconfig-core-handle-get-properties-hash ((handle
editorconfig-core-handle) file)
"Return hash of properties from HANDLE for FILE.
If HANDLE is nil return nil."
(when handle
- (let ((hash (make-hash-table)))
- (dolist (prop (editorconfig-core-handle-prop handle))
- (when (editorconfig-core-handle--fnmatch-p file
- (car prop)
- (file-name-directory
(editorconfig-core-handle-path
-
handle)))
- (cl-loop for (key . value) in (cdr prop)
- do (puthash (intern key) value hash))))
+ (let ((hash (make-hash-table))
+ (dir (file-name-directory (editorconfig-core-handle-path
+ handle))))
+ (dolist (section (editorconfig-core-handle-sections handle))
+ (cl-loop for (key . value) in
(editorconfig-core-handle-section-get-properties section file dir)
+ do (puthash (intern key) value hash)))
hash)))
(defun editorconfig-core-handle--fnmatch-p (name pattern dir)
@@ -168,7 +174,7 @@ If CONF is not found return nil."
(insert-file-contents conf)
(goto-char (point-min))
(let ((point-max (point-max))
- (all-props ())
+ (sections ())
(top-props nil)
;; String of current line
@@ -194,11 +200,15 @@ If CONF is not found return nil."
((string-equal "" line)
nil)
+ ;; Start of section
((string-match "^\\[\\(.*\\)\\]$"
line)
(when pattern
- (setq all-props
- `(,@all-props (,pattern . ,props)))
+ (setq sections
+ `(,@sections ,(make-editorconfig-core-handle-section
+ :name pattern
+ :props props)))
+ (setq pattern nil)
(setq props nil))
(setq pattern (match-string 1 line)))
@@ -234,10 +244,12 @@ If CONF is not found return nil."
(forward-line (1- current-line-number))
)
(when pattern
- (setq all-props
- `(,@all-props (,pattern . ,props))))
- (cons top-props
- all-props)))))
+ (setq sections
+ `(,@sections ,(make-editorconfig-core-handle-section
+ :name pattern
+ :props props))))
+ (list :top-props top-props
+ :sections sections)))))
(provide 'editorconfig-core-handle)
- [nongnu] scratch/editorconfig-cc f2c8300dba 129/351: Add Usami Kenta to the contributor list., (continued)
- [nongnu] scratch/editorconfig-cc f2c8300dba 129/351: Add Usami Kenta to the contributor list., Stefan Monnier, 2024/06/13
- [nongnu] scratch/editorconfig-cc d7c6a8befd 128/351: Reidentation for all *.el files., Stefan Monnier, 2024/06/13
- [nongnu] scratch/editorconfig-cc 8a2e9be119 131/351: Fix indentations of ert-tests/, Stefan Monnier, 2024/06/13
- [nongnu] scratch/editorconfig-cc 20c8ea8cc9 174/351: Update travis config file, Stefan Monnier, 2024/06/13
- [nongnu] scratch/editorconfig-cc b5f1eab15e 169/351: Update texi, Stefan Monnier, 2024/06/13
- [nongnu] scratch/editorconfig-cc 5986c305c9 167/351: Remove .info and add .texi, Stefan Monnier, 2024/06/13
- [nongnu] scratch/editorconfig-cc 7b762043d6 176/351: Add commented allow_failure, Stefan Monnier, 2024/06/13
- [nongnu] scratch/editorconfig-cc 4185bcfac2 210/351: Call -hack-properties-functions in editorconfig-apply, Stefan Monnier, 2024/06/13
- [nongnu] scratch/editorconfig-cc 2cfbec79f9 216/351: Update core-test submodule, Stefan Monnier, 2024/06/13
- [nongnu] scratch/editorconfig-cc 2ba007601a 219/351: Add document for editorconfig-trim-whitespaces-mode, Stefan Monnier, 2024/06/13
- [nongnu] scratch/editorconfig-cc f2d4539bae 228/351: Refactor handle lib,
Stefan Monnier <=
- [nongnu] scratch/editorconfig-cc b303bfd220 211/351: Add test for -hack-properties-functions, Stefan Monnier, 2024/06/13
- [nongnu] scratch/editorconfig-cc 8688e1d3bf 230/351: Revert "Add support for Emacs24.5", Stefan Monnier, 2024/06/13
- [nongnu] scratch/editorconfig-cc 5f5a7bf03d 237/351: Update commentary section, Stefan Monnier, 2024/06/13
- [nongnu] scratch/editorconfig-cc 8cf419254d 246/351: Do not update submodule when running tests, Stefan Monnier, 2024/06/13
- [nongnu] scratch/editorconfig-cc be0973c580 291/351: Fix for coding-system set when remote files do not exist (#250), Stefan Monnier, 2024/06/13
- [nongnu] scratch/editorconfig-cc 9bfade9437 257/351: Use dist: trusty, Stefan Monnier, 2024/06/13
- [nongnu] scratch/editorconfig-cc 0e299b7528 278/351: Silence byte-compiler warnings (#235), Stefan Monnier, 2024/06/13
- [nongnu] scratch/editorconfig-cc cf012776b4 306/351: Use new implementation by default (#263), Stefan Monnier, 2024/06/13
- [nongnu] scratch/editorconfig-cc 0f2c33d11a 282/351: Update README 2 (#225), Stefan Monnier, 2024/06/13
- [nongnu] scratch/editorconfig-cc 1969dd74da 271/351: Add conf-mode abbrev-table definitions (#220), Stefan Monnier, 2024/06/13