Skip to content

Conversation

@ideasman42
Copy link
Contributor

@ideasman42 ideasman42 commented Aug 16, 2024

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

Add a minor mode to run clang-format on save.
@github-actions
Copy link

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be
notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write
permissions for the repository. In which case you can instead tag reviewers by
name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review
by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate
is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

@llvmbot
Copy link
Member

llvmbot commented Aug 16, 2024

@llvm/pr-subscribers-clang-format

Author: Campbell Barton (ideasman42)

Changes

Add a minor mode 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


Full diff: https://github.com/llvm/llvm-project/pull/104533.diff

1 Files Affected:

  • (modified) clang/tools/clang-format/clang-format.el (+59)
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

@mydeveloperday
Copy link
Contributor

I'm not an emacs user and I'm unsure if the other maintainers are. If you are happy to address any issues that might arise I'd be happy for this to land.

@mydeveloperday mydeveloperday self-requested a review August 16, 2024 07:26
@ideasman42
Copy link
Contributor Author

ideasman42 commented Aug 16, 2024

I'm not an emacs user and I'm unsure if the other maintainers are. If you are happy to address any issues that might arise I'd be happy for this to land.

Yes, I'm happy to resolve any issues - for some context, I've been using a version of this for some years.
Originally from this answer https://emacs.stackexchange.com/q/48500/2418

I also maintain an auto-formatting package for autopep8: https://codeberg.org/ideasman42/emacs-py-autopep8

The minor mode just makes it convenient to enable/disable - I find it especially when using C/C++ on some projects that don't use clang-format to check for .clang-format and only format-on-save in this case (that behavior can be configured too).

@ideasman42
Copy link
Contributor Author

Is there any action needed on my side? (the PR was approved over a week ago).

@ideasman42
Copy link
Contributor Author

@mydeveloperday bump, it'd be nice to get this merged if there are no problems with the PR.

@HazardyKnusperkeks HazardyKnusperkeks merged commit 093d4db into llvm:main Oct 19, 2024
@github-actions
Copy link

@ideasman42 Congratulations on having your first Pull Request (PR) merged into the LLVM Project!

Your changes will be combined with recent changes from other authors, then tested by our build bots. If there is a problem with a build, you may receive a report in an email or a comment on this PR.

Please check whether problems have been caused by your change specifically, as the builds can include changes from many authors. It is not uncommon for your change to be included in a build that fails due to someone else's changes, or infrastructure issues.

How to do this, and the rest of the post-merge process, is covered in detail here.

If your change does cause a problem, it may be reverted, or you can revert it yourself. This is a normal part of LLVM development. You can fix your changes and open a new PR to merge them again.

If you don't get any reports, no action is required from you. Your changes are working as expected, well done!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants