Add automatic formatting on save for C++ using ClangFormat

This can be enabled by setting both `c-c++-enable-clang-format-on-save` and
`c-c++-enable-clang-support` to `t`.
This commit is contained in:
Robbert van der Helm 2017-04-07 11:21:05 +02:00 committed by syl20bnr
parent 1d82d8a0f8
commit 91a2ca456f
4 changed files with 21 additions and 1 deletions

View File

@ -84,6 +84,14 @@ particular mode or all modes in your =dotspacemacs/user-config= (within your
(define-key c++-mode-map [tab] 'clang-format-buffer))
#+END_SRC
To enable automatic buffer formatting on save, set the variable
=c-c++-enable-clang-format-on-save= to =t=:
#+BEGIN_SRC emacs-lisp
(setq-default dotspacemacs-configuration-layers '(
(c-c++ :variables c-c++-enable-clang-format-on-save t)))
#+END_SRC
*** Company-clang and flycheck
This layer adds some fancy improvements to =company-clang=.
It includes a hook to load a projects =.clang_complete= file, which is

View File

@ -14,6 +14,10 @@
(defvar c-c++-enable-clang-support nil
"If non nil Clang related packages and configuration are enabled.")
(defvar c-c++-enable-clang-format-on-save nil
"If non-nil, automatically format code with ClangFormat on
save. Clang support has to be enabled for this to work.")
(spacemacs|define-jump-handlers c++-mode)
(spacemacs|define-jump-handlers c-mode)

View File

@ -9,6 +9,10 @@
;;
;;; License: GPLv3
(defun clang-format/format-on-save ()
"Format buffers with ClangFormat when they get saved."
(add-hook 'before-save-hook 'clang-format-buffer nil t))
(defun company-mode/more-than-prefix-guesser ()
(c-c++/load-clang-args)
(company-clang-guess-prefix))

View File

@ -61,7 +61,11 @@
(defun c-c++/init-clang-format ()
(use-package clang-format
:if c-c++-enable-clang-support))
:if c-c++-enable-clang-support
:init
(when c-c++-enable-clang-format-on-save
(spacemacs/add-to-hooks 'clang-format/format-on-save
'(c-mode-hook c++-mode-hook)))))
(defun c-c++/init-cmake-mode ()
(use-package cmake-mode