branch: scratch/editorconfig-wip commit c4bb7da5d1f726212a2b871824d81448e92b0ec6 Author: Stefan Monnier <monn...@iro.umontreal.ca> Commit: Stefan Monnier <monn...@iro.umontreal.ca>
WiP --- editorconfig-core-handle.el | 34 ++++++++++++++++------------------ 1 file changed, 16 insertions(+), 18 deletions(-) diff --git a/editorconfig-core-handle.el b/editorconfig-core-handle.el index ac8c0889af..071329c006 100644 --- a/editorconfig-core-handle.el +++ b/editorconfig-core-handle.el @@ -51,13 +51,14 @@ Slots: (name nil) (props nil)) -(defun editorconfig-core-handle-section-get-properties (section file dir) +(defun editorconfig-core-handle-section-get-properties (section file) "Return properties alist when SECTION name match FILE. -DIR should be the directory where .editorconfig file which has SECTION lives. +FILE should be a relative file name, relative to the directory where +.editorconfig file which has SECTION lives. IF not match, return nil." (when (editorconfig-core-handle--fnmatch-p - file (editorconfig-core-handle-section-name section) dir) + file (editorconfig-core-handle-section-name section)) (editorconfig-core-handle-section-props section))) (cl-defstruct editorconfig-core-handle @@ -115,14 +116,12 @@ If HANDLE is nil return nil." The list returned will be ordered by the lines they appear. If HANDLE is nil return nil." - (declare (obsolete editorconfig-core-handle-get-properties-hash "0.8.0"))) - (when handle - (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))))) + (declare (obsolete editorconfig-core-handle-get-properties-hash "0.8.0")) + (let ((hash (editorconfig-core-handle-get-properties-hash handle file)) + alist) + (when hash + (maphash (lambda (k v) (push (cons k v) alist)) hash) + alist))) (defun editorconfig-core-handle-get-properties-hash (handle file) "Return hash of properties from HANDLE for FILE. @@ -130,23 +129,22 @@ If HANDLE is nil return nil." If HANDLE is nil return nil." (when handle (let ((hash (make-hash-table)) - (dir (file-name-directory (editorconfig-core-handle-path + (file (file-relative-name (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) + (cl-loop for (key . value) in (editorconfig-core-handle-section-get-properties section file) do (puthash (intern key) value hash))) hash))) -(defun editorconfig-core-handle--fnmatch-p (name pattern dir) +(defun editorconfig-core-handle--fnmatch-p (name pattern) "Return non-nil if NAME match PATTERN. If pattern has slash, pattern should be relative to DIR. This function is a fnmatch with a few modification for EditorConfig usage." (if (string-match-p "/" pattern) - (let ((pattern (replace-regexp-in-string "^/" "" pattern)) - (dir (file-name-as-directory dir))) - (editorconfig-fnmatch-p name (concat dir pattern))) - (editorconfig-fnmatch-p name (concat "**/" pattern)))) + (let ((pattern (replace-regexp-in-string "\\`/" "" pattern))) + (editorconfig-fnmatch-p name pattern)) + (editorconfig-fnmatch-p (file-name-nondirectory name) pattern))) (defsubst editorconfig-core-handle--string-trim (str) "Remove leading and trailing whitespaces from STR."