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 | 0e295f3 | 2013-07-21 10:45:33 +0000 | [diff] [blame] | 20 | import difflib |
Daniel Jasper | 6bd3b93 | 2013-05-21 12:21:39 +0000 | [diff] [blame] | 21 | import json |
Daniel Jasper | 7c4a9a0 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 22 | import subprocess |
Reid Kleckner | 57e6847 | 2013-06-10 14:16:26 +0000 | [diff] [blame] | 23 | import sys |
Daniel Jasper | 6bd3b93 | 2013-05-21 12:21:39 +0000 | [diff] [blame] | 24 | import vim |
Daniel Jasper | 7c4a9a0 | 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 | |
Chandler Carruth | 439fc85 | 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 Wennborg | 9a7a50e | 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. |
Chandler Carruth | 439fc85 | 2013-09-02 07:42:02 +0000 | [diff] [blame] | 33 | style = 'file' |
Daniel Jasper | 6391183 | 2013-04-09 15:23:04 +0000 | [diff] [blame] | 34 | |
Daniel Jasper | 7c4a9a0 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 35 | # Get the current text. |
| 36 | buf = vim.current.buffer |
Daniel Jasper | 6bd3b93 | 2013-05-21 12:21:39 +0000 | [diff] [blame] | 37 | text = '\n'.join(buf) |
Daniel Jasper | 7c4a9a0 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 38 | |
| 39 | # Determine range to format. |
Daniel Jasper | 6bd3b93 | 2013-05-21 12:21:39 +0000 | [diff] [blame] | 40 | cursor = int(vim.eval('line2byte(line("."))+col(".")')) - 2 |
Alexander Kornienko | 3727c8f | 2013-07-20 01:01:25 +0000 | [diff] [blame] | 41 | 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] | 42 | |
Reid Kleckner | 57e6847 | 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 | |
Daniel Jasper | 7c4a9a0 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 50 | # Call formatter. |
Nico Weber | 73e9431 | 2013-10-25 20:06:20 +0000 | [diff] [blame^] | 51 | command = [binary, '-lines', lines, '-style', style, '-cursor', str(cursor)] |
| 52 | if vim.current.buffer.name: |
| 53 | command.extend(['-assume-filename', vim.current.buffer.name]) |
| 54 | p = subprocess.Popen(command, |
Daniel Jasper | 7c4a9a0 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 55 | stdout=subprocess.PIPE, stderr=subprocess.PIPE, |
Reid Kleckner | 57e6847 | 2013-06-10 14:16:26 +0000 | [diff] [blame] | 56 | stdin=subprocess.PIPE, startupinfo=startupinfo) |
Daniel Jasper | 7c4a9a0 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 57 | stdout, stderr = p.communicate(input=text) |
| 58 | |
| 59 | # If successful, replace buffer contents. |
| 60 | if stderr: |
| 61 | message = stderr.splitlines()[0] |
| 62 | parts = message.split(' ', 2) |
| 63 | if len(parts) > 2: |
| 64 | message = parts[2] |
| 65 | print 'Formatting failed: %s (total %d warnings, %d errors)' % ( |
| 66 | message, stderr.count('warning:'), stderr.count('error:')) |
| 67 | |
| 68 | if not stdout: |
| 69 | print ('No output from clang-format (crashed?).\n' + |
| 70 | 'Please report to bugs.llvm.org.') |
Daniel Jasper | 6bd3b93 | 2013-05-21 12:21:39 +0000 | [diff] [blame] | 71 | else: |
Daniel Jasper | 7c4a9a0 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 72 | lines = stdout.split('\n') |
Daniel Jasper | 6bd3b93 | 2013-05-21 12:21:39 +0000 | [diff] [blame] | 73 | output = json.loads(lines[0]) |
| 74 | lines = lines[1:] |
Daniel Jasper | 0e295f3 | 2013-07-21 10:45:33 +0000 | [diff] [blame] | 75 | sequence = difflib.SequenceMatcher(None, vim.current.buffer, lines) |
Daniel Jasper | 4626a20 | 2013-07-22 16:22:13 +0000 | [diff] [blame] | 76 | for op in reversed(sequence.get_opcodes()): |
Daniel Jasper | 0e295f3 | 2013-07-21 10:45:33 +0000 | [diff] [blame] | 77 | if op[0] is not 'equal': |
| 78 | vim.current.buffer[op[1]:op[2]] = lines[op[3]:op[4]] |
| 79 | vim.command('goto %d' % (output['Cursor'] + 1)) |