branch: scratch/editorconfig commit 0a6a169efd1ff72b6e01b76c8f3561eed89bfcc0 Author: Stefan Monnier <monn...@iro.umontreal.ca> Commit: Stefan Monnier <monn...@iro.umontreal.ca>
(editorconfig--get-dir-local-variables): Use editorconfig--get-local-variables --- editorconfig.el | 104 +++++++++++++++++--------------------------------------- 1 file changed, 31 insertions(+), 73 deletions(-) diff --git a/editorconfig.el b/editorconfig.el index 470c7b5d5a..05c166352d 100644 --- a/editorconfig.el +++ b/editorconfig.el @@ -473,27 +473,25 @@ See `editorconfig-lisp-use-default-indent' for details." t) -(defun editorconfig-get-indentation (style &optional size tab_width) +(defun editorconfig--get-indentation (style &optional size tab_width) "Get indentation vars according to STYLE, SIZE and TAB_WIDTH." + (when tab_width + (setq tab_width (string-to-number tab_width))) + (setq size (cond ((editorconfig-string-integer-p size) (string-to-number size)) ((equal size "tab") - "tab") + (or tab_width tab-width)) (t nil))) - `(,@(cond (tab_width - `((tab-width . ,(string-to-number tab_width)))) + `(,@(cond (tab_width `((tab-width . ,tab_width))) ;; FIXME: This seems wrong: `tab-width' controls the display width ;; of TAB characters in the buffer, which is largely independent ;; from the indentation step. - ((numberp size) - `((tab-width . ,size)))) - - ,@(when (equal size "tab") - (setq size tab-width) - nil) + ;;((numberp size) `((tab-width . ,size))) + ) ,@(cond ((equal style "space") `((indent-tabs-mode . nil))) @@ -503,12 +501,20 @@ See `editorconfig-lisp-use-default-indent' for details." ,@(when (and size (featurep 'evil)) `((evil-shift-width . ,size))) ,@(when size - (let ((parent major-mode) - entry) + (let* ((parents (if (fboundp 'derived-mode-all-parents) + (derived-mode-all-parents major-mode))) + (parent (if parents (pop parents) major-mode)) + (entry ())) ;; Find the closet parent mode of `major-mode' in ;; `editorconfig-indentation-alist'. - (while (and (not (setq entry (assoc parent editorconfig-indentation-alist))) - (setq parent (get parent 'derived-mode-parent)))) + ;; FIXME: Rather than keep a central database of all modes and their + ;; indent var(s), we should have a buffer-local var and let each + ;; major mode set it to indicate how to set the indentation size. + (while (and (not (setq entry + (assoc parent editorconfig-indentation-alist))) + (setq parent (if (fboundp 'derived-mode-all-parents) + (pop parents) + (get parent 'derived-mode-parent))))) (when entry (let ((fn-or-list (cdr entry))) (cond ((functionp fn-or-list) (funcall fn-or-list size)) @@ -582,7 +588,7 @@ This function will revert buffer when the coding-system has been changed." (revert-buffer-with-coding-system coding-system))) (setq editorconfig--apply-coding-system-currently nil))))) -(defun editorconfig-get-trailing-nl (final-newline) +(defun editorconfig--get-trailing-nl (final-newline) "Get the vars to require final newline according to FINAL-NEWLINE." (pcase final-newline ("true" @@ -597,7 +603,7 @@ This function will revert buffer when the coding-system has been changed." `((require-final-newline . nil) (mode-require-final-newline . nil))))) -(defun editorconfig-get-trailing-ws (trim-trailing-ws) +(defun editorconfig--get-trailing-ws (trim-trailing-ws) "Get vars to trim of trailing whitespace according to TRIM-TRAILING-WS." `(,@(when (and (equal trim-trailing-ws "true") ;; FIXME: Test this in `before-save-hook'? @@ -615,7 +621,7 @@ This function will revert buffer when the coding-system has been changed." (remove-hook 'before-save-hook #'delete-trailing-whitespace t) nil))) -(defun editorconfig-get-line-length (length) +(defun editorconfig--get-line-length (length) "Get the max line length (`fill-column') to LENGTH." (when (and (editorconfig-string-integer-p length) (> (string-to-number length) 0)) @@ -704,18 +710,18 @@ This function also removes `unset' properties and calls when (equal v "unset") do (remhash k props)) props)) -(defun editorconfig-get-local-variables (props) +(defun editorconfig--get-local-variables (props) "Get variables settings according to EditorConfig PROPS." - (editorconfig-get-indentation (gethash 'indent_style props) + (editorconfig--get-indentation (gethash 'indent_style props) (gethash 'indent_size props) (gethash 'tab_width props)) - (editorconfig-get-trailing-nl (gethash 'insert_final_newline props)) - (editorconfig-get-trailing-ws (gethash 'trim_trailing_whitespace props)) - (editorconfig-get-line-length (gethash 'max_line_length props))) + (editorconfig--get-trailing-nl (gethash 'insert_final_newline props)) + (editorconfig--get-trailing-ws (gethash 'trim_trailing_whitespace props)) + (editorconfig--get-line-length (gethash 'max_line_length props))) (defun editorconfig-set-local-variables (props) "Set buffer variables according to EditorConfig PROPS." - (pcase-dolist (`(,var . ,val) (editorconfig-get-local-variables props)) + (pcase-dolist (`(,var . ,val) (editorconfig--get-local-variables props)) (when (editorconfig--should-set var) (set (make-local-variable var) val)))) @@ -860,61 +866,13 @@ F is that function, and FILENAME and ARGS are arguments passed to F." (editorconfig-merge-coding-systems (gethash 'end_of_line props) (gethash 'charset props))))) -(defvar editorconfig-indent-vars-function - ;; FIXME: Obey `editorconfig-indentation-alist' as best as we can? - ;; Set `smie-indent-basic' if all else fails? - ;; FIXME: Change `editorconfig-indentation-alist' so that the functions - ;; therein return an list of (VAR . VAL) instead of setting the vars directly. - #'editorconfig--get-indent-vars) - -(defun editorconfig--get-indent-vars (size) - (let ((parents (derived-mode-all-parents major-mode)) - (entry ())) - (while (and parents (not entry)) - (setq entry (assq (pop parents) editorconfig-indentation-alist))) - (if (functionp (cdr entry)) - (funcall (cdr entry)) - (mapcar (lambda (elem) - (if (consp elem) - (let ((spec (cdr elem))) - (cons (car elem) - (cond ((functionp spec) (funcall spec size)) - ((integerp spec) (* spec size)) - (t spec)))) - (cons elem size))) - (cdr entry))))) - (defun editorconfig--get-dir-local-variables () (when (and (stringp buffer-file-name) ;; FIXME: How important is it to support these `disabled-*'? (not (editorconfig--disabled-for-filename buffer-file-name)) (not (editorconfig--disabled-for-majormode major-mode))) - ;; FIXME: Cache the result of `editorconfig-call-get-properties-function'? - (let ((props (editorconfig-call-get-properties-function buffer-file-name)) - (alist ())) - (maphash - (lambda (prop setting) - (pcase prop - ('tab_width - (when setting - (push `(tab-width . ,(string-to-number setting)) alist))) - ('indent-size - (let ((size - (cond - ((editorconfig-string-integer-p setting) - (string-to-number setting)) - ((equal setting "tab") - (let ((tabsize (gethash 'tab_width props))) - (if tabsize (string-to-number tabsize) - tab-width)))))) - (when size - (setq alist (nconc (funcall editorconfig-indent-vars-function - size) - alist))))) - ;; FIXME: Add the other settings supported by - ;; `editorconfig-set-local-variables'! - )) - props) + (let* ((props (editorconfig-call-get-properties-function buffer-file-name)) + (alist (editorconfig--get-local-variables props))) ;; FIXME: Actually, we should loop over the "editorconfig-core-handles" ;; since each one comes from a different directory. (cons