Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions bin/editorconfig-el
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
Expand Down
53 changes: 5 additions & 48 deletions editorconfig-core.el
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.

Expand Down Expand Up @@ -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)))))
Expand All @@ -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.

Expand All @@ -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.
Expand All @@ -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))
Expand All @@ -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)
Expand Down