diff options
| author | Daniel Jasper <djasper@google.com> | 2012-12-05 21:06:08 +0000 |
|---|---|---|
| committer | Daniel Jasper <djasper@google.com> | 2012-12-05 21:06:08 +0000 |
| commit | 79e68bcf0e0eeb5e3127272f64243155df1e92d9 (patch) | |
| tree | 07b9291fc9a47d796e47c833bba944c051057b4d | |
| parent | 05176cad2145d7f8ff751303389883fd21295152 (diff) | |
| download | bcm5719-llvm-79e68bcf0e0eeb5e3127272f64243155df1e92d9.tar.gz bcm5719-llvm-79e68bcf0e0eeb5e3127272f64243155df1e92d9.zip | |
Basic clang-format vim-integration.
File contains installation and usage instructions.
llvm-svn: 169421
| -rw-r--r-- | clang-tools-extra/clang-format/clang-format.py | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/clang-tools-extra/clang-format/clang-format.py b/clang-tools-extra/clang-format/clang-format.py new file mode 100644 index 00000000000..b674a764442 --- /dev/null +++ b/clang-tools-extra/clang-format/clang-format.py @@ -0,0 +1,52 @@ +# This file is a minimal clang-format vim-integration. To install: +# - Change 'binary' if clang-format is not on the path (see below). +# - Add to your .vimrc: +# +# map <C-I> :pyf <path-to-this-file>/clang-format.py<CR> +# imap <C-I> <ESC>:pyf <path-to-this-file>/clang-format.py<CR>i +# +# The first line enables clang-format for NORMAL and VISUAL mode, the second +# line adds support for INSERT mode. Change "C-I" to another binding if you +# need clang-format on a different key (C-I stands for Ctrl+i). +# +# With this integration you can press the bound key and clang-format will +# format the current line in NORMAL and INSERT mode or the selected region in +# VISUAL mode. The line or region is extended to the next bigger syntactic +# entity. +# +# It operates on the current, potentially unsaved buffer and does not create +# or save any files. To revert a formatting, just undo. + +import vim +import subprocess + +# Change this to the full path if clang-format is not on the path. +binary = 'clang-format' + +# Get the current text. +buf = vim.current.buffer +text = "\n".join(buf) + +# Determine range to format. +offset = int(vim.eval('line2byte(' + + str(vim.current.range.start + 1) + ')')) - 1 +length = int(vim.eval('line2byte(' + + str(vim.current.range.end + 2) + ')')) - offset - 2 + +# Call formatter. +p = subprocess.Popen([binary, '-offset', str(offset), '-length', str(length)], + stdout=subprocess.PIPE, stderr=subprocess.PIPE, + stdin=subprocess.PIPE) +stdout, stderr = p.communicate(input=text) + +# If successful, replace buffer contents. +if stderr: + print stderr +else: + if stdout != text: + lines = stdout.split('\n') + for i in range(min(len(buf), len(lines))): + buf[i] = lines[i] + for line in lines[len(buf):]: + buf.append(line) + del buf[len(lines):] |

