10

Any way to get Vim to format my comments to comply with Google cpplint?

enter image description here

Top section is not compliant, to comply 5 changes are required.

  1. keep this comment at the same level
  2. Add a space after //
  3. add minimum of two spaces between code and comment
  4. align comments
  5. add space after //

(Note I am also asking this question here, it may be possible to do with Tabular https://github.com/godlygeek/tabular/issues/44)

EDIT: As per recommendation below, this works:

Plugin => https://github.com/rhysd/vim-clang-format

.nvimrc or .vimrc Configuration:

let g:clang_format#code_style='google'

let g:clang_format#style_options = {
            \ "AccessModifierOffset" : -3,
            \ "Standard" : "C++03"}
5
  • 3
    clang-format has a googlestyle setting. I believe there is a plugin that you can use to run it from vim.
    – FDinoff
    Feb 11, 2016 at 20:38
  • @FDinoff, awesome it works, if you answer, I will select it as solution... Feb 11, 2016 at 22:19
  • I don't understand (1)--they both have four spaces before the first /, right? Feb 12, 2016 at 4:52
  • @KyleStrand: I believe (1) has to do with not indenting that line to match the other two comments (i.e. OP is specifying an edge case that should not change).
    – Kevin
    Feb 12, 2016 at 5:02
  • @Kevin Ah, I thought it was saying it should be at the same indent-level as something on the previous or next line, but that makes sense. Thanks. Feb 12, 2016 at 5:05

1 Answer 1

11

clang-format is a command line tool that formats c++ code. One of the default formatters is google.

clang-format -style=google

There are plugins that would allow you to run clang-format in vim. One such plugin is https://github.com/rhysd/vim-clang-format (I have not used it, it was the first I found)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.