[clang] Add "clang-format-on-save-mode" minor mode to clang-format.el (PR #104533)
Campbell Barton via cfe-commits
cfe-commits at lists.llvm.org
Thu Aug 15 18:48:07 PDT 2024
https://github.com/ideasman42 updated https://github.com/llvm/llvm-project/pull/104533
>From d53cf4f059cacadbbb6e32349a26580e7ea1a55b Mon Sep 17 00:00:00 2001
From: Campbell Barton <ideasman42 at gmail.com>
Date: Fri, 16 Aug 2024 11:28:19 +1000
Subject: [PATCH 1/2] Add "clang-format-on-save-mode" minor mode to
clang-format.el
Add a minor mode to run clang-format on save.
---
clang/tools/clang-format/clang-format.el | 59 ++++++++++++++++++++++++
1 file changed, 59 insertions(+)
diff --git a/clang/tools/clang-format/clang-format.el b/clang/tools/clang-format/clang-format.el
index f43bf063c62970..25a5865efdebd2 100644
--- a/clang/tools/clang-format/clang-format.el
+++ b/clang/tools/clang-format/clang-format.el
@@ -70,6 +70,20 @@ in such buffers."
:safe #'stringp)
(make-variable-buffer-local 'clang-format-fallback-style)
+(defcustom clang-format-on-save-p 'clang-format-on-save-check-config-exists
+ "Only reformat on save if this function returns non-nil.
+
+You may wish to choose one of the following options:
+- `always': To always format on save.
+- `clang-format-on-save-check-config-exists':
+ Only reformat when \".clang-format\" exists.
+
+Otherwise you can set this to a user defined function."
+ :group 'clang-format
+ :type 'function
+ :risky t)
+(make-variable-buffer-local 'clang-format-on-save-p)
+
(defun clang-format--extract (xml-node)
"Extract replacements and cursor information from XML-NODE."
(unless (and (listp xml-node) (eq (xml-node-name xml-node) 'replacements))
@@ -217,5 +231,50 @@ the function `buffer-file-name'."
;;;###autoload
(defalias 'clang-format 'clang-format-region)
+;; Format on save minor mode.
+;;
+;; Optional minor mode for formatting on save.
+
+(defun clang-format--on-save-buffer-hook ()
+ "The hook to run on buffer saving to format the buffer."
+ ;; Demote errors as this is user configurable, we can't be sure it wont error.
+ (when (with-demoted-errors "clang-format-on-save: Error %S"
+ (funcall clang-format-on-save-p))
+ (clang-format-buffer))
+ ;; Continue to save.
+ nil)
+
+(defun clang-format--on-save-enable ()
+ "Disable the minor mode."
+ (add-hook 'before-save-hook #'clang-format--on-save-buffer-hook nil t))
+
+(defun clang-format--on-save-disable ()
+ "Enable the minor mode."
+ (remove-hook 'before-save-hook #'clang-format--on-save-buffer-hook t))
+
+;; Default value for `clang-format-on-save-p'.
+(defun clang-format-on-save-check-config-exists ()
+ "Return non-nil when `.clang-format' is found in a parent directory."
+ ;; Unlikely but possible this is nil.
+ (let ((filepath buffer-file-name))
+ (cond
+ (filepath
+ (null (null (locate-dominating-file (file-name-directory filepath) ".clang-format"))))
+ (t
+ nil))))
+
+;;;###autoload
+(define-minor-mode clang-format-on-save-mode
+ "Clang-format on save minor mode."
+ :global nil
+ :lighter ""
+ :keymap nil
+
+ (cond
+ (clang-format-on-save-mode
+ (clang-format--on-save-enable))
+ (t
+ (clang-format--on-save-disable))))
+
(provide 'clang-format)
;;; clang-format.el ends here
>From a3e87f43933a80cca51bc002ed9d4801b18b6b22 Mon Sep 17 00:00:00 2001
From: Campbell Barton <ideasman42 at gmail.com>
Date: Fri, 16 Aug 2024 11:47:59 +1000
Subject: [PATCH 2/2] Minor cleanup.
---
clang/tools/clang-format/clang-format.el | 4 +---
1 file changed, 1 insertion(+), 3 deletions(-)
diff --git a/clang/tools/clang-format/clang-format.el b/clang/tools/clang-format/clang-format.el
index 25a5865efdebd2..64d1d3f5789c72 100644
--- a/clang/tools/clang-format/clang-format.el
+++ b/clang/tools/clang-format/clang-format.el
@@ -232,8 +232,6 @@ the function `buffer-file-name'."
(defalias 'clang-format 'clang-format-region)
;; Format on save minor mode.
-;;
-;; Optional minor mode for formatting on save.
(defun clang-format--on-save-buffer-hook ()
"The hook to run on buffer saving to format the buffer."
@@ -259,7 +257,7 @@ the function `buffer-file-name'."
(let ((filepath buffer-file-name))
(cond
(filepath
- (null (null (locate-dominating-file (file-name-directory filepath) ".clang-format"))))
+ (not (null (locate-dominating-file (file-name-directory filepath) ".clang-format"))))
(t
nil))))
More information about the cfe-commits
mailing list