Skip to content

Commit 093d4db

Browse files
authored
Add "clang-format-on-save-mode" minor mode to clang-format.el (#104533)
Add an minor mode which can be optionally used to run clang-format on save. Formatting before saving works well and is convenient to avoid having to remember to manually run clang format. I've written this as it's own package but it's probably better if the functionality is supported by clang-format.el. See: melpa/melpa#8762
1 parent 93ec08d commit 093d4db

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

clang/tools/clang-format/clang-format.el

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,20 @@ in such buffers."
7070
:safe #'stringp)
7171
(make-variable-buffer-local 'clang-format-fallback-style)
7272

73+
(defcustom clang-format-on-save-p 'clang-format-on-save-check-config-exists
74+
"Only reformat on save if this function returns non-nil.
75+
76+
You may wish to choose one of the following options:
77+
- `always': To always format on save.
78+
- `clang-format-on-save-check-config-exists':
79+
Only reformat when \".clang-format\" exists.
80+
81+
Otherwise you can set this to a user defined function."
82+
:group 'clang-format
83+
:type 'function
84+
:risky t)
85+
(make-variable-buffer-local 'clang-format-on-save-p)
86+
7387
(defun clang-format--extract (xml-node)
7488
"Extract replacements and cursor information from XML-NODE."
7589
(unless (and (listp xml-node) (eq (xml-node-name xml-node) 'replacements))
@@ -217,5 +231,48 @@ the function `buffer-file-name'."
217231
;;;###autoload
218232
(defalias 'clang-format 'clang-format-region)
219233

234+
;; Format on save minor mode.
235+
236+
(defun clang-format--on-save-buffer-hook ()
237+
"The hook to run on buffer saving to format the buffer."
238+
;; Demote errors as this is user configurable, we can't be sure it wont error.
239+
(when (with-demoted-errors "clang-format: Error %S"
240+
(funcall clang-format-on-save-p))
241+
(clang-format-buffer))
242+
;; Continue to save.
243+
nil)
244+
245+
(defun clang-format--on-save-enable ()
246+
"Disable the minor mode."
247+
(add-hook 'before-save-hook #'clang-format--on-save-buffer-hook nil t))
248+
249+
(defun clang-format--on-save-disable ()
250+
"Enable the minor mode."
251+
(remove-hook 'before-save-hook #'clang-format--on-save-buffer-hook t))
252+
253+
;; Default value for `clang-format-on-save-p'.
254+
(defun clang-format-on-save-check-config-exists ()
255+
"Return non-nil when `.clang-format' is found in a parent directory."
256+
;; Unlikely but possible this is nil.
257+
(let ((filepath buffer-file-name))
258+
(cond
259+
(filepath
260+
(not (null (locate-dominating-file (file-name-directory filepath) ".clang-format"))))
261+
(t
262+
nil))))
263+
264+
;;;###autoload
265+
(define-minor-mode clang-format-on-save-mode
266+
"Clang-format on save minor mode."
267+
:global nil
268+
:lighter ""
269+
:keymap nil
270+
271+
(cond
272+
(clang-format-on-save-mode
273+
(clang-format--on-save-enable))
274+
(t
275+
(clang-format--on-save-disable))))
276+
220277
(provide 'clang-format)
221278
;;; clang-format.el ends here

0 commit comments

Comments
 (0)