From 342196f7b05e8d618ff4119de353eda1292b1d45 Mon Sep 17 00:00:00 2001 From: Eric Engestrom Date: Mon, 19 Jun 2023 12:09:41 +0100 Subject: [PATCH] docs/coding-style: add example vim config for clang-format Signed-off-by: Eric Engestrom Reviewed-by: Alyssa Rosenzweig Part-of: --- docs/codingstyle.rst | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/docs/codingstyle.rst b/docs/codingstyle.rst index 0f3a32a..89cafd1 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 --------------------------- -- 2.7.4