[cfe-dev] Slightly improved clang-format vim integration

Steffen Prohaska prohaska at zib.de
Tue Sep 17 06:12:20 PDT 2013


On Sep 17, 2013, at 10:10 AM, Daniel Jasper <djasper at google.com> wrote:

> So, remind me again, because it seems to have escaped from this discussion: Why is it a benefit to load this as a python module?

My main reason was that with Python runtimepath handling (vim >= 7.3.1163), I think it's easier to install and use as a module.


> I think the capabilities to configure the plugin with global variables is obvious, but it seems unrelated to this being loaded as a module.

True, and maybe it's not worth breaking compatibility.


> + #     let g:clang_format_style = '{ BasedOnStyle: llvm, IndentWitdh: 4 }'
> IndentWidth..

Thanks.  Fixed.


> +  # Use g:clang_format_binary or default 'clang-format'.
> +  binary = vim.eval(
> +    'exists("g:clang_format_binary") ? g:clang_format_binary : "clang-format"')
> 
> I think we should still default to a constant that is defined higher up in the file, possibly within the introductory paragraph. Yes, it can be configured setting the global options, but in some cases, it might be easier to change an easy to find constant in this file. Same for clang_format_style.

How about the attached revision v4?  It's backward compatible.  The defaults for binary and style are specified after the imports.  The binary can be specified in a global vim var.  The style can be specified as an explicit function arg, a buffer-local vim var (useful for autocmd), or a global vim var.  The install instructions are kept simple.  Using a Python module is only mentioned later as an alternative.

Suggested commit message:

'''
clang-format (vim): Support configuration via vim vars and import as module

'clang-format.py' can now be configured via vim variables
'g:clang_format_binary' and 'b:clang_format_style' or
'g:clang_format_style'.  Using vim variables is more flexible than
hard-coded values in the Python script.  For example, the style can be
changed from vim on-the-fly without modifying the Python script.

Furthermore, 'clang-format.py' can now alternatively be imported and
used as a Python module.  The change is backward compatible.  When used
as a module, the style can explicitly specified when calling the
format() function.

Note also that unnamed buffers are now properly handled.  Previously, a
Python exception was raised for unnamed buffers by subprocess.Popen,
because it was called with None for vim.current.buffer.name.

The implementation should work with vim 7.1 and later (maybe even 7.0).
It has been tested with 7.2.330 (Ubuntu 10.04), 7.3.429 (Ubuntu 12.04),
and MacVim 7.4.
'''

	Steffen

-------------- next part --------------
A non-text attachment was scrubbed...
Name: clang-format.py
Type: text/x-python-script
Size: 4870 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20130917/3426b49b/attachment.bin>


More information about the cfe-dev mailing list