djasper | 7f66360 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 1 | # This file is a minimal clang-format vim-integration. To install: |
| 2 | # - Change 'binary' if clang-format is not on the path (see below). |
| 3 | # - Add to your .vimrc: |
| 4 | # |
| 5 | # map <C-I> :pyf <path-to-this-file>/clang-format.py<CR> |
| 6 | # imap <C-I> <ESC>:pyf <path-to-this-file>/clang-format.py<CR>i |
| 7 | # |
| 8 | # The first line enables clang-format for NORMAL and VISUAL mode, the second |
| 9 | # line adds support for INSERT mode. Change "C-I" to another binding if you |
| 10 | # need clang-format on a different key (C-I stands for Ctrl+i). |
| 11 | # |
| 12 | # With this integration you can press the bound key and clang-format will |
| 13 | # format the current line in NORMAL and INSERT mode or the selected region in |
| 14 | # VISUAL mode. The line or region is extended to the next bigger syntactic |
| 15 | # entity. |
| 16 | # |
| 17 | # It operates on the current, potentially unsaved buffer and does not create |
| 18 | # or save any files. To revert a formatting, just undo. |
| 19 | |
| 20 | import vim |
| 21 | import subprocess |
| 22 | |
| 23 | # Change this to the full path if clang-format is not on the path. |
| 24 | binary = 'clang-format' |
| 25 | |
djasper | 05e7331 | 2013-04-09 15:23:04 +0000 | [diff] [blame^] | 26 | # Change this to format according to other formatting styles (see |
| 27 | # clang-format -help) |
| 28 | style = 'LLVM' |
| 29 | |
djasper | 7f66360 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 30 | # Get the current text. |
| 31 | buf = vim.current.buffer |
| 32 | text = "\n".join(buf) |
| 33 | |
| 34 | # Determine range to format. |
| 35 | offset = int(vim.eval('line2byte(' + |
| 36 | str(vim.current.range.start + 1) + ')')) - 1 |
| 37 | length = int(vim.eval('line2byte(' + |
| 38 | str(vim.current.range.end + 2) + ')')) - offset - 2 |
| 39 | |
| 40 | # Call formatter. |
djasper | 05e7331 | 2013-04-09 15:23:04 +0000 | [diff] [blame^] | 41 | p = subprocess.Popen([binary, '-offset', str(offset), '-length', str(length), |
| 42 | '-style', style], |
djasper | 7f66360 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 43 | stdout=subprocess.PIPE, stderr=subprocess.PIPE, |
| 44 | stdin=subprocess.PIPE) |
| 45 | stdout, stderr = p.communicate(input=text) |
| 46 | |
| 47 | # If successful, replace buffer contents. |
| 48 | if stderr: |
| 49 | message = stderr.splitlines()[0] |
| 50 | parts = message.split(' ', 2) |
| 51 | if len(parts) > 2: |
| 52 | message = parts[2] |
| 53 | print 'Formatting failed: %s (total %d warnings, %d errors)' % ( |
| 54 | message, stderr.count('warning:'), stderr.count('error:')) |
| 55 | |
| 56 | if not stdout: |
| 57 | print ('No output from clang-format (crashed?).\n' + |
| 58 | 'Please report to bugs.llvm.org.') |
| 59 | elif stdout != text: |
| 60 | lines = stdout.split('\n') |
| 61 | for i in range(min(len(buf), len(lines))): |
| 62 | buf[i] = lines[i] |
| 63 | for line in lines[len(buf):]: |
| 64 | buf.append(line) |
| 65 | del buf[len(lines):] |