branch: elpa/editorconfig
commit 6783cb2a9fb82dc152e633e2e40162c150a4e818
Author: monnier <[email protected]>
Commit: GitHub <[email protected]>
(editorconfig-core-get-properties-hash): Simplify (#381)
* editorconfig-core.el (editorconfig-core-get-properties-hash):
Remove code that sets tab_width from indent_size and vice-versa,
since that's better done (and is done) later in `editorconfig.el`.
Also remove autoload cookie since this file is already
`require`d from the caller anyway.
Remove`confversion` arg, not used.
(editorconfig-core-get-properties): Move to `editorconfig-el`.
* bin/editorconfig-el (editorconfig-core-get-properties):
New function taken from `editorconfig-core.el`.
Co-authored-by: Hong Xu <[email protected]>
---
bin/editorconfig-el | 46 +++++++++++++++++++++++++++++++++++++++++++++
editorconfig-core.el | 53 +++++-----------------------------------------------
2 files changed, 51 insertions(+), 48 deletions(-)
diff --git a/bin/editorconfig-el b/bin/editorconfig-el
index 19335032714..f4c3d7277b7 100755
--- a/bin/editorconfig-el
+++ b/bin/editorconfig-el
@@ -98,6 +98,52 @@ with required output."
version))
))
+(defun editorconfig-core-get-properties (&optional file confname confversion)
+ "Get EditorConfig properties for FILE.
+If FILE is not given, use currently visiting file.
+Give CONFNAME for basename of config file other than .editorconfig.
+If need to specify config format version, give CONFVERSION.
+
+This function returns an alist of properties. Each element will
+look like (KEY . VALUE)."
+ (let* ((props (editorconfig-core-get-properties-hash file confname))
+ (result nil))
+
+ ;; Preserve the ugly and unnecessary part of the spec which
+ ;; special-cases `tab_width' and `indent_size'.
+
+ (setq confversion (or confversion "0.12.0"))
+ ;; Add indent_size property
+ (let ((v-indent-size (gethash 'indent_size props))
+ (v-indent-style (gethash 'indent_style props)))
+ (when (and (not v-indent-size)
+ (string= v-indent-style "tab")
+ ;; If VERSION < 0.9.0, indent_size should have no default
value
+ (version<= "0.9.0"
+ confversion))
+ (puthash 'indent_size
+ "tab"
+ props)))
+ ;; Add tab_width property
+ (let ((v-indent-size (gethash 'indent_size props))
+ (v-tab-width (gethash 'tab_width props)))
+ (when (and v-indent-size
+ (not v-tab-width)
+ (not (string= v-indent-size "tab")))
+ (puthash 'tab_width v-indent-size props)))
+ ;; Update indent-size property
+ (let ((v-indent-size (gethash 'indent_size props))
+ (v-tab-width (gethash 'tab_width props)))
+ (when (and v-indent-size
+ v-tab-width
+ (string= v-indent-size "tab"))
+ (puthash 'indent_size v-tab-width props)))
+
+ (maphash (lambda (key value)
+ (push (cons (symbol-name key) value) result))
+ props)
+ (sort result (lambda (x y) (string< (car x) (car y))))))
+
(defun main (argv)
;; TODO: Read file list from stdin if - is given as FILENAME
(let ((parsed (editorconfig-bin-parse-args argv)))
diff --git a/editorconfig-core.el b/editorconfig-core.el
index 905084d0a5e..a007d6e26bb 100644
--- a/editorconfig-core.el
+++ b/editorconfig-core.el
@@ -38,7 +38,7 @@
;; If you always want to use this library, add following lines to your init.el:
;; (setq editorconfig-get-properties-function
-;; 'editorconfig-core-get-properties-hash)
+;; #'editorconfig-core-get-properties-hash)
;; Functions
@@ -55,7 +55,7 @@
;; (KEY . VALUE) .
-;; editorconfig-core-get-properties-hash (&optional file confname confversion)
+;; editorconfig-core-get-properties-hash (&optional file confname)
;; Get EditorConfig properties for FILE.
@@ -85,7 +85,7 @@ RESULT is used internally and normally should not be used."
(parent (file-name-directory (directory-file-name dir))))
(if (or (string= parent dir)
(and handle (editorconfig-core-handle-root-p handle)))
- (cl-remove-if-not 'identity (cons handle result))
+ (cl-remove-if-not #'identity (cons handle result))
(editorconfig-core--get-handles parent
confname
(cons handle result)))))
@@ -98,22 +98,6 @@ RESULT is used internally and normally should not be used."
".editorconfig")))))
(editorconfig-core-handle-path handle)))
-;;;###autoload
-(defun editorconfig-core-get-properties (&optional file confname confversion)
- "Get EditorConfig properties for FILE.
-If FILE is not given, use currently visiting file.
-Give CONFNAME for basename of config file other than .editorconfig.
-If need to specify config format version, give CONFVERSION.
-
-This function returns an alist of properties. Each element will
-look like (KEY . VALUE)."
- (let ((hash (editorconfig-core-get-properties-hash file confname
confversion))
- (result nil))
- (maphash (lambda (key value)
- (add-to-list 'result (cons (symbol-name key) value)))
- hash)
- (sort result (lambda (x y) (string< (car x) (car y))))))
-
(defun editorconfig-core--hash-merge (into update)
"Merge two hashes INTO and UPDATE.
@@ -122,8 +106,7 @@ When the same key exists in both two hashes, values of
UPDATE takes precedence."
(maphash (lambda (key value) (puthash key value into)) update)
into)
-;;;###autoload
-(defun editorconfig-core-get-properties-hash (&optional file confname
confversion)
+(defun editorconfig-core-get-properties-hash (&optional file confname)
"Get EditorConfig properties for FILE.
If FILE is not given, use currently visiting file.
Give CONFNAME for basename of config file other than .editorconfig.
@@ -136,7 +119,6 @@ hash object instead."
buffer-file-name
(error "FILE is not given and `buffer-file-name'
is nil"))))
(setq confname (or confname ".editorconfig"))
- (setq confversion (or confversion "0.12.0"))
(let ((result (make-hash-table)))
(dolist (handle (editorconfig-core--get-handles (file-name-directory file)
confname))
@@ -145,37 +127,12 @@ hash object instead."
file)))
;; Downcase known boolean values
+ ;; FIXME: Why not do that in `editorconfig-core-handle--parse-file'?
(dolist (key '( end_of_line indent_style indent_size insert_final_newline
trim_trailing_whitespace charset))
(when-let* ((val (gethash key result)))
(puthash key (downcase val) result)))
- ;; Add indent_size property
- (let ((v-indent-size (gethash 'indent_size result))
- (v-indent-style (gethash 'indent_style result)))
- (when (and (not v-indent-size)
- (string= v-indent-style "tab")
- ;; If VERSION < 0.9.0, indent_size should have no default
value
- (version<= "0.9.0"
- confversion))
- (puthash 'indent_size
- "tab"
- result)))
- ;; Add tab_width property
- (let ((v-indent-size (gethash 'indent_size result))
- (v-tab-width (gethash 'tab_width result)))
- (when (and v-indent-size
- (not v-tab-width)
- (not (string= v-indent-size "tab")))
- (puthash 'tab_width v-indent-size result)))
- ;; Update indent-size property
- (let ((v-indent-size (gethash 'indent_size result))
- (v-tab-width (gethash 'tab_width result)))
- (when (and v-indent-size
- v-tab-width
- (string= v-indent-size "tab"))
- (puthash 'indent_size v-tab-width result)))
-
result))
(provide 'editorconfig-core)