Daniel Jasper | 7c4a9a0 | 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 | |
Daniel Jasper | 6bd3b93 | 2013-05-21 12:21:39 +0000 | [diff] [blame] | 20 | import json |
Daniel Jasper | 7c4a9a0 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 21 | import subprocess |
Reid Kleckner | 57e6847 | 2013-06-10 14:16:26 +0000 | [diff] [blame] | 22 | import sys |
Daniel Jasper | 6bd3b93 | 2013-05-21 12:21:39 +0000 | [diff] [blame] | 23 | import vim |
Daniel Jasper | 7c4a9a0 | 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 | |
Daniel Jasper | 6391183 | 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 | |
Daniel Jasper | 7c4a9a0 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 32 | # Get the current text. |
| 33 | buf = vim.current.buffer |
Daniel Jasper | 6bd3b93 | 2013-05-21 12:21:39 +0000 | [diff] [blame] | 34 | text = '\n'.join(buf) |
Daniel Jasper | 7c4a9a0 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 35 | |
| 36 | # Determine range to format. |
Daniel Jasper | 6bd3b93 | 2013-05-21 12:21:39 +0000 | [diff] [blame] | 37 | cursor = int(vim.eval('line2byte(line("."))+col(".")')) - 2 |
Alexander Kornienko | 3727c8f | 2013-07-20 01:01:25 +0000 | [diff] [blame^] | 38 | lines = '%s:%s' % (vim.current.range.start + 1, vim.current.range.end + 1) |
Daniel Jasper | 7c4a9a0 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 39 | |
Reid Kleckner | 57e6847 | 2013-06-10 14:16:26 +0000 | [diff] [blame] | 40 | # Avoid flashing an ugly, ugly cmd prompt on Windows when invoking clang-format. |
| 41 | startupinfo = None |
| 42 | if sys.platform.startswith('win32'): |
| 43 | startupinfo = subprocess.STARTUPINFO() |
| 44 | startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW |
| 45 | startupinfo.wShowWindow = subprocess.SW_HIDE |
| 46 | |
Daniel Jasper | 7c4a9a0 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 47 | # Call formatter. |
Alexander Kornienko | 3727c8f | 2013-07-20 01:01:25 +0000 | [diff] [blame^] | 48 | p = subprocess.Popen([binary, '-lines', lines, '-style', style, |
| 49 | '-cursor', str(cursor)], |
Daniel Jasper | 7c4a9a0 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 50 | stdout=subprocess.PIPE, stderr=subprocess.PIPE, |
Reid Kleckner | 57e6847 | 2013-06-10 14:16:26 +0000 | [diff] [blame] | 51 | stdin=subprocess.PIPE, startupinfo=startupinfo) |
Daniel Jasper | 7c4a9a0 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 52 | stdout, stderr = p.communicate(input=text) |
| 53 | |
| 54 | # If successful, replace buffer contents. |
| 55 | if stderr: |
| 56 | message = stderr.splitlines()[0] |
| 57 | parts = message.split(' ', 2) |
| 58 | if len(parts) > 2: |
| 59 | message = parts[2] |
| 60 | print 'Formatting failed: %s (total %d warnings, %d errors)' % ( |
| 61 | message, stderr.count('warning:'), stderr.count('error:')) |
| 62 | |
| 63 | if not stdout: |
| 64 | print ('No output from clang-format (crashed?).\n' + |
| 65 | 'Please report to bugs.llvm.org.') |
Daniel Jasper | 6bd3b93 | 2013-05-21 12:21:39 +0000 | [diff] [blame] | 66 | else: |
Daniel Jasper | 7c4a9a0 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 67 | lines = stdout.split('\n') |
Daniel Jasper | 6bd3b93 | 2013-05-21 12:21:39 +0000 | [diff] [blame] | 68 | output = json.loads(lines[0]) |
| 69 | lines = lines[1:] |
| 70 | if '\n'.join(lines) != text: |
Daniel Jasper | 7248217 | 2013-07-19 09:30:44 +0000 | [diff] [blame] | 71 | common_length = min(len(buf), len(lines)) |
| 72 | buf[:common_length] = lines[:common_length] |
Daniel Jasper | 6bd3b93 | 2013-05-21 12:21:39 +0000 | [diff] [blame] | 73 | for line in lines[len(buf):]: |
| 74 | buf.append(line) |
| 75 | del buf[len(lines):] |
| 76 | vim.command('goto %d' % (output['Cursor'] + 1)) |