Module: Mesa
Branch: main
Commit: 342196f7b05e8d618ff4119de353eda1292b1d45
URL:    
http://cgit.freedesktop.org/mesa/mesa/commit/?id=342196f7b05e8d618ff4119de353eda1292b1d45

Author: Eric Engestrom <[email protected]>
Date:   Mon Jun 19 12:09:41 2023 +0100

docs/coding-style: add example vim config for clang-format

Signed-off-by: Eric Engestrom <[email protected]>
Reviewed-by: Alyssa Rosenzweig <[email protected]>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/23722>

---

 docs/codingstyle.rst | 28 ++++++++++++++++++++++++++++
 1 file changed, 28 insertions(+)

diff --git a/docs/codingstyle.rst b/docs/codingstyle.rst
index 0f3a32ad827..89cafd11642 100644
--- a/docs/codingstyle.rst
+++ b/docs/codingstyle.rst
@@ -21,6 +21,34 @@ running ``ninja -C build/ clang-format``.
 Most code editors also support automatically formatting code as you
 write it; check your editor or its pluggins to see how to enable this.
 
+Vim
+***
+
+Add this to your ``.vimrc`` to automatically format any C & C++ file
+(that has a .clang-format config) when you save it:
+
+.. code:: vim
+
+   augroup ClangFormatOnSave
+     au!
+
+     function! ClangFormatOnSave()
+       " Only format files that have a .clang-format in a parent folder
+       if !empty(findfile('.clang-format', '.;'))
+         let l:formatdiff = 1 " Only format lines that have changed
+         py3f /usr/share/clang/clang-format.py
+       endif
+     endfunction
+
+     autocmd BufWritePre *.h,*.c,*.cc,*.cpp call ClangFormatOnSave()
+   augroup END
+
+If ``/usr/share/clang/clang-format.py`` doesn't exist, try
+``/usr/share/clang/clang-format-$CLANG_VERSION/clang-format.py``
+(replacing ``$CLANG_VERSION`` with your clang version). If your distro
+has put the file somewhere else, look through the files in the package
+providing ``clang-format``.
+
 Basic formatting guidelines
 ---------------------------
 

Reply via email to