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> <c-o>:pyf <path-to-this-file>/clang-format.py<cr> 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 20 import difflib 21 import json 22 import subprocess 23 import sys 24 import vim 25 26 # set g:clang_format_path to the path to clang-format if it is not on the path 27 # Change this to the full path if clang-format is not on the path. 28 binary = 'clang-format' 29 if vim.eval('exists("g:clang_format_path")') == "1": 30 binary = vim.eval('g:clang_format_path') 31 32 # Change this to format according to other formatting styles. See the output of 33 # 'clang-format --help' for a list of supported styles. The default looks for 34 # a '.clang-format' or '_clang-format' file to indicate the style that should be 35 # used. 36 style = 'file' 37 if vim.eval('exists("g:clang_format_fallback_style")') == "1": 38 fallback_style = vim.eval('g:clang_format_fallback_style') 39 40 def main(): 41 # Get the current text. 42 buf = vim.current.buffer 43 text = '\n'.join(buf) 44 45 # Determine range to format. 46 lines = '%s:%s' % (vim.current.range.start + 1, vim.current.range.end + 1) 47 48 # Determine the cursor position. 49 cursor = int(vim.eval('line2byte(line("."))+col(".")')) - 2 50 if cursor < 0: 51 print 'Couldn\'t determine cursor position. Is your file empty?' 52 return 53 54 # Avoid flashing an ugly, ugly cmd prompt on Windows when invoking clang-format. 55 startupinfo = None 56 if sys.platform.startswith('win32'): 57 startupinfo = subprocess.STARTUPINFO() 58 startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW 59 startupinfo.wShowWindow = subprocess.SW_HIDE 60 61 # Call formatter. 62 command = [binary, '-lines', lines, '-style', style, '-cursor', str(cursor)] 63 if fallback_style: 64 command.extend(['-fallback-style', fallback_style]) 65 if vim.current.buffer.name: 66 command.extend(['-assume-filename', vim.current.buffer.name]) 67 p = subprocess.Popen(command, 68 stdout=subprocess.PIPE, stderr=subprocess.PIPE, 69 stdin=subprocess.PIPE, startupinfo=startupinfo) 70 stdout, stderr = p.communicate(input=text) 71 72 # If successful, replace buffer contents. 73 if stderr: 74 print stderr 75 76 if not stdout: 77 print ('No output from clang-format (crashed?).\n' + 78 'Please report to bugs.llvm.org.') 79 else: 80 lines = stdout.split('\n') 81 output = json.loads(lines[0]) 82 lines = lines[1:] 83 sequence = difflib.SequenceMatcher(None, vim.current.buffer, lines) 84 for op in reversed(sequence.get_opcodes()): 85 if op[0] is not 'equal': 86 vim.current.buffer[op[1]:op[2]] = lines[op[3]:op[4]] 87 vim.command('goto %d' % (output['Cursor'] + 1)) 88 89 main() 90