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 | b7e02b1 | 2013-07-21 10:45:33 +0000 | [diff] [blame] | 20 | import difflib |
djasper | d0252b7 | 2013-05-21 12:21:39 +0000 | [diff] [blame] | 21 | import json |
djasper | 7f66360 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 22 | import subprocess |
rnk | 8f19196 | 2013-06-10 14:16:26 +0000 | [diff] [blame] | 23 | import sys |
djasper | d0252b7 | 2013-05-21 12:21:39 +0000 | [diff] [blame] | 24 | import vim |
djasper | 7f66360 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 25 | |
| 26 | # Change this to the full path if clang-format is not on the path. |
| 27 | binary = 'clang-format' |
| 28 | |
chandlerc | e6992fd | 2013-09-02 07:42:02 +0000 | [diff] [blame] | 29 | # Change this to format according to other formatting styles. See the output of |
| 30 | # 'clang-format --help' for a list of supported styles. The default looks for |
hans | 79a882f | 2013-09-10 15:41:12 +0000 | [diff] [blame] | 31 | # a '.clang-format' or '_clang-format' file to indicate the style that should be |
| 32 | # used. |
chandlerc | e6992fd | 2013-09-02 07:42:02 +0000 | [diff] [blame] | 33 | style = 'file' |
djasper | 05e7331 | 2013-04-09 15:23:04 +0000 | [diff] [blame] | 34 | |
ace2001ac | ba28cea | 2014-03-10 22:12:14 +0000 | [diff] [blame] | 35 | def main(): |
| 36 | # Get the current text. |
| 37 | buf = vim.current.buffer |
| 38 | text = '\n'.join(buf) |
djasper | 7f66360 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 39 | |
ace2001ac | ba28cea | 2014-03-10 22:12:14 +0000 | [diff] [blame] | 40 | # Determine range to format. |
| 41 | cursor = int(vim.eval('line2byte(line("."))+col(".")')) - 2 |
| 42 | lines = '%s:%s' % (vim.current.range.start + 1, vim.current.range.end + 1) |
djasper | 7f66360 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 43 | |
ace2001ac | ba28cea | 2014-03-10 22:12:14 +0000 | [diff] [blame] | 44 | # Avoid flashing an ugly, ugly cmd prompt on Windows when invoking clang-format. |
| 45 | startupinfo = None |
| 46 | if sys.platform.startswith('win32'): |
| 47 | startupinfo = subprocess.STARTUPINFO() |
| 48 | startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW |
| 49 | startupinfo.wShowWindow = subprocess.SW_HIDE |
rnk | 8f19196 | 2013-06-10 14:16:26 +0000 | [diff] [blame] | 50 | |
ace2001ac | ba28cea | 2014-03-10 22:12:14 +0000 | [diff] [blame] | 51 | # Call formatter. |
| 52 | command = [binary, '-lines', lines, '-style', style, '-cursor', str(cursor)] |
| 53 | if vim.current.buffer.name: |
| 54 | command.extend(['-assume-filename', vim.current.buffer.name]) |
| 55 | p = subprocess.Popen(command, |
| 56 | stdout=subprocess.PIPE, stderr=subprocess.PIPE, |
| 57 | stdin=subprocess.PIPE, startupinfo=startupinfo) |
| 58 | stdout, stderr = p.communicate(input=text) |
djasper | 7f66360 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 59 | |
ace2001ac | ba28cea | 2014-03-10 22:12:14 +0000 | [diff] [blame] | 60 | # If successful, replace buffer contents. |
| 61 | if stderr: |
nico | 43f8ef4 | 2014-04-17 17:51:57 +0000 | [diff] [blame^] | 62 | print stderr |
djasper | 7f66360 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 63 | |
ace2001ac | ba28cea | 2014-03-10 22:12:14 +0000 | [diff] [blame] | 64 | if not stdout: |
| 65 | print ('No output from clang-format (crashed?).\n' + |
| 66 | 'Please report to bugs.llvm.org.') |
| 67 | else: |
| 68 | lines = stdout.split('\n') |
| 69 | output = json.loads(lines[0]) |
| 70 | lines = lines[1:] |
| 71 | sequence = difflib.SequenceMatcher(None, vim.current.buffer, lines) |
| 72 | for op in reversed(sequence.get_opcodes()): |
| 73 | if op[0] is not 'equal': |
| 74 | vim.current.buffer[op[1]:op[2]] = lines[op[3]:op[4]] |
| 75 | vim.command('goto %d' % (output['Cursor'] + 1)) |
| 76 | |
| 77 | main() |