Skip to content

Commit 6783cb2

Browse files
monnierxuhdev
andauthored
(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 <hong@topbug.net>
1 parent 70fc653 commit 6783cb2

2 files changed

Lines changed: 51 additions & 48 deletions

File tree

bin/editorconfig-el

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,52 @@ with required output."
9898
version))
9999
))
100100

101+
(defun editorconfig-core-get-properties (&optional file confname confversion)
102+
"Get EditorConfig properties for FILE.
103+
If FILE is not given, use currently visiting file.
104+
Give CONFNAME for basename of config file other than .editorconfig.
105+
If need to specify config format version, give CONFVERSION.
106+
107+
This function returns an alist of properties. Each element will
108+
look like (KEY . VALUE)."
109+
(let* ((props (editorconfig-core-get-properties-hash file confname))
110+
(result nil))
111+
112+
;; Preserve the ugly and unnecessary part of the spec which
113+
;; special-cases `tab_width' and `indent_size'.
114+
115+
(setq confversion (or confversion "0.12.0"))
116+
;; Add indent_size property
117+
(let ((v-indent-size (gethash 'indent_size props))
118+
(v-indent-style (gethash 'indent_style props)))
119+
(when (and (not v-indent-size)
120+
(string= v-indent-style "tab")
121+
;; If VERSION < 0.9.0, indent_size should have no default value
122+
(version<= "0.9.0"
123+
confversion))
124+
(puthash 'indent_size
125+
"tab"
126+
props)))
127+
;; Add tab_width property
128+
(let ((v-indent-size (gethash 'indent_size props))
129+
(v-tab-width (gethash 'tab_width props)))
130+
(when (and v-indent-size
131+
(not v-tab-width)
132+
(not (string= v-indent-size "tab")))
133+
(puthash 'tab_width v-indent-size props)))
134+
;; Update indent-size property
135+
(let ((v-indent-size (gethash 'indent_size props))
136+
(v-tab-width (gethash 'tab_width props)))
137+
(when (and v-indent-size
138+
v-tab-width
139+
(string= v-indent-size "tab"))
140+
(puthash 'indent_size v-tab-width props)))
141+
142+
(maphash (lambda (key value)
143+
(push (cons (symbol-name key) value) result))
144+
props)
145+
(sort result (lambda (x y) (string< (car x) (car y))))))
146+
101147
(defun main (argv)
102148
;; TODO: Read file list from stdin if - is given as FILENAME
103149
(let ((parsed (editorconfig-bin-parse-args argv)))

editorconfig-core.el

Lines changed: 5 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
;; If you always want to use this library, add following lines to your init.el:
3939

4040
;; (setq editorconfig-get-properties-function
41-
;; 'editorconfig-core-get-properties-hash)
41+
;; #'editorconfig-core-get-properties-hash)
4242

4343

4444
;; Functions
@@ -55,7 +55,7 @@
5555
;; (KEY . VALUE) .
5656

5757

58-
;; editorconfig-core-get-properties-hash (&optional file confname confversion)
58+
;; editorconfig-core-get-properties-hash (&optional file confname)
5959

6060
;; Get EditorConfig properties for FILE.
6161

@@ -85,7 +85,7 @@ RESULT is used internally and normally should not be used."
8585
(parent (file-name-directory (directory-file-name dir))))
8686
(if (or (string= parent dir)
8787
(and handle (editorconfig-core-handle-root-p handle)))
88-
(cl-remove-if-not 'identity (cons handle result))
88+
(cl-remove-if-not #'identity (cons handle result))
8989
(editorconfig-core--get-handles parent
9090
confname
9191
(cons handle result)))))
@@ -98,22 +98,6 @@ RESULT is used internally and normally should not be used."
9898
".editorconfig")))))
9999
(editorconfig-core-handle-path handle)))
100100

101-
;;;###autoload
102-
(defun editorconfig-core-get-properties (&optional file confname confversion)
103-
"Get EditorConfig properties for FILE.
104-
If FILE is not given, use currently visiting file.
105-
Give CONFNAME for basename of config file other than .editorconfig.
106-
If need to specify config format version, give CONFVERSION.
107-
108-
This function returns an alist of properties. Each element will
109-
look like (KEY . VALUE)."
110-
(let ((hash (editorconfig-core-get-properties-hash file confname confversion))
111-
(result nil))
112-
(maphash (lambda (key value)
113-
(add-to-list 'result (cons (symbol-name key) value)))
114-
hash)
115-
(sort result (lambda (x y) (string< (car x) (car y))))))
116-
117101
(defun editorconfig-core--hash-merge (into update)
118102
"Merge two hashes INTO and UPDATE.
119103
@@ -122,8 +106,7 @@ When the same key exists in both two hashes, values of UPDATE takes precedence."
122106
(maphash (lambda (key value) (puthash key value into)) update)
123107
into)
124108

125-
;;;###autoload
126-
(defun editorconfig-core-get-properties-hash (&optional file confname confversion)
109+
(defun editorconfig-core-get-properties-hash (&optional file confname)
127110
"Get EditorConfig properties for FILE.
128111
If FILE is not given, use currently visiting file.
129112
Give CONFNAME for basename of config file other than .editorconfig.
@@ -136,7 +119,6 @@ hash object instead."
136119
buffer-file-name
137120
(error "FILE is not given and `buffer-file-name' is nil"))))
138121
(setq confname (or confname ".editorconfig"))
139-
(setq confversion (or confversion "0.12.0"))
140122
(let ((result (make-hash-table)))
141123
(dolist (handle (editorconfig-core--get-handles (file-name-directory file)
142124
confname))
@@ -145,37 +127,12 @@ hash object instead."
145127
file)))
146128

147129
;; Downcase known boolean values
130+
;; FIXME: Why not do that in `editorconfig-core-handle--parse-file'?
148131
(dolist (key '( end_of_line indent_style indent_size insert_final_newline
149132
trim_trailing_whitespace charset))
150133
(when-let* ((val (gethash key result)))
151134
(puthash key (downcase val) result)))
152135

153-
;; Add indent_size property
154-
(let ((v-indent-size (gethash 'indent_size result))
155-
(v-indent-style (gethash 'indent_style result)))
156-
(when (and (not v-indent-size)
157-
(string= v-indent-style "tab")
158-
;; If VERSION < 0.9.0, indent_size should have no default value
159-
(version<= "0.9.0"
160-
confversion))
161-
(puthash 'indent_size
162-
"tab"
163-
result)))
164-
;; Add tab_width property
165-
(let ((v-indent-size (gethash 'indent_size result))
166-
(v-tab-width (gethash 'tab_width result)))
167-
(when (and v-indent-size
168-
(not v-tab-width)
169-
(not (string= v-indent-size "tab")))
170-
(puthash 'tab_width v-indent-size result)))
171-
;; Update indent-size property
172-
(let ((v-indent-size (gethash 'indent_size result))
173-
(v-tab-width (gethash 'tab_width result)))
174-
(when (and v-indent-size
175-
v-tab-width
176-
(string= v-indent-size "tab"))
177-
(puthash 'indent_size v-tab-width result)))
178-
179136
result))
180137

181138
(provide 'editorconfig-core)

0 commit comments

Comments
 (0)