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 | |
djasper | d0252b7 | 2013-05-21 12:21:39 +0000 | [diff] [blame] | 20 | import json |
djasper | 7f66360 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 21 | import subprocess |
djasper | d0252b7 | 2013-05-21 12:21:39 +0000 | [diff] [blame] | 22 | import vim |
djasper | 7f66360 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 23 | |
| 24 | # Change this to the full path if clang-format is not on the path. |
| 25 | binary = 'clang-format' |
| 26 | |
djasper | 05e7331 | 2013-04-09 15:23:04 +0000 | [diff] [blame] | 27 | # Change this to format according to other formatting styles (see |
| 28 | # clang-format -help) |
| 29 | style = 'LLVM' |
| 30 | |
djasper | 7f66360 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 31 | # Get the current text. |
| 32 | buf = vim.current.buffer |
djasper | d0252b7 | 2013-05-21 12:21:39 +0000 | [diff] [blame] | 33 | text = '\n'.join(buf) |
djasper | 7f66360 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 34 | |
| 35 | # Determine range to format. |
djasper | d0252b7 | 2013-05-21 12:21:39 +0000 | [diff] [blame] | 36 | cursor = int(vim.eval('line2byte(line("."))+col(".")')) - 2 |
djasper | 7f66360 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 37 | offset = int(vim.eval('line2byte(' + |
| 38 | str(vim.current.range.start + 1) + ')')) - 1 |
| 39 | length = int(vim.eval('line2byte(' + |
| 40 | str(vim.current.range.end + 2) + ')')) - offset - 2 |
| 41 | |
| 42 | # Call formatter. |
djasper | 05e7331 | 2013-04-09 15:23:04 +0000 | [diff] [blame] | 43 | p = subprocess.Popen([binary, '-offset', str(offset), '-length', str(length), |
djasper | d0252b7 | 2013-05-21 12:21:39 +0000 | [diff] [blame] | 44 | '-style', style, '-cursor', str(cursor)], |
djasper | 7f66360 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 45 | stdout=subprocess.PIPE, stderr=subprocess.PIPE, |
| 46 | stdin=subprocess.PIPE) |
| 47 | stdout, stderr = p.communicate(input=text) |
| 48 | |
| 49 | # If successful, replace buffer contents. |
| 50 | if stderr: |
| 51 | message = stderr.splitlines()[0] |
| 52 | parts = message.split(' ', 2) |
| 53 | if len(parts) > 2: |
| 54 | message = parts[2] |
| 55 | print 'Formatting failed: %s (total %d warnings, %d errors)' % ( |
| 56 | message, stderr.count('warning:'), stderr.count('error:')) |
| 57 | |
| 58 | if not stdout: |
| 59 | print ('No output from clang-format (crashed?).\n' + |
| 60 | 'Please report to bugs.llvm.org.') |
djasper | d0252b7 | 2013-05-21 12:21:39 +0000 | [diff] [blame] | 61 | else: |
djasper | 7f66360 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 62 | lines = stdout.split('\n') |
djasper | d0252b7 | 2013-05-21 12:21:39 +0000 | [diff] [blame] | 63 | output = json.loads(lines[0]) |
| 64 | lines = lines[1:] |
| 65 | if '\n'.join(lines) != text: |
| 66 | for i in range(min(len(buf), len(lines))): |
| 67 | buf[i] = lines[i] |
| 68 | for line in lines[len(buf):]: |
| 69 | buf.append(line) |
| 70 | del buf[len(lines):] |
| 71 | vim.command('goto %d' % (output['Cursor'] + 1)) |