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