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