From 9545682ef936e2974a6bf5367963dea03ea6c8dd Mon Sep 17 00:00:00 2001 From: 10sr <8.slashes@gmail.com> Date: Thu, 10 Jan 2019 13:48:52 +0900 Subject: [PATCH 1/5] 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 e14eaf21..e36fb696 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) From 103081756d5d1de03dfa2d9d42b6e4a8581fca29 Mon Sep 17 00:00:00 2001 From: 10sr <8.slashes@gmail.com> Date: Thu, 10 Jan 2019 14:13:21 +0900 Subject: [PATCH 2/5] Add support for Emacs24.5 --- .travis.yml | 1 + editorconfig-core-handle.el | 2 ++ editorconfig.el | 2 +- 3 files changed, 4 insertions(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 87aefe8e..9923bb48 100644 --- a/.travis.yml +++ b/.travis.yml @@ -16,6 +16,7 @@ before_install: - ./.evm/bin/evm config path /tmp - ./.evm/bin/evm install emacs-$EMACS_VERSION-travis --use - ./.evm/bin/emacs --version + - if [[ "$EMACS_VERSION" == 24.5 ]]; then curl "http://git.savannah.gnu.org/cgit/emacs/elpa.git/plain/packages/cl-generic/cl-generic.el?id=a1cdea05e8cbfe15ba075c64417db20b814e48e8" >cl-generic.el; fi script: make test EMACS=$PWD/.evm/bin/emacs diff --git a/editorconfig-core-handle.el b/editorconfig-core-handle.el index e36fb696..85ccf01c 100644 --- a/editorconfig-core-handle.el +++ b/editorconfig-core-handle.el @@ -32,6 +32,8 @@ ;;; Code: (require 'cl-lib) +;; Require explicit load for Emacs<25 +(require 'cl-generic) (require 'editorconfig-fnmatch) diff --git a/editorconfig.el b/editorconfig.el index dfe21f65..767c0845 100644 --- a/editorconfig.el +++ b/editorconfig.el @@ -5,7 +5,7 @@ ;; Author: EditorConfig Team ;; Version: 0.7.14 ;; URL: https://github.com/editorconfig/editorconfig-emacs#readme -;; Package-Requires: ((cl-lib "0.5")) +;; Package-Requires: ((cl-lib "0.5") (cl-generic "0.3")) ;; See ;; https://github.com/editorconfig/editorconfig-emacs/graphs/contributors From a7dd33cc6a9ef326df0b2e995213478981f43b86 Mon Sep 17 00:00:00 2001 From: 10sr <8.slashes@gmail.com> Date: Thu, 10 Jan 2019 14:30:55 +0900 Subject: [PATCH 3/5] Revert "Add support for Emacs24.5" This reverts commit 103081756d5d1de03dfa2d9d42b6e4a8581fca29. --- .travis.yml | 1 - editorconfig-core-handle.el | 2 -- editorconfig.el | 2 +- 3 files changed, 1 insertion(+), 4 deletions(-) diff --git a/.travis.yml b/.travis.yml index 9923bb48..87aefe8e 100644 --- a/.travis.yml +++ b/.travis.yml @@ -16,7 +16,6 @@ before_install: - ./.evm/bin/evm config path /tmp - ./.evm/bin/evm install emacs-$EMACS_VERSION-travis --use - ./.evm/bin/emacs --version - - if [[ "$EMACS_VERSION" == 24.5 ]]; then curl "http://git.savannah.gnu.org/cgit/emacs/elpa.git/plain/packages/cl-generic/cl-generic.el?id=a1cdea05e8cbfe15ba075c64417db20b814e48e8" >cl-generic.el; fi script: make test EMACS=$PWD/.evm/bin/emacs diff --git a/editorconfig-core-handle.el b/editorconfig-core-handle.el index 85ccf01c..e36fb696 100644 --- a/editorconfig-core-handle.el +++ b/editorconfig-core-handle.el @@ -32,8 +32,6 @@ ;;; Code: (require 'cl-lib) -;; Require explicit load for Emacs<25 -(require 'cl-generic) (require 'editorconfig-fnmatch) diff --git a/editorconfig.el b/editorconfig.el index 767c0845..dfe21f65 100644 --- a/editorconfig.el +++ b/editorconfig.el @@ -5,7 +5,7 @@ ;; Author: EditorConfig Team ;; Version: 0.7.14 ;; URL: https://github.com/editorconfig/editorconfig-emacs#readme -;; Package-Requires: ((cl-lib "0.5") (cl-generic "0.3")) +;; Package-Requires: ((cl-lib "0.5")) ;; See ;; https://github.com/editorconfig/editorconfig-emacs/graphs/contributors From cae41862e8608a61060f0193a8765e36802b7b2e Mon Sep 17 00:00:00 2001 From: 10sr <8.slashes@gmail.com> Date: Thu, 10 Jan 2019 14:31:53 +0900 Subject: [PATCH 4/5] Stop using cl-defclass With Emacs<25 cl-defmethod cannot used for structs --- editorconfig-core-handle.el | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/editorconfig-core-handle.el b/editorconfig-core-handle.el index e36fb696..0156a947 100644 --- a/editorconfig-core-handle.el +++ b/editorconfig-core-handle.el @@ -46,8 +46,7 @@ ;; (KEY . VALUE) (props nil)) -(cl-defmethod editorconfig-core-handle-section-get-properties - ((section editorconfig-core-handle-section) file dir) +(defun editorconfig-core-handle-section-get-properties (section file dir) "Return properties alist when SECTION name matches FILE. DIR should be where the directory where .editorconfig which has SECTION exists. @@ -94,7 +93,7 @@ If CONF does not exist return nil." :path conf) editorconfig-core-handle--cache-hash)))))) -(cl-defmethod editorconfig-core-handle-root-p ((handle editorconfig-core-handle)) +(defun editorconfig-core-handle-root-p (handle) "Return non-nil if HANDLE represent root EditorConfig file. If HANDLE is nil return nil." @@ -104,7 +103,7 @@ If HANDLE is nil return nil." (editorconfig-core-handle-top-props handle))) ""))))) -(cl-defmethod editorconfig-core-handle-get-properties ((handle editorconfig-core-handle) file) +(defun editorconfig-core-handle-get-properties (handle file) "Return list of alist of properties from HANDLE for FILE. The list returned will be ordered by the lines they appear. @@ -121,7 +120,7 @@ If HANDLE is nil return nil." "0.8.0") -(cl-defmethod editorconfig-core-handle-get-properties-hash ((handle editorconfig-core-handle) file) +(defun editorconfig-core-handle-get-properties-hash (handle file) "Return hash of properties from HANDLE for FILE. If HANDLE is nil return nil." From baad90b852c67009c7b242411fe3e570f3f09803 Mon Sep 17 00:00:00 2001 From: 10sr <8.slashes@gmail.com> Date: Thu, 10 Jan 2019 14:40:30 +0900 Subject: [PATCH 5/5] Remove useless spaces --- editorconfig-core-handle.el | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/editorconfig-core-handle.el b/editorconfig-core-handle.el index 0156a947..f6120689 100644 --- a/editorconfig-core-handle.el +++ b/editorconfig-core-handle.el @@ -115,12 +115,12 @@ If HANDLE is nil return nil." file dir) when props collect (copy-alist props))))) - (make-obsolete 'editorconfig-core-handle-get-properties - 'editorconfig-core-handle-get-properties-hash - "0.8.0") +(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) +(defun editorconfig-core-handle-get-properties-hash (handle file) "Return hash of properties from HANDLE for FILE. If HANDLE is nil return nil."